{"id":12671,"date":"2019-03-28T06:59:39","date_gmt":"2019-03-28T06:59:39","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12671"},"modified":"2019-03-28T06:59:39","modified_gmt":"2019-03-28T06:59:39","slug":"how-to-install-pm2-to-run-node-js-apps-on-production-server","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/how-to-install-pm2-to-run-node-js-apps-on-production-server\/","title":{"rendered":"How to Install PM2 to Run Node.js Apps on Production Server"},"content":{"rendered":"<p><strong>PM2<\/strong>\u00a0is a free open source, advanced, efficient and cross-platform production-level process manager for\u00a0<strong>Node.js<\/strong>with a built-in load balancer. It works on Linux, MacOS as well as Windows. It supports app monitoring, efficient management of micro-services\/processes, running apps in cluster mode, graceful start and shutdown of apps.<\/p>\n<p>It keeps your apps \u201c<strong>alive forever<\/strong>\u201d with auto restarts and can be enabled to start at system boot, thus allowing for\u00a0<strong>High Availability<\/strong>\u00a0(<strong>HA<\/strong>) configurations or architectures.<\/p>\n<p>Notably,\u00a0<strong>PM2<\/strong>\u00a0allows you to run your apps in\u00a0<strong>cluster mode<\/strong>\u00a0without making any changes in your code (this also depends on the number of CPU cores on your server). It also allows you to easily manage app logs, and so much more.<\/p>\n<p>In addition, it also has incredible support for major\u00a0<strong>Node.js<\/strong>\u00a0frameworks such as\u00a0<strong>Express<\/strong>,\u00a0<strong>Adonis Js<\/strong>,\u00a0<strong>Sails<\/strong>,\u00a0<strong>Hapi<\/strong>and more, without need for any code changes.\u00a0<strong>PM2<\/strong>\u00a0is being used by companies such\u00a0<strong>IBM<\/strong>,\u00a0<strong>Microsoft<\/strong>,\u00a0<strong>PayPal<\/strong>, and more.<\/p>\n<p>In this article, we will explain how to install and use\u00a0<strong>PM2<\/strong>\u00a0to run\u00a0<strong>Nodejs<\/strong>\u00a0apps in Linux production server. We will create an app for demonstrating some of PM2\u2019s fundamental features for you to get started with it.<\/p>\n<h3>Step 1: Install Nodejs and NPM in Linux<\/h3>\n<p><strong>1.<\/strong>\u00a0To install most recent version of\u00a0<strong>Node.js<\/strong>\u00a0and\u00a0<strong>NPM<\/strong>, first you need to enable official NodeSource repository under your Linux distribution and then install\u00a0<strong>Node.js<\/strong>\u00a0and\u00a0<strong>NPM<\/strong>\u00a0packages as shown.<\/p>\n<h4>On Debian\/Ubuntu<\/h4>\n<pre><strong>---------- Install Node.js v11.x ----------<\/strong> \r\n$ curl -sL https:\/\/deb.nodesource.com\/setup_11.x | sudo -E bash -\r\n$ sudo apt-get install -y nodejs\r\n\r\n<strong>---------- Install Node.js v10.x ----------<\/strong>\r\n$ curl -sL https:\/\/deb.nodesource.com\/setup_10.x | sudo -E bash -\r\n$ sudo apt-get install -y nodejs\r\n<\/pre>\n<h4>On CentOS\/RHEL and Fedora<\/h4>\n<pre><strong>---------- Install Node.js v11.x ----------<\/strong> \r\n$ curl -sL https:\/\/rpm.nodesource.com\/setup_11.x | bash -\r\n\r\n<strong>---------- Install Node.js v10.x ----------<\/strong>\r\n$ curl -sL https:\/\/rpm.nodesource.com\/setup_10.x | bash -\r\n<\/pre>\n<h3>Step 2: Create a Nodejs Application<\/h3>\n<p><strong>2.<\/strong>\u00a0Now, let\u2019s create a testing application (we will assume it has a client and admin side which share the same database), the microservices will run on ports\u00a0<strong>3000<\/strong>, and\u00a0<strong>3001<\/strong>\u00a0respectively.<\/p>\n<pre>$ sudo mkdir -p \/var\/www\/html\/app\r\n$ sudo mkdir -p \/var\/www\/html\/adminside\r\n$ sudo vim \/var\/www\/html\/app\/server.js\r\n$ sudo vim \/var\/www\/html\/adminside\/server.js<\/pre>\n<p>Next, copy and paste the following pieces of code in the\u00a0<code>server.js<\/code>\u00a0files (replace\u00a0<strong>192.168.43.31<\/strong>\u00a0with your server IP).<\/p>\n<pre><strong>##mainapp code<\/strong>\r\nconst http = require('http');\r\n\r\nconst hostname = '192.168.43.31';\r\nconst port = 3000;\r\n\r\nconst server = http.createServer((req, res) =&gt; {\r\n\tres.statusCode = 200;\r\n  \tres.setHeader('Content-Type', 'text\/plain');\r\n  \tres.end('This is the Main App!\\n');\r\n});\r\n\r\nserver.listen(port, hostname, () =&gt; {\r\n  \tconsole.log(`Server running at http:\/\/${hostname}:${port}\/`);\r\n});\r\n<strong>##adminside code<\/strong>\r\nconst http = require('http');\r\n\r\nconst hostname = '192.168.43.31';\r\nconst port = 3001;\r\n\r\nconst server = http.createServer((req, res) =&gt; {\r\n\tres.statusCode = 200;\r\n  \tres.setHeader('Content-Type', 'text\/plain');\r\n  \tres.end('This is the Admin Side!\\n');\r\n});\r\n\r\nserver.listen(port, hostname, () =&gt; {\r\n  \tconsole.log(`Server running at http:\/\/${hostname}:${port}\/`);\r\n});\r\n<\/pre>\n<p>Save the file and exit.<\/p>\n<h3>Step 3: Install PM2 Product Process Manager in Linux<\/h3>\n<p><strong>3.<\/strong>\u00a0The latest stable version of\u00a0<strong>PM2<\/strong>\u00a0is available to install via\u00a0<strong>NPM<\/strong>\u00a0as shown.<\/p>\n<pre>$ sudo npm i -g pm2 \r\n<\/pre>\n<p><strong>4.<\/strong>\u00a0Once\u00a0<strong>PM2<\/strong>\u00a0installed, you can start your node applications using following commands.<\/p>\n<pre>$ sudo node \/var\/www\/html\/app\/server.js\r\n$ sudo node \/var\/www\/html\/adminside\/server.js\r\n<\/pre>\n<p>Note that, in a production environment, you should start them using\u00a0<strong>PM2<\/strong>, as shown (you may not need\u00a0<a href=\"https:\/\/www.tecmint.com\/su-vs-sudo-and-how-to-configure-sudo-in-linux\/\" target=\"_blank\" rel=\"noopener\">sudo command<\/a>\u00a0if your app is stored in a location where a normal user has read and write permissions).<\/p>\n<pre>$ sudo pm2 start \/var\/www\/html\/app\/server.js\r\n$ sudo pm2 start \/var\/www\/html\/adminside\/server.js\r\n<\/pre>\n<div id=\"attachment_31212\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/12\/start-nodejs-app-using-pm2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31212\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/12\/start-nodejs-app-using-pm2.png\" alt=\"Start Nodejs App Using PM2\" width=\"632\" height=\"691\" aria-describedby=\"caption-attachment-31212\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-31212\" class=\"wp-caption-text\">Start Nodejs App Using PM2<\/p>\n<\/div>\n<h3>Step 4: How to Use and Manage PM2 in Linux<\/h3>\n<p><strong>5.<\/strong>\u00a0To start an application in cluster mode using the\u00a0<code>-i<\/code>\u00a0flag to specify the number of instances, for example.<\/p>\n<pre>$ sudo pm2 start \/var\/www\/html\/app\/server.js -i 4 \r\n$ sudo pm2 scale 0 8\t\t\t#scale cluster app to 8 processes\r\n<\/pre>\n<p><strong>6.<\/strong>\u00a0To list all your node application (process\/microservices), run the following command.<\/p>\n<pre>$ sudo pm2 list\r\n<\/pre>\n<div id=\"attachment_31213\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/12\/list-all-pm2-managed-node-apps.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31213\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/12\/list-all-pm2-managed-node-apps.png\" alt=\"List All PM2 Managed Node Apps\" width=\"692\" height=\"249\" aria-describedby=\"caption-attachment-31213\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-31213\" class=\"wp-caption-text\">List All PM2 Managed Node Apps<\/p>\n<\/div>\n<p><strong>7.<\/strong>\u00a0To monitor logs, custom metrics, process information from all processes by running the following command.<\/p>\n<pre>$ sudo pm2 monit\r\n<\/pre>\n<div id=\"attachment_31214\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/12\/monitor-all-node-processes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31214\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/12\/monitor-all-node-processes.png\" alt=\"Monitor All Node Processes\" width=\"692\" height=\"667\" aria-describedby=\"caption-attachment-31214\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-31214\" class=\"wp-caption-text\">Monitor All Node Processes<\/p>\n<\/div>\n<p><strong>8.<\/strong>\u00a0To view details of a single Node process as shown, using the process ID or name.<\/p>\n<pre>$ sudo pm2 show 0\r\n<\/pre>\n<div id=\"attachment_31215\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/12\/view-details-of-a-single-managed-app.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31215\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/12\/view-details-of-a-single-managed-app.png\" alt=\"View Details of Single App\" width=\"692\" height=\"667\" aria-describedby=\"caption-attachment-31215\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-31215\" class=\"wp-caption-text\">View Details of Single App<\/p>\n<\/div>\n<h3>Step 5: How to Manage Node Apps Using PM2 in Linux<\/h3>\n<p><strong>9.<\/strong>\u00a0The following is a list of some common process (single or all) management commands you should take note of.<\/p>\n<pre>$ sudo pm2 stop all                  \t\t#stop all apps\r\n$ sudo pm2 stop 0                    \t\t#stop process with ID 0\r\n$ sudo pm2 restart all               \t\t#restart all apps\r\n$ sudo pm2 reset 0\t\t         \t#reset all counters\r\n$ sudo pm2 delete all                \t\t#kill and remove all apps\r\n$ sudo pm2 delete 1                 \t\t#kill and delete app with ID 1<\/pre>\n<p><strong>10.<\/strong>\u00a0To manage application logs, use the following commands.<\/p>\n<pre>$ sudo pm2 logs                      \t#view logs for all processes \r\n$ sudo pm2 logs 1\t         \t#view logs for app 1\r\n$ sudo pm2 logs --json               \t#view logs for all processes in JSON format\r\n$ sudo pm2 flush\t\t\t#flush all logs\r\n<\/pre>\n<p><strong>11.<\/strong>\u00a0To manage the PM2 process, use the following commands.<\/p>\n<pre>$ sudo pm2 startup            #enable PM2 to start at system boot\r\n$ sudo pm2 startup systemd    #or explicitly specify systemd as startup system \r\n$ sudo pm2 save               #save current process list on reboot\r\n$ sudo pm2 unstartup          #disable PM2 from starting at system boot\r\n$ sudo pm2 update\t      #update PM2 package\r\n<\/pre>\n<h3>Step 6: Access Node Apps From Web Browser<\/h3>\n<p><strong>12.<\/strong>\u00a0To 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.<\/p>\n<pre><strong>-------- Debian and Ubuntu --------<\/strong> \r\n$ sudo ufw allow 3000\/tcp\r\n$ sudo ufw allow 3001\/tcp\r\n$ sudo ufw reload\r\n\r\n<strong>-------- RHEL and CentOS --------<\/strong>\r\n# firewall-cmd --permanent --add-port=3000\/tcp\r\n# firewall-cmd --permanent --add-port=3001\/tcp\r\n# firewall-cmd --reload \r\n<\/pre>\n<p><strong>13.<\/strong>\u00a0Then access your apps from a web browser with these URLs:<\/p>\n<pre>http:\/\/198.168.43.31:3000\r\nhttp:\/\/198.168.43.31:3001 \r\n<\/pre>\n<div id=\"attachment_31216\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/12\/access-node-apps-from-web-browser.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31216\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/12\/access-node-apps-from-web-browser.png\" alt=\"Access Node Apps from Browser\" width=\"691\" height=\"370\" aria-describedby=\"caption-attachment-31216\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-31216\" class=\"wp-caption-text\">Access Node Apps from Browser<\/p>\n<\/div>\n<p>Last but not least,\u00a0<strong>PM2<\/strong>\u00a0is a simple, in-built module system to extend its core capabilities, some of the modules include pm2-logrotate, pm2-webshell, pm2-server-monit, and more \u2013 you can also create and use your own modules.<\/p>\n<p>For more information, go to the PM2 GitHub repository:\u00a0<a href=\"https:\/\/github.com\/Unitech\/PM2\/\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/github.com\/Unitech\/PM2\/<\/a>.<\/p>\n<p>That\u2019s all!\u00a0<strong>PM2<\/strong>\u00a0is an advanced, and efficient production-level process manager for\u00a0<strong>Node.js<\/strong>\u00a0with a built-in load balancer. In this article, we showed how to install and use PM2 to manage Nodejs apps in Linux. If you have any queries, send them to use via the comment form below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/install-pm2-to-run-nodejs-apps-on-linux-server\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PM2\u00a0is a free open source, advanced, efficient and cross-platform production-level process manager for\u00a0Node.jswith a built-in load balancer. It works on Linux, MacOS as well as Windows. It supports app monitoring, efficient management of micro-services\/processes, running apps in cluster mode, graceful start and shutdown of apps. It keeps your apps \u201calive forever\u201d with auto restarts and &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/how-to-install-pm2-to-run-node-js-apps-on-production-server\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Install PM2 to Run Node.js Apps on Production Server&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12671","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12671"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12671\/revisions"}],"predecessor-version":[{"id":12672,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12671\/revisions\/12672"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}