How to Install PM2 on Ubuntu 20.04 server for Node.js

Install PM2 on Ubuntu
Spread the love

Installing PM2 on Ubuntu allows for streamlined process management and monitoring for Node.js applications. In this comprehensive guide, we’ll walk through the steps from logging into an Ubuntu server via SSH or other credentials, creating a Node.js app, installing PM2, starting an app with PM2, utilizing PM2 log, monitoring features and more.

Process Manager 2, commonly known as PM2, is a versatile and robust process manager for Node.js applications. It simplifies the management of Node.js applications by providing features like process monitoring, cluster mode and zero downtime reloads. While Node.js comes with its own process manager, PM2 offers additional functionalities and enhancements that make it a preferred choice for developers and system administrators.

Best Practices

SSH Key Authentication

Utilize SSH key-based authentication for secure server access instead of relying solely on passwords. This significantly enhances security by preventing brute-force attacks.

Firewall Configuration

Always configure your firewall to allow specific ports (e.g., 3000 for Node.js apps) for necessary application access. This ensures controlled access to your server and apps.

PM2 for Process Management

Employ PM2 for managing Node.js applications. It offers automatic restarts, load balancing and monitoring, crucial for maintaining app uptime and performance.

Regular Monitoring

Make monitoring with PM2 (pm2 monit) a routine practice. Monitoring helps identify performance issues, resource consumption and potential bottlenecks.

Thing to Avoid

Running Applications as Root

Avoid running Node.js applications as the root user for security reasons. Utilize a dedicated user for running your apps.

Neglecting Firewall Rules

Always set firewall rules to control port access. Neglecting this might expose your applications and server to unauthorized access or attacks.

Ignoring Regular Backups

Failure to back up your Node.js applications and configurations regularly may lead to data loss in case of system failures or unexpected issues.

Overlooking PM2 Features

Avoid underutilizing PM2 features. Explore its capabilities thoroughly for process management, logs, scaling and more to maximize its benefits.

By adhering to these practices and avoiding common pitfalls, you can ensure a secure, efficient and reliable deployment of Node.js applications using PM2 on your Ubuntu server. Incorporating these best practices will help in maintaining a robust and well-managed application environment.

Installation Guide for PM2 on ubuntu

Logging into Ubuntu Server

To start, ensure you have SSH access to your Ubuntu server. Open a terminal or command prompt and use the following command:

ssh username@server_ip_address

Replace `username` with your server’s username and `server_ip_address` with its IP address.

Installing Node.js

Before using PM2, ensure Node.js is installed:

sudo apt update
sudo apt install nodejs
sudo apt install npm

Verify the installation:

node -v
npm -v

Creating a Node.js App

Create a simple Node.js app for demonstration purposes. Navigate to a directory where you want to create the app and run:

mkdir my-node-app
cd my-node-app
npm init -y

Create an index.js file with some basic code:

// index.js
const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello, PM2!\n');

server.listen(3000, 'localhost', () => {
  console.log('Server running at http://localhost:3000/');

Installing PM2

Install PM2 globally using npm:

sudo npm install pm2 -g

Starting an App with PM2

Start your Node.js app using PM2:

pm2 start index.js

// other way
pm2 start {command} --name "APP NAME" -- {script}

pm2 start npm --name "NODE APP" -- start

Managing Permissions and Ports

For port access, ensure firewall permissions. Open a port (e.g., 3000) for your app:

sudo ufw allow 3000

PM2 Log

Access logs with:

pm2 logs

This command displays live logs for all apps managed by PM2.

PM2 Monitoring

Monitor app performance and resource usage:

pm2 monit


PM2 offers a robust suite of features for Node.js application management, from process monitoring to logging and beyond. By following these steps, you can seamlessly integrate PM2 into your workflow, ensuring efficient and reliable Node.js app management on your Ubuntu server.

Implementing PM2 simplifies the management and monitoring of Node.js applications, enhancing their reliability and performance. With its user-friendly interface and powerful features, PM2 is an invaluable tool for developers deploying Node.js applications.

By harnessing the power of PM2, developers can streamline Node.js application management, ensuring enhanced stability, scalability and efficiency in production environments.

Remember, PM2 offers a multitude of features beyond the scope of this guide. Delve deeper into its capabilities to leverage its full potential for your Node.js applications.

If you have any further questions or encounter issues during the installation or usage of PM2, refer to the official PM2 documentation or reach out to the vibrant developer community for assistance.

Install PM2 today and witness the ease and efficiency it brings to managing Node.js applications on your Ubuntu server!


Use SSH key-based authentication for a more secure login method than passwords.

Use SSH key-based authentication for a more secure login method than passwords.

Can I change app ports with PM2?

Yes, modify the port in your Node.js app code and restart with PM2.

How to grant PM2 user permissions?

Grant specific permissions via: sudo chown -R $USER:$USER ~/.pm2

What’s the benefit of using PM2?

PM2 streamlines Node.js process management, ensuring apps remain online, facilitating easy scaling, and simplifying deployment.

Can I run multiple Node.js apps with PM2?

Absolutely! PM2 can manage multiple Node.js apps simultaneously, providing a centralized management system.

How can I restart an app with PM2?

Use pm2 restart to restart a specific app.

Spread the love

Similar Posts


Leave a Reply

Your email address will not be published. Required fields are marked *