Search
Close this search box.

How to Install PM2 on Ubuntu 20.04

Table of Contents

Smart and Easy way to Install PM2 on Ubuntu 20.04

PM2 is a process executive for Node.js applications, allowing users to manage and deploy Node.js applications quickly. It is a popular process manager, especially among developers developing applications with Node.js. This article will discuss PM2 on Ubuntu and everything you need to know about it.


What is PM2?


PM2 is a production process executive for Node.js applications, designed to be easy to use, flexible, and robust. It is a free and open-source tool that allows users to run, monitor, and manage their Node.js applications in a production environment. PM2 also has many features that make it an essential tool for developers building Node.js applications.

 

Here’s how to set up PM2 on Ubuntu

 

If you do not already have a VPSie account, you will need to create one in order to register Ubuntu systems.

 

Now open your terminal and run the following commands to update your system.

 

 

# apt-get update && apt-get upgrade -y

 

So let’s start the installation process:

 

Step 1: Install NVM and Node.JS

 

 

Install the NVM with the following command:

 

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

 

 

 

Reload your ~/.bashrc to activate NVM after installation.  

 

# source ~/.bashrc

 

 

 

Verify the version:

 

# nvm -v

 

Now install the Node.js using the following command:

 

# nvm install node

 

 

Verify the version:

 

# node -v

 

Step 2: Install PM2 Product Process Manager

 

Using NPM, you can install the latest stable version of PM2. Type the following command to install it(If sudo is causing errors, avoid it.):

 

 

# sudo npm i -g pm2

 

Step 3: Create a Nodejs Application

 

Now, let’s create a testing application(Let’s assume it has a client-side and admin-side that share the same database.):

 

 

On mainapp side:

 

# sudo mkdir -p /var/www/html/app
# sudo vim /var/www/html/app/server.js

 

And paste the following contet on that file:

 

##mainapp code
const http = require('http');

const hostname = '{your-ip}';
const port = 3000;

const server = http.createServer((req, res) => {
	res.statusCode = 200;
  	res.setHeader('Content-Type', 'text/plain');
  	res.end('Main App!\n');
});

server.listen(port, hostname, () => {
  	console.log(`Server running at http://${your-hostname}:${port}/`);
});

 

 

On Adminside:

 

# sudo mkdir -p /var/www/html/adminside
# sudo vim /var/www/html/adminside/server.js

 

Paste the following content:

 

##adminside code
const http = require('http');

const hostname = 'your-ip';
const port = 3001;

const server = http.createServer((req, res) => {
	res.statusCode = 200;
  	res.setHeader('Content-Type', 'text/plain');
  	res.end('Admin Side!\n');
});

server.listen(port, hostname, () => {
  	console.log(`Server running at http://${hostname}:${port}/`);
});

 

Now you can start your node applications using following commands:

 

# pm2 start /var/www/html/app/server.js - we are giving starting this for example here.
# pm2 start /var/www/html/adminside/server.js
Tutorial Image of Installing PM2 on Ubuntu

Now you need to open the following ports on your system firewall in order to access all of your Node applications from a remote web browser. Run the following command to do so:

 

 

# sudo ufw allow 3000/tcp - mainapp
# sudo ufw allow 3001/tcp - adminside
# sudo ufw reload

 

Now access your apps from a web browser at https://{server-ip}:3000 or https://{server-ip}:3001.

Some of the code is based on the original Tecmint article. For details on how to manage apps with PM2, check the article. We have now installed PM2, created apps, and started them.

 

Why use PM2?

There are several reasons why developers prefer to use PM2 for managing their Node.js applications in a production environment. Some of these reasons are:

  1. Automatic restart: PM2 has an automatic restart feature that restarts the application when it crashes or stops running.
  2. Easy deployment: PM2 makes it easy to deploy Node.js applications, allowing developers to start and stop their applications with a single command quickly.
  3. Process monitoring: PM2 monitors the processes running on the server, allowing developers to track and monitor the status of their applications in real time.
  4. Load balancing: PM2 comes with a built-in load balancer that distributes incoming requests to multiple instances of the application, improving the performance and availability of the application.
  5. Logs management: PM2 manages application logs, making it easy for developers to analyze and troubleshoot issues with their applications.

 

Features of PM2

PM2 has many features that make it an essential tool for developers building Node.js applications. Some of these features are:

  1. Process management: PM2 allows developers to manage multiple processes for their Node.js applications, allowing them to start, stop, and restart their applications quickly.
  2. Monitoring: PM2 monitors the processes running on the server, providing real-time information about the status of the application.
  3. Load balancing: PM2 comes with a built-in load balancer that distributes incoming requests to multiple instances of the application, improving the performance and availability of the application.
  4. Automatic restart: PM2 automatically restarts the application when it crashes or stops running.
  5. Logs management: PM2 manages application logs, making it easy for developers to analyze and troubleshoot issues with their applications.
  6. Zero downtime deployment: PM2 allows developers to deploy new versions of their applications with zero downtime, ensuring that the application is always available to users.

 

Advantages of PM2

There are several advantages of using PM2 to manage Node.js applications in a production environment. Some of these advantages are:

  1. Easy deployment: PM2 makes it easy to deploy Node.js applications, allowing developers to start and stop their applications with a single command quickly.
  2. Automatic restart: PM2 automatically restarts the application when it crashes or stops running, ensuring it is always available to users.
  3. Load balancing: PM2 comes with a built-in load balancer that distributes incoming requests to multiple instances of the application, improving the performance and availability of the application.
  4. Process monitoring: PM2 monitors the processes running on the server, allowing developers to track and monitor the status of their applications in real time.
  5. Logs management: PM2 manages application logs, making it easy for developers to analyze and troubleshoot issues with their applications.

 

Disadvantages of PM2

Despite the many advantages of using PM2 to manage Node.js applications in a production environment, there are some disadvantages that developers should be aware of. Some of these disadvantages are:

  1. Learning curve: PM2 can have a steep learning curve.
  2. Limited to Node.js: PM2 is designed explicitly for Node.js applications, which means it cannot be used to manage applications built with other programming languages.
  3. Resource-intensive: PM2 can consume many resources on the server, primarily if the application handles many requests.
  4. Configuration complexity: PM2 configuration can be complex, especially for developers who are new to the tool.
  5. Limited control over server configuration: PM2 is designed to manage Node.js processes on a server, which means it does not provide control over the server configuration.

Conclusion

PM2 is a powerful tool for managing Node.js applications in a production environment. It has many features that make it easy to deploy, monitor, and manage Node.js applications. However, it also has disadvantages, such as a steep learning curve, resource consumption, and limited control over server configuration. Despite these disadvantages, PM2 remains a popular process manager among Node.js developers, and it is worth considering if you are building Node.js applications in a production environment.

 

I hope this article was helpful to you. 

You can also read Install Uptime Kuma on Ubuntu 22.04 LTS 

Install Rocket.chat on VPSie

 

FAQ

PM2 is a process manager for Node.js applications that enables you to manage, monitor, and scale your applications easily.

PM2 helps you keep your Node.js applications running in the background, and it ensures that your application is always available to your users.

You can install PM2 on Ubuntu 20.04 by following these simple steps:

  • Install NVM and Node.js
  • Install PM2 using NPM
  • Create a Node.js application
  • Start your application with PM2

You can open ports on your system firewall by using the command “sudo ufw allow [port]/tcp” (replace “[port]” with the port number of your application).

You can access your Node.js applications from a web browser by using the URL “https://{server-ip}:{port}”.

Make a Comment
Share on
Facebook
Twitter
LinkedIn
Print
VPSie Cloud service

Fast and Secure Cloud VPS Service

Try FREE
For a month

The First 1 orders gets free discount today! Try Sign up on VPSie to get a chance to get the discount.