【Linux】Setting app auto run when system boot with PM2



Install PM2 Product Process Manager in Linux

3. The latest stable version of PM2 is available to install via NPM as shown.
$ sudo npm i -g pm2 
4. Once PM2 installed, you can start your node applications using following commands.
$ sudo node /var/www/html/app/server.js
$ sudo node /var/www/html/adminside/server.js
Note that, in a production environment, you should start them using PM2, as shown (you may not need sudo command if your app is stored in a location where a normal user has read and write permissions).
$ sudo pm2 start /var/www/html/app/server.js
$ sudo pm2 start /var/www/html/adminside/server.js
Start Nodejs App Using PM2
Start Nodejs App Using PM2

Step 4: How to Use and Manage PM2 in Linux

5. To start an application in cluster mode using the -i flag to specify the number of instances, for example.
$ sudo pm2 start /var/www/html/app/server.js -i 4 
$ sudo pm2 scale 0 8   #scale cluster app to 8 processes
6. To list all your node application (process/microservices), run the following command.
$ sudo pm2 list
List All PM2 Managed Node Apps
List All PM2 Managed Node Apps
7. To monitor logs, custom metrics, process information from all processes by running the following command.
$ sudo pm2 monit
Monitor All Node Processes
Monitor All Node Processes
8. To view details of a single Node process as shown, using the process ID or name.
$ sudo pm2 show 0
View Details of Single App
View Details of Single App

Step 5: How to Manage Node Apps Using PM2 in Linux

9. The following is a list of some common process (single or all) management commands you should take note of.
$ sudo pm2 stop all                    #stop all apps
$ sudo pm2 stop 0                      #stop process with ID 0
$ sudo pm2 restart all                 #restart all apps
$ sudo pm2 reset 0            #reset all counters
$ sudo pm2 delete all                  #kill and remove all apps
$ sudo pm2 delete 1                   #kill and delete app with ID 1
10. To manage application logs, use the following commands.
$ sudo pm2 logs                       #view logs for all processes 
$ sudo pm2 logs 1           #view logs for app 1
$ sudo pm2 logs --json                #view logs for all processes in JSON format
$ sudo pm2 flush   #flush all logs
11. To manage the PM2 process, use the following commands.
$ sudo pm2 startup            #enable PM2 to start at system boot
$ sudo pm2 startup systemd    #or explicitly specify systemd as startup system 
$ sudo pm2 save               #save current process list on reboot
$ sudo pm2 unstartup          #disable PM2 from starting at system boot
$ sudo pm2 update       #update PM2 package

Step 6: Access Node Apps From Web Browser

12. To access all your node application from a remote web browser, first you need to open following ports on your system firewall, to allow client connections to the apps as shown.
-------- Debian and Ubuntu -------- 
$ sudo ufw allow 3000/tcp
$ sudo ufw allow 3001/tcp
$ sudo ufw reload

-------- RHEL and CentOS --------
# firewall-cmd --permanent --add-port=3000/tcp
# firewall-cmd --permanent --add-port=3001/tcp
# firewall-cmd --reload 
13. Then access your apps from a web browser with these URLs:
http://198.168.43.31:3000
http://198.168.43.31:3001 
https://www.tecmint.com/install-pm2-to-run-nodejs-apps-on-linux-server/


No comments

Powered by Blogger.