Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用sudo权限通过pm2启动NodeJS以访问SSL密钥_Node.js_Ssl_Ubuntu 16.04_File Permissions_Pm2 - Fatal编程技术网

Node.js 使用sudo权限通过pm2启动NodeJS以访问SSL密钥

Node.js 使用sudo权限通过pm2启动NodeJS以访问SSL密钥,node.js,ssl,ubuntu-16.04,file-permissions,pm2,Node.js,Ssl,Ubuntu 16.04,File Permissions,Pm2,我有一个运行Ubuntu 16.04的数字海洋水滴。我随后在我的NodeJS服务器上使用了HTTPS 简言之 我使用certbot创建了一个SSL证书,这意味着在这个目录/etc/letsencrypt/live/yourdomain.com/,创建了3个文件: privkey.pem 证书 链 因此,在我的服务器代码中,我必须获取这些文件,我这样做: // Certificate const privateKey = fs.readFileSync('/etc/letsencrypt/live

我有一个运行Ubuntu 16.04的数字海洋水滴。我随后在我的NodeJS服务器上使用了HTTPS

简言之 我使用certbot创建了一个SSL证书,这意味着在这个目录
/etc/letsencrypt/live/yourdomain.com/
,创建了3个文件:

  • privkey.pem
  • 证书
  • 因此,在我的服务器代码中,我必须获取这些文件,我这样做:

    // Certificate
    const privateKey = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem', 'utf8');
    const certificate = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/cert.pem', 'utf8');
    const ca = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/chain.pem', 'utf8');
    
    问题 当我尝试使用命令
    node server
    或使用
    pm2 start server
    运行服务器时,我收到以下错误消息:

      { Error: EACCES: permission denied, open '/etc/letsencrypt/live/yourdomain.com/privkey.pem'
        at Object.fs.openSync (fs.js:646:18)
        at Object.fs.readFileSync (fs.js:551:33)
        at Object.<anonymous> (/home/myuser/mywebsite/lib/server-configurations.js:13:21)
        at Module._compile (module.js:653:30)
        at Object.Module._extensions..js (module.js:664:10)
        at Module.load (module.js:566:32)
        at tryModuleLoad (module.js:506:12)
        at Function.Module._load (module.js:498:3)
        at Module.require (module.js:597:17)
        at require (internal/module.js:11:18)
      errno: -13,
      code: 'EACCES',
      syscall: 'open',
      path: '/etc/letsencrypt/live/yourdomain.com/privkey.pem' }
     
    
    {错误:EACCES:权限被拒绝,打开'/etc/letsencrypt/live/yourdomain.com/privkey.pem'
    在Object.fs.openSync(fs.js:646:18)
    在Object.fs.readFileSync(fs.js:551:33)
    在对象上。(/home/myuser/mywebsite/lib/server configurations.js:13:21)
    编译(Module.js:653:30)
    在Object.Module.\u extensions..js(Module.js:664:10)
    在Module.load(Module.js:566:32)
    在tryModuleLoad时(module.js:506:12)
    在Function.Module.\u加载(Module.js:498:3)
    at Module.require(Module.js:597:17)
    根据需要(内部/module.js:11:18)
    errno:-13,
    代码:'EACCES',
    系统调用:“打开”,
    路径:'/etc/letsencrypt/live/yourdomain.com/privkey.pem'}
    
    但是 当我尝试使用
    sudo节点服务器启动它时,
    it没有任何问题

    小通知 我知道我可以更改这些文件的权限,但我不希望这样做,因为我已经多次阅读,最好不要更改这些文件的权限

    最重要的是。。。 谢谢您的帮助:)

    您可以使用


    可能会有帮助,以及此问题的其他解决方案。

    我确实根据更改了权限 这对跑步有效

    node file.js


    现在pm2的进程不知何故无法访问证书,即使它应该以与节点相同的用户身份运行。。。令人困惑。

    为什么不更改这些文件的权限而不是以root用户身份启动应用程序更好?@TGrif我真的不知道,但我已经多次看到有人说不更改Linux中的权限,尤其是敏感数据的权限。我不是真正的Linux专家,因为这是我第一次管理Linux服务器(或者管理服务器)。我很想知道你是否认为更改权限没有问题,你是否认为我的想法有缺陷。我的建议是永远不要以root用户身份启动你的应用程序。因此,我认为你必须以某种方式处理你的证书文件权限。我也有同样的问题。更改权限后,我可以自己运行该程序,但PM2不能,即使它应该以我的用户身份运行。你找到解决办法了吗?我也有同样的问题,重启了机器,它成功了。