node.js 如何运行 pm2 以便其他服务器用户能够访问该进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32178443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run pm2 so other server users are able to access the process?
提问by Nik Sumeiko
When I start my Nodejs app with pm2, other server users are not able to access the process.
当我开始我的应用程序的NodeJS PM2,其他服务器的用户都无法访问该进程。
Even if I start pm2 from a custom directory (not current user's ~/, what pm2 is using by default):
即使我从自定义目录启动 pm2(不是当前用户的~/pm2 默认使用的是什么):
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.
任何用户都可以访问目录(与 相比~/,但其他服务器用户仍然无法访问该进程。
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:
当其他服务器用户这样做时pm2 list,它显示他有 0 个进程正在运行 - 但有(由另一个用户启动)。当其他用户尝试时HOME=/var/www pm2 list,CLI 会抛出错误:
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)
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?
所以我想知道如何确保用户能够访问其他服务器用户运行的 pm2 进程?或者应该以不同的方式处理?
I am wondering why every server user is able to make git pullto deploy latest source code from a Git repository, but can't restart pm2process afterwards? Only the user that started pm2process is able to restart it… Weird.
我想知道为什么每个服务器用户都能够git pull从 Git 存储库部署最新的源代码,但pm2之后无法重新启动进程?只有启动pm2进程的用户才能重新启动它……奇怪。
回答by vinayp
Here's how we bypassed this.
这是我们绕过这个的方法。
Just create a group
只需创建一个组
Create a new group
pm2or whatever name works for you$ groupadd pm2Change the
/var/www/folder group owner to grouppm2$ chgrp -R pm2 /var/wwwAdd the other user, let's say bob, to pm2
$ usermod -aG pm2 bob
创建一个新组
pm2或任何适合您的名称$ groupadd pm2将
/var/www/文件夹组所有者更改为组pm2$ chgrp -R pm2 /var/www添加另一个用户,比如说 bob,到 pm2
$ usermod -aG pm2 bob
Now bob can run pm2 commands by changing $HOME to /var/www
现在 bob 可以通过将 $HOME 更改为/var/www来运行 pm2 命令
$ env HOME=/var/www pm2 list
$ env HOME=/var/www pm2 list
Or (better still) create an alias as @jcollum suggested
或者(更好)按照@jcollum 的建议创建一个别名
$ alias pm2='env HOME=/var/www pm2'
$ alias pm2='env HOME=/var/www pm2'
回答by Ilja Denisovs
Ok, here is my solution for same problem:
好的,这是我对同一问题的解决方案:
# 1. Create user PM2 and set his password
sudo useradd -d /opt/pm2 -m -s /bin/bash pm2
sudo passwd pm2
# 2. Add users you want to provide the access to PM2 to PM2 group
sudo usermod -aG pm2 <username>
# Note: if you added yourself to pm2 group, perform logout and login back to the host machine
# 3. Set the PM2_HOME variable
sudo touch /etc/profile.d/pm2.sh
sudo sh -c 'echo "export PM2_HOME=\"/opt/pm2/.pm2\"" > /etc/profile.d/pm2.sh'
source /etc/profile.d/pm2.sh
# 4. Install the PM2
# Check the npm prefix if fail:
# https://docs.npmjs.com/misc/config#prefix
sudo npm install pm2 -g
# 5. Make startup script
sudo pm2 startup ubuntu -u pm2 --hp /opt/pm2
sudo systemctl enable pm2-pm2 && \
sudo systemctl start pm2-pm2 && \
sudo systemctl status pm2-pm2
# 6. Change permission of PM2_HOME
sudo chmod -v g+w /opt/pm2/.pm2
# 7. Check the PM2
pm2 status
回答by Allen Kung
It seems that PM2 saves data under user's '~/.pm2' folder, so other users can not see your PM2 process with 'pm2 status'.
PM2 好像是把数据保存在用户的“~/.pm2”文件夹下,所以其他用户看不到你的 PM2 进程“pm2 status”。
I created a new linux user for PM2, and all users use 'su pm2user' before starting Pm2 process:
我为 PM2 创建了一个新的 linux 用户,所有用户在启动 Pm2 进程之前都使用“su pm2user”:
$ sudo su pm2user
$ sudo pm2 start app.js
It's a stupid way, but it is simple and works well. Hope this would help :)
这是一种愚蠢的方法,但它很简单,而且效果很好。希望这会有所帮助:)
回答by Diego Luces
Assuming you run pm2 as www-data. To have access to that pm2 instance, I do: sudo -u www-data HOME=/var/www pm2 listfor example. You can, of course, create a script (e.g. supm2) that does that for you so you can just do supm2 listinstead.
假设您将 pm2 作为www-data. 要访问该 pm2 实例,我会这样做:sudo -u www-data HOME=/var/www pm2 list例如。当然,您可以创建一个脚本(例如supm2)来为您执行此操作,以便您可以supm2 list改为执行此操作。
回答by Shrinath Shenoy
I've faced a similar issue. The reason may be that you do not have the required permissions, or you do not own the pid and sock files created by pm2. In my case, it was working fine when I started the pm2 from commandline instead of startup. When I used startup, it was running as root user by default. So root was the owner of the pid, sock files
我遇到过类似的问题。原因可能是您没有所需的权限,或者您没有pm2创建的pid和sock文件。就我而言,当我从命令行启动 pm2 而不是启动时,它运行良好。当我使用启动时,它默认以 root 用户身份运行。所以 root 是 pid、sock 文件的所有者
回答by crankshaft
I know that I am late to the party, but this is how I did it:
我知道我参加聚会迟到了,但我是这样做的:
PM2="/usr/share/nodejs/pm2"
USER="me"
useradd $USER
groupadd pm2
chgrp -R pm2 $PM2
usermod -aG pm2 $USER
setfacl -Rdm g:pm2:rwx $PM2
/etc/bash.bashrc etc
/etc/bash.bashrc 等
export PM2_HOME=$PM2;

