How to Install PM2 to Run Node.js Apps on Production Server

PM2 is a free open source, advanced, efficient and cross-platform production-level process manager for Node.js with a built-in load balancer. It works on Linux, MacOS as well as Windows. It supports app monitoring, efficient...

PM2 is a free open source, advanced, efficient and cross-platform production-level process manager for Node.js 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.

It keeps your apps “alive forever” with auto restarts and can be enabled to start at system boot, thus allowing for High Availability (HA) configurations or architectures.

Notably, PM2 allows you to run your apps in cluster mode without 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.

In addition, it also has incredible support for major Node.js frameworks such as Express, Adonis Js, Sails, Hapi and more, without need for any code changes. PM2 is being used by companies such IBM, Microsoft, PayPal, and more.

In this article, we will explain how to install and use PM2 to run Nodejs apps in Linux production server. We will create an app for demonstrating some of PM2’s fundamental features for you to get started with it.

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.

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).

Last but not least, PM2 is 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 – you can also create and use your own modules.

That’s all! PM2 is an advanced, and efficient production-level process manager for Node.js with 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.