node.js 使用 pm2 以 root 身份启动应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35105100/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:51:04  来源:igfitidea点击:

Start app as root with pm2

node.jsrootpm2

提问by Martin Nilsson

I have a daemon that must be run as root on startup.

我有一个必须在启动时以 root 身份运行的守护进程。

I use pm2 to start other apps but can not figure out if it can start an app as root. Can it be done?

我使用 pm2 启动其他应用程序,但无法确定它是否可以以 root 身份启动应用程序。可以做到吗?

If not, what are my options?

如果没有,我有什么选择?

回答by Elias Fyksen

I had problems with sudo pm2 start api, but this was since pm2 was already running without sudo privileges, therefor you need to run:

我遇到了问题sudo pm2 start api,但这是因为 pm2 已经在没有 sudo 权限的情况下运行,因此您需要运行:

pm2 kill
sudo pm2 start api

This kills the pm2 deamon first, so that it starts in sudo, but then you need sudo for ALLpm2 commands afterwards, like: sudo pm2 ls

这首先会杀死 pm2 守护进程,以便它以 sudo 启动,但是之后您需要为所有pm2 命令使用sudo ,例如:sudo pm2 ls

回答by Rabbits

If you only need your daemon to be run as root in order to access a port number (such as 80 or 443), the pm2 documentationrecommends using authbind. So, if you want the user yourusernameto have access to port 80, run:

如果您只需要以 root 用户身份运行守护程序以访问端口号(例如 80 或 443),则pm2 文档建议使用authbind. 因此,如果您希望用户yourusername能够访问端口 80,请运行:

$ sudo apt-get install authbind
$ sudo touch /etc/authbind/byport/80
$ sudo chown yourusername /etc/authbind/byport/80
$ sudo chmod 755 /etc/authbind/byport/80
$ authbind --deep pm2 update

And then use authbind --deep pm2instead of pm2. The documentation suggests setting up an alias.

然后使用authbind --deep pm2代替pm2。文档建议设置别名。

回答by devtech

I would recommend:

我会推荐:

sudo pm2 start index.js

OR

或者

pm2 start 'http-server' /var/www -p 80
sudo pm2 startup
pm2 save

OR

或者

pm2 start 'which http-server' /var/www -p 80

To start it on your HTTP Port

在您的 HTTP 端口上启动它

Also, I always put -i 0at the end - this starts up as many worker processes as you have cores. Check THIS

另外,我总是放在-i 0最后 - 这会启动与您拥有的内核一样多的工作进程。检查这个

It is not always necessary to start PM2 as root. If you have PM2 as root and the cli module installed, security is a big risk. This is only required if you're starting your app on a port between 1 and 1024

并不总是需要以 root 身份启动 PM2。如果您以 root 身份安装了 PM2 并安装了 cli 模块,那么安全性是一个很大的风险。仅当您在 1 到 1024 之间的端口上启动应用程序时才需要

回答by Manohar Reddy Poreddy

Wasted about an hour

浪费了大约一个小时

On AWS EC2 machine, one system was in inconsistent state due to earlier installations, that forced sudoelevations in the application for all commands to OS, like sh, etc.

在 AWS EC2 机器上,一个系统由于早期安装而处于不一致状态,这迫使sudo应用程序中的所有命令都提升到操作系统,例如 sh 等。

pm2 wasrunning as root:

PM2运行作为根:

ps aux | grep pm2
# root ... PM2 v4.1.2: God Daemon (/root/.pm2)

Now pm2 isrunning as ubuntu:

现在,PM2运行的Ubuntu的:

ps aux | grep pm2
# ubuntu  ...  PM2 v4.1.2: God Daemon (/home/ubuntu/.pm2)

Below commands worked:

以下命令有效:

sudo pm2 kill
sudo npm remove pm2 -g

sudo npm i -g pm2@latest
sudo pm2 update
sudo chown -R ubuntu:ubuntu /home/ubuntu/.pm2

Hope that helps

希望有帮助

回答by Nuwan Sameera

First, install pm2 globally. Then set root permissions to pm2 using this command

首先,全局安装pm2。然后使用此命令将 root 权限设置为 pm2

sudo chown ubuntu:ubuntu /home/ubuntu/.pm2/rpc.sock /home/ubuntu/.pm2/pub.sock

回答by Jeremias Binder

you might consider routing your traffic with iptables, since there is a reason behind this errror

您可能会考虑使用 iptables 路由您的流量,因为此错误背后是有原因的

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

回答by Aminadav Glickshtein

You should start pm2 as a root, (sudo pm2 start app.js), then your app will start as a root

您应该以 root 身份启动 pm2,(sudo pm2 start app.js),然后您的应用程序将以 root 身份启动