Express.js / Express.js Deployment and Scaling

Using PM2 for Managing Processes

This tutorial covers how to use PM2, a powerful process manager for Node.js applications. You will learn to install and use PM2 to manage your Express.js application processes and…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers deploying, scaling, and monitoring Express applications.

1. Introduction

In this tutorial, we are going to learn about PM2, a robust, production-grade process manager for Node.js applications. PM2 empowers you to keep your site and apps available forever. It helps you manage your application's services, logs, exceptions, and even monitor the resource usage in real-time.

By the end of this tutorial, you will be able to:
- Install PM2 on your machine
- Use PM2 to manage your Node.js applications
- Use PM2 to ensure your apps are always running

Prerequisites:
- Basic understanding of Node.js and Express.js
- Node.js and NPM installed on your system

2. Step-by-Step Guide

2.1 Installing PM2
The first step is to install PM2. You can install PM2 globally on your system using NPM (Node Package Manager). Run the following command in your terminal:

npm install pm2 -g

2.2 Starting an Application with PM2
To start an application with PM2, navigate to the directory of your application and run the following command:

pm2 start app.js

Replace app.js with the entry point of your application.

2.3 Managing Processes
You can list all processes managed by PM2 using the pm2 list command. To stop a process, use pm2 stop <id> where <id> is the id of the process. Use pm2 restart <id> to restart a process.

2.4 Keeping Applications Running
PM2 can keep your applications running even after a system reboot. You can achieve this by using the pm2 startup command.

3. Code Examples

Let's assume we have an Express.js application. The entry point of our application is app.js.

3.1 Starting the Application

pm2 start app.js

This will start the application under PM2's supervision.

3.2 Listing Managed Processes

pm2 list

This will list all processes managed by PM2.

3.3 Stopping a process
If the id of our process is 0, we can stop it using:

pm2 stop 0

This will stop the process with the id 0.

3.4 Restarting a Process

pm2 restart 0

This will restart the process with the id 0.

3.5 Keeping the Application Running

pm2 startup

This command will generate a command that you need to run with superuser privileges. This will make sure your application starts on system reboot.

4. Summary

In this tutorial, we learned how to use PM2 to manage our Node.js applications. We learned how to start, stop, and restart our applications using PM2. We also learned how to make sure our applications keep running even after a system reboot.

Next, you can learn more about the other features of PM2 like clustering, monitoring, etc. You can refer to the official PM2 documentation for more details.

5. Practice Exercises

5.1 Exercise 1
Create a basic Express.js application and use PM2 to start the application.

5.2 Exercise 2
Use PM2 to list all running applications, stop the application you started in Exercise 1, and then restart it.

5.3 Exercise 3
Set up your application to start on system reboot.

Solutions

5.1 Solution to Exercise 1
You can create a basic Express.js application using the Express Generator. You can then navigate to the directory of your application and use pm2 start ./bin/www to start your application.

5.2 Solution to Exercise 2
You can use pm2 list to list all running applications. You can stop your application using pm2 stop <id>. You can restart your application using pm2 restart <id>.

5.3 Solution to Exercise 3
You can use pm2 startup to generate a command. You need to run this command with superuser privileges. This will set your application to start on system reboot.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

Watermark Generator

Add watermarks to images easily.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help