How To Setup PM2 And Run Node.Js Applications

In this blog post , We will learn to setup and configure PM2 and then we will run Node.js applications using PM2 on the production servers.

What Is PM2?

  • PM2 is an acronym of Process Management Module which is used to run and manage Node.js applications.
  • Its an open source with an in-built Load balancer.
  • When a process goes down, PM2 will automatically restart the service and make it Live.
  • PM2 works on Linux , Windows and MacOS.
  • We can enable the process to automatically start on system boot up or while restart.
  • Without the need of changing the application code , We can scale the applications or processes for High Availability.

Installing Nodejs And NPM

Make sure you have sudo or root privileges to install the packages on the system.

First update the Ubuntu’s APT package repository using the below command , If you’re using ubuntu.

We will be using Ubuntu system for this tutorial.

sudo apt-get update

Next step is to add the Nodejs package repository to the system.We can either install the latest version of Nodejs to run applications or the specific version depending on the application code.

If you want to install the specific version of Nodejs , First we need to install NVM (Node Version Manager).

NVM can be installed using the below command.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash

After the installation is completed , We can check the version of nvm installed using the below command.

nvm --version

Now , Lets go ahead and install Nodejs using nvm.

Running the below command will install Nodejs and npm (node package manager)

nvm install node

If you wish to install the specif version of Nodejs , Then execute the following command.

nvm install 8.0.0

Lets check the version of Nodejs and NPM installed on the system using the below commands.

node --version
npm --version

Node : Installing multiple version of Nodejs on the same server , Will not remove any previous installed Nodejs version or packages frmo the system.

Hence We can make use of required version depending on the application needs.

Installing PM2

We need to install PM2 to run Nodejs applications.

PM2 can be installed using NPM , Which installs the latest stable version.

For the PM2 to be able to manage any node applications , Then install pm2 should be installed globally

npm install pm2 -g

PM2 is successfully installed on the system.

To check the version of PM2 installed on the system , Execute the below command.

pm2 --version

Setup Sample Nodejs App For Testing

Lets create a directory and setup sample Nodejs application for testing.

create a folder named Node-PM2test and then inside the folder , Create a file named sample-apps.js

mkdir Node-PM2test
touch sample-app.js

Open the file and add the below contents.

var http = require("http");

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});

    response.end('Testing Nodejs Application with PM2\n');
}).listen(8080);

console.log('Server running at http://127.0.0.1:8080/');

Save and close the file.

Nodejs applications can be started using the following command.

node sample-app.js

But the Issue with starting applications using the above command will not manage the Nodejs process , For that , We have to pm2 to start the applications.

Starting the Nodejs app with PM2,

pm2 start sample-app.js

After the app is started , You will get the below resposne.

Managing PM2 Process

To get the lists of all the available pm2 command , Then Execute the below command.

pm2 -h

To list all the node applications or processes , Run the below command.

pm2 ls

To start a process ,

pm2 start sample-app.js

To delete all the instances of a particular process,

pm2 delete sample-app

Each process will have a unique id , using which we can manage a particular process , Instead of restarting entire app.

To restart all the instances of an application then , run the below command.

pm2 restart sample-app

To know more details about a particular process , Then run this command,

pm2 show 0

We can also use the below command To check the details for a process.

pm2 desc 0

To reset the restart count for a process or for all the processes,

pm2 reset all
pm2 reset 0

To start the application in cluster mode , Lets say you want to run 4 processes for a application,

pm2 start sample-app.js -i 4

To scale up an application to 10 process,

pm2 scale sample-app 10

To monitor metrics , logs , and other information for all the process , Use the below command.

pm2 monit

Press Ctrl + C , To quit from it.

To check the logs for a particular process or an application,

pm2 logs 5
pm2 logs sample-app

To stop a process or an application , Run the below command.

pm2 stop 8
pm2 stop sample-app

To flush all the logs for all the applications, Execute the below command.

pm2 flush

To enable the PM2 to automatically start on system bootup, The command will create a automatically-configured startup script for the system.

pm2 startup

To save the lists of process , So that the processes will be automatically started after the system reboot.

pm2 save

To manually restore the previously saved processes,

pm2 resurrect

If you wish to disable PM2 from starting automatically on system boot , Execute the below command.

pm2 unstartup

Accessing Nodejs App From Browser

To access the application from a web server , We need to make sure the port used by the applications are whitelisted on the firewall.

So that the client should be able to reach the Nodejs applications.

sudo ufw allow 8080

If you have used EC2 Instance to run the Nodejs applications Then Make sure you have allowed inbound rules for the respective ports.

Open the web browser and access the Nodejs application as shown below.

http://IPaddress:8080

We have deployed a sample Nodejs application and also We have leart to manage the applications using PM2.

Thanks for reading.Hope you found it helpful.

Please do check out my other blogs.