Skip to content

Instantly share code, notes, and snippets.

@WillDent
Last active February 2, 2022 13:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save WillDent/7a876d0e4fde2db72c23 to your computer and use it in GitHub Desktop.
Save WillDent/7a876d0e4fde2db72c23 to your computer and use it in GitHub Desktop.
Installation of PM2 on Ubuntu so it autostarts scripts
# Requirements:
# Node Installed
# Safe User that is running as node
#1) Install pm2
sudo npm install -g pm2
#2) Set-up PM2 to autostart upon server rebooting
#sudo env PATH=$PATH:/usr/local/bin pm2 startup <platform> -u <safe_user_not_root>
sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u ubuntu
#If above works then you will see:
#Adding system startup for /etc/init.d/pm2-init.sh ...
#
# /etc/rc0.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
# /etc/rc1.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
# /etc/rc6.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
# /etc/rc2.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
# /etc/rc3.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
# /etc/rc4.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
# /etc/rc5.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
#3) Make your node process start with PM2 as a service
pm2 start <name of start file(run.js, start.js, etc.)>
#IMPORTANT NOTE: Pm2 will only restart items that we're already running prior to server restart.
#OPTIONAL: Running Node on Port 80
##If you want to run NodeJS on port 80 you will need to configure your safe user to allow this as only root can run port 80
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node
#Good Thread on details of above is found here:
#https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps
@simonemarra
Copy link

Thanks man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment