Start app as root with pm2

点点圈 提交于 2019-12-02 20:26:19

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

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

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:

$ 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.

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

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

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

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

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

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

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

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!