如何运行pm2,以便其他服务器用户可以访问该进程? [英] How to run pm2 so other server users are able to access the process?
问题描述
当我使用 pm2 启动Nodejs应用程序时,其他服务器用户将无法访问该进程.
When I start my Nodejs app with pm2, other server users are not able to access the process.
即使我从自定义目录(不是当前用户的~/
,默认情况下,pm2在使用什么)启动pm2:
Even if I start pm2 from a custom directory (not current user's ~/
, what pm2 is using by default):
HOME=/var/www pm2 start app.js
任何用户都可以访问目录(与~/
相比,但是其他服务器用户仍然无法访问该进程.
Directory is accessible by any user (comparing to ~/
, but there's still no way other server user is able to access the process.
当其他服务器用户执行pm2 list
时,表明他正在运行0个进程-但有(正在由另一个用户启动).当其他用户尝试HOME=/var/www pm2 list
时,CLI会引发错误:
When other server user does pm2 list
, it shows him 0 processes are running – but there are (started by another user). And when other user tries HOME=/var/www pm2 list
, CLI throws an error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: connect EACCES
at errnoException (net.js:905:11)
at Object.afterConnect [as oncomplete] (net.js:896:19)
所以我想知道如何确保用户能够访问其他服务器用户运行的pm2进程?还是应该采用不同的方法?
So I am wondering how to make sure users are able to access pm2 processes run by other server users? Or it shall be approached differently?
我想知道为什么每个服务器用户都能使git pull
从Git存储库中部署最新的源代码,但之后又不能重新启动pm2
进程?只有启动pm2
进程的用户才能重新启动它…很奇怪.
I am wondering why every server user is able to make git pull
to deploy latest source code from a Git repository, but can't restart pm2
process afterwards? Only the user that started pm2
process is able to restart it… Weird.
推荐答案
这是我们绕过此问题的方式.
Here's how we bypassed this.
只需创建一个组
-
创建一个新组
pm2
或任何适合您的名称
$ groupadd pm2
将/var/www/
文件夹组所有者更改为组pm2
Change the /var/www/
folder group owner to group pm2
$ chgrp -R pm2 /var/www
将另一个用户(例如bob)添加到pm2
Add the other user, let's say bob, to pm2
$ usermod -aG pm2 bob
现在bob可以通过将$ HOME更改为/var/www
Now bob can run pm2 commands by changing $HOME to /var/www
$ env HOME=/var/www pm2 list
或者(更好)创建一个@jcollum建议的别名
Or (better still) create an alias as @jcollum suggested
$ alias pm2='env HOME=/var/www pm2'
这篇关于如何运行pm2,以便其他服务器用户可以访问该进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!