To avoid running into issues when running the same script multiple times with different parameters, it is best to give them different names. If you encounter problems while running a script due to pm2 running without sudo privileges, you can use the command “pm2 kill” to kill the pm2 daemon and start it with sudo privileges. However, keep in mind that all pm2 commands afterwards will require sudo. Alternatively, if you need your daemon to access a port number that requires root access, you can use the recommended pm2 documentation solution of using “sudo pm2” instead.
I faced difficulties with
sudo pm2 start api
due to pm2 already running without sudo privileges. To resolve this, you must execute:
sudo pm2 start api
Initially, the pm2 deamon is terminated to enable sudo initiation. However, all subsequent pm2 commands, including
sudo pm2 ls
, require sudo privileges.
To grant access to a port number (like 80 or 443), the pm2 documentation suggests utilizing
rather than running your daemon as root. For instance, if you desire to allow user
access to port 80, execute the following command:
$ 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
authbind --deep pm2
as per the documentation, which recommends creating an alias.
I would recommend:
sudo pm2 start index.js
pm2 start 'http-server' /var/www -p 80
sudo pm2 startup
pm2 start 'which http-server' /var/www -p 80
To start it on your HTTP Port
In addition, I consistently append
to initiate worker processes equivalent to the number of cores. Refer to THIS for verification.
Starting PM2 as root is not mandatory, except when launching your application on a port that falls between 1 and 1024. If you have the cli module installed while running PM2 as root, it poses a major security threat.