How to Install PM2 to Run Node.js Apps on Production Server
PM2 is a unfastened open supply, complicated, environment friendly and cross-platform production-level procedure supervisor for Node.js with a integrated load balancer. It works on Linux, MacOS in addition to Windows. It helps app tracking, environment friendly control of micro-services/processes, operating apps in cluster mode, swish get started and shutdown of apps.
It assists in keeping your apps “alive eternally” with auto restarts and may also be enabled to get started at machine boot, thus taking into consideration High Availability (HA) configurations or architectures.
Notably, PM2 lets in you to run your apps in cluster mode with out making any adjustments for your code (this additionally is dependent on the collection of CPU cores on your server). It additionally lets in you to simply arrange app logs, and so a lot more.
In addition, it additionally has unbelievable give a boost to for primary Node.js frameworks corresponding to Express, Adonis Js, Sails, Hapi and extra, with out want for any code adjustments. PM2 is being utilized by firms such IBM, Microsoft, PayPal, and extra.
In this text, we will be able to provide an explanation for how to set up and use PM2 to run Nodejs apps in Linux manufacturing server. We will create an app for demonstrating a few of PM2’s basic options for you to get began with it.
Step 1: Install Nodejs and NPM in Linux
1. To set up most up-to-date model of Node.js and NPM, first you wish to have to allow authentic NodeSupply repository below your Linux distribution after which set up Node.js and NPM applications as proven.
---------- Install Node.js v11.x ---------- $ curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash - $ sudo apt-get set up -y nodejs ---------- Install Node.js v10.x ---------- $ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - $ sudo apt-get set up -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: Create a Nodejs Application
2. Now, let’s create a checking out utility (we will be able to suppose it has a shopper and admin facet which proportion the similar database), the microservices will run on ports 3000, and 3001 respectively.
$ sudo mkdir -p /var/www/html/app $ sudo mkdir -p /var/www/html/adminside $ sudo vim /var/www/html/app/server.js $ sudo vim /var/www/html/adminside/server.js
Next, replica and paste the next items of code within the
server.js information (exchange 192.168.43.31 together with your server IP).
##mainapp code const http = require('http'); const hostname = '192.168.43.31'; const port = 3000; const server = http.createServer((req, res) => ); server.pay attention(port, hostname, () => ); ##adminside code const http = require('http'); const hostname = '192.168.43.31'; const port = 3001; const server = http.createServer((req, res) => ); server.pay attention(port, hostname, () => );
Save the document and go out.
Step three: Install PM2 Product Process Manager in Linux
three. The newest solid model of PM2 is to be had to set up by way of NPM as proven.
$ sudo npm i -g pm2
four. Once PM2 put in, you’ll get started your node programs the use of following instructions.
$ sudo node /var/www/html/app/server.js $ sudo node /var/www/html/adminside/server.js
Note that, in a manufacturing atmosphere, you will have to get started them the use of PM2, as proven (you won’t want sudo command in case your app is saved in a location the place a standard person has learn and write permissions).
$ sudo pm2 get started /var/www/html/app/server.js $ sudo pm2 get started /var/www/html/adminside/server.js
Step four: How to Use and Manage PM2 in Linux
five. To get started an utility in cluster mode the use of the
-i flag to specify the collection of cases, as an example.
$ sudo pm2 get started /var/www/html/app/server.js -i four $ sudo pm2 scale zero eight #scale cluster app to eight processes
6. To listing your entire node utility (procedure/microservices), run the next command.
$ sudo pm2 listing
7. To track logs, customized metrics, procedure knowledge from all processes by way of operating the next command.
$ sudo pm2 monit
eight. To view main points of a unmarried Node procedure as proven, the use of the method ID or title.
$ sudo pm2 display zero
Step five: How to Manage Node Apps Using PM2 in Linux
nine. The following is a listing of a few commonplace procedure (unmarried or all) control instructions you will have to remember of.
$ sudo pm2 prevent all #prevent all apps $ sudo pm2 prevent zero #prevent procedure with ID zero $ sudo pm2 restart all #restart all apps $ sudo pm2 reset zero #reset all counters $ sudo pm2 delete all #kill and take away all apps $ sudo pm2 delete 1 #kill and delete app with ID 1
10. To arrange utility logs, use the next instructions.
$ 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 structure $ sudo pm2 flush #flush all logs
11. To arrange the PM2 procedure, use the next instructions.
$ sudo pm2 startup #allow PM2 to get started at machine boot $ sudo pm2 startup systemd #or explicitly specify systemd as startup machine $ sudo pm2 save #save present procedure listing on reboot $ sudo pm2 unstartup #disable PM2 from beginning at machine boot $ sudo pm2 replace #replace PM2 bundle
Step 6: Access Node Apps From Web Browser
12. To get right of entry to your entire node utility from a far off internet browser, first you wish to have to open following ports on your machine firewall, to permit shopper connections to the apps as proven.
-------- Debian and Ubuntu -------- $ sudo ufw permit 3000/tcp $ sudo ufw permit 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 get right of entry to your apps from a internet browser with those URLs:
Last however no longer least, PM2 is an easy, inbuilt module machine to prolong its core features, one of the most modules come with pm2-logrotate, pm2-webshell, pm2-server-monit, and extra – you’ll additionally create and use your individual modules.
For additional info, cross to the PM2 GitHub repository: https://github.com/Unitech/PM2/.
That’s all! PM2 is a complicated, and environment friendly production-level procedure supervisor for Node.js with a integrated load balancer. In this text, we confirmed how to set up and use PM2 to arrange Nodejs apps in Linux. If you could have any queries, ship them to use by way of the remark shape under.