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

时间:2022-10-15 22:04:39

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?

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

5 个解决方案

#1


8  

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 0 at 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。如果您将PM2作为root并安装了cli模块,则安全性是一个很大的风险。只有在1到1024之间的端口上启动应用程序时才需要这样做

#2


8  

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 documentation recommends using authbind. So, if you want the user yourusername to have access to port 80, run:

如果您只需要以root身份运行守护程序以访问端口号(例如80或443),则pm2文档建议使用authbind。因此,如果您希望用户的用户名有权访问端口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 pm2 instead of pm2. The documentation suggests setting up an alias.

然后使用authbind --deep pm2而不是pm2。文档建议设置别名。

#3


1  

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启动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 ALL pm2 commands afterwards, like: sudo pm2 ls

这会首先杀死pm2 deamon,所以它在sudo中启动,但之后你需要sudo for all pm2命令,比如:sudo pm2 ls

#4


0  

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

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

#5


0  

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

#1


8  

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 0 at 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。如果您将PM2作为root并安装了cli模块,则安全性是一个很大的风险。只有在1到1024之间的端口上启动应用程序时才需要这样做

#2


8  

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 documentation recommends using authbind. So, if you want the user yourusername to have access to port 80, run:

如果您只需要以root身份运行守护程序以访问端口号(例如80或443),则pm2文档建议使用authbind。因此,如果您希望用户的用户名有权访问端口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 pm2 instead of pm2. The documentation suggests setting up an alias.

然后使用authbind --deep pm2而不是pm2。文档建议设置别名。

#3


1  

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启动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 ALL pm2 commands afterwards, like: sudo pm2 ls

这会首先杀死pm2 deamon,所以它在sudo中启动,但之后你需要sudo for all pm2命令,比如:sudo pm2 ls

#4


0  

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

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

#5


0  

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