Blog
This blog explains how PM2, a process manager for Node.js, helps with continuous operation, automatic restarts, centralized logging, and load balancing. It provides a guide on installing PM2 on an AWS EC2 instance and managing processes using PM2 commands. PM2 improves Node.js app deployment, performance, and reliability.
Managing and running Node.js without interruption can be challenging; that’s where PM2 comes in. PM2 excels as a premier process manager for Node.js applications that helps you start, stop, and restart your app if it crashes. The tool is designed to simplify the deployment, management, and monitoring of Node.js apps. This guide outlines a step-by-step process for installing PM2, key advantages, and a usage walkthrough for Node.js on an AWS EC2 instance.
PM2 is a popular process manager for Node.js applications. It helps programmers manage, monitor, and keep Node.js processes running in the background, especially in the case of the production environment. It is a handy tool that guarantees smooth operation, effective handling of multiple applications, centralized logging, load balancing, and a resilient ecosystem.
It lets you start, stop, restart, and delete processes easily, and also enables you to manage multiple apps simultaneously.
The built-in load balancing feature distributes incoming traffic across multiple instances of an application, improving scalability and performance.
PM2 offers logging and monitoring tools to view logs, monitor CPU and memory usage, and performance metrics, helping you track and debug issues.
The feature enables restarting apps when they crash or encounter any failure, ensuring uninterrupted service and reducing disruption for the end user.
This PM2 feature generates startup scripts to ensure the app automatically restarts on server reboots.
PM2 supports ecosystem.config.js (preferred), JSON, or YAML files for managing application settings.
PM2 is widely used in production environments because it simplifies the management of Node.js applications. Here is why developers use PM2:
Overall, PM2 can help you streamline the deployment and management of your Node.js applications, improve their performance and reliability, and simplify your development workflow.
To install PM2 with Node.js on an AWS EC2 instance, follow these steps. Note that instructions vary depending on the EC2 instance’s operating system (e.g., Amazon Linux or Ubuntu).
Use SSH to connect to your EC2 instance. The default user depends on the OS:
Example for Amazon Linux:
ssh -i your-key.pem ec2-user@your-ec2-instance
Replace your-key.pem with your key file and your-ec2-instance with the instance’s public IP or DNS.
Install a current Long-Term Support (LTS) version of Node.js (e.g., Node.js 20). Instructions vary by OS.
sudo yum update -y
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo yum install -y nodejs
sudo apt-get update
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
Verify the installation:
node -v
npm -v
Install PM2 globally using npm:
sudo npm install pm2 -g
Verify the installation:
pm2 -v
Navigate to your application directory and start your Node.js app with PM2:
cd /path/to/your/app
pm2 start app.js
Replace app.js with your main application file.
To enable load balancing with cluster mode:
pm2 start app.js -i max
This runs multiple instances based on available CPU cores.
Ensure your application restarts after server reboots:
pm2 startup
Copy and run the generated command to configure startup scripts.
Save the current PM2 process list to persist across reboots:
pm2 save
Your Node.js application is now managed by PM2 on your EC2 instance.
To list all the processes inside the PM2 manager, you can use the following command:
pm2 list
This command will display a table containing information about all the processes currently managed by PM2, including the process name, process ID (PID), status, and other details.
pm2 list --sort memory
pm2 list --filter app
These options help you quickly find the necessary information and manage your processes more efficiently with PM2.
The table below showcases some basic PM2 commands that will easily help you manage Node.js apps.
Command | Description |
---|---|
pm2 start app.js | Start an application |
pm2 list | List all managed processes |
pm2 stop app_name | Stop a running app by name or ID |
pm2 restart app_name | Restart a running app |
pm2 delete app_name | Stop and remove an app |
pm2 logs | Display logs for all apps |
pm2 monit | Monitor processes via a dashboard |
pm2 save | Save the current process list |
While PM2 is designed for Node.js, it can manage other processes using custom interpreters. For example, to run a Python script:
pm2 start script.py --interpreter python3
For advanced setups, use an ecosystem.config.js file to manage multiple applications and settings. Example:
module.exports = {
apps: [
{
name: 'my-app',
script: 'app.js',
instances: 'max',
exec_mode: 'cluster',
Discover the power of PM2 for Node.js applications – from automatic restarts and code reloads to managing multiple applications, centralized logging, load balancing, and integration with popular tools. Follow the guide to install PM2 on AWS EC2, streamline deployment, and enhance the performance, reliability, and development workflow of your Node.js applications.
Hire Node.js developers with us to launch your app with minimal downtime. Our Node.js experts keep up with the latest advancements and follow best practices to build secure, scalable, and advanced solutions for your needs.
One-stop solution for next-gen tech.