Ultimate solution without restarting server.js every time.
Step 1: Install Nodejs and NPM in Linux
1. To install most recent version of
Node.js and
NPM, first you need to enable official NodeSource repository under your Linux distribution and then install
Node.js and
NPM packages as shown.
On Debian/Ubuntu
---------- Install Node.js v11.x ----------
$ curl -sL
https://deb.nodesource.com/setup_11.x | sudo -E bash -
$ sudo apt-get install -y nodejs
---------- Install Node.js v10.x ----------
$ curl -sL
https://deb.nodesource.com/setup_10.x | sudo -E bash -
$ sudo apt-get install -y nodejs
On CentOS/RHEL and Fedora
---------- Install Node.js v11.x ----------
$ curl -sL
https://rpm.nodesource.com/setup_11.x | bash -
---------- Install Node.js v10.x ----------
$ curl -sL
https://rpm.nodesource.com/setup_10.x | bash -
Step 2: 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
Step 3: Start Node.js Applications/Processes
Next, you want to start your
Node.js applications using
PM2 as follows. If you already have them up and running, started via
PM2, you can skip this step:
# cd /patch/you/script/server.js
# pm2 start /patch/you/script/server.js -n server
later you will manage the name "server"
$ sudo pm2 start server
$ sudo pm2 restart server
$ sudo pm2 stop server
Step 4: 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
$ 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.
NB: the port 3000 must be opened according to the one you choose to open
-------- Debian and Ubuntu --------
$ sudo ufw allow 3000/tcp
$ sudo ufw reload
-------- RHEL and CentOS --------
# firewall-cmd --permanent --add-port=3000/tcp
# firewall-cmd --reload
Step 7: Enable cron for restart server.js every 5 minutes
$ crontab -e
paste this code
5 * * * * /usr/local/bin/pm2 restart server >/dev/null 2>&1 >/dev/null 2>&1