PM2, also called "Process Management Module", is a process manager used for managing Node.js applications. It is free, open-source and, has an in-built load balancer. It will automatically restart the Node.js server when it a process goes down. It's cross-platform and can be run on Windows, Linux and, macOS. It allows you to run the Node.js app in cluster mode without making any changes in the code.
In this guide, we'll show you how to run and manage Node.js applications using PM2.
Prerequisites
- A server running on Ubuntu 20.04.
- A root password configured on the server.
Install Node.js
Before starting, Node.js must be installed on your server. First, add the Node source repository with the following command:
# curl -sL https://deb.nodesource.com/setup_14.x | bash -
Next, install Node.js with the following command:
# apt-get install nodejs -y
Once the installation is completed, verify the Node.js version using the command below:
# node --version
You should see the following output:
# v14.17.1
Create a Sample Node.js Application
For the purpose of this tutorial, you will need to create a sample Node.js application.
First, create a directory for your app with the following command:
# mkdir myapp
Next, create a hello.js application with the following command:
# nano myapp/hello.js
Add the following lines:
const http = require('http');
const hostname = 'your-server-ip';
const port = 8000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('This is My First Nodejs App!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Remember to change 'your-server-ip' to the Public IP of the server. Save and close the file when you are done.
You should also open port 8000 on your server's Firewall.
Install PM2
You can install PM2 easily using the NPM command as shown below:
# npm i -g pm2
Once the PM2 is installed, start your application using PM2:
# pm2 start myapp/hello.js
You should see the following output:
__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
_\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
_\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
_\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
_\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
_\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
_\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
_\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
_\///______________\///______________\///__\///////////////__
Runtime Edition
PM2 is a Production Process Manager for Node.js applications
with a built-in Load Balancer.
Start and Daemonize any application:
$ pm2 start app.js
Load Balance 4 instances of api.js:
$ pm2 start api.js -i 4
Monitor in production:
$ pm2 monitor
Make pm2 auto-boot at server restart:
$ pm2 startup
To go further checkout:
http://pm2.io/
-------------
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /root/myapp/hello.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ hello │ default │ N/A │ fork │ 2788 │ 0s │ 0 │ online │ 0% │ 28.0mb │ root │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
How to Use PM2
To list all your running Node.js applications, run the following command:
# pm2 list
You should get the following output:
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ hello │ default │ N/A │ fork │ 2788 │ 26s │ 0 │ online │ 0% │ 37.5mb │ root │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
To restart the application, run the following command:
# pm2 restart myapp/hello.js
To start your Node.js application in cluster mode, run the following command:
# pm2 start myapp/hello.js -i 2
You should see the following output:
[PM2] Starting /root/myapp/hello.js in cluster_mode (2 instances)
[PM2] Done.
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ hello │ default │ N/A │ cluster │ 3228 │ 0s │ 0 │ online │ 0% │ 35.6mb │ root │ disabled │
│ 1 │ hello │ default │ N/A │ cluster │ 3235 │ 0s │ 0 │ online │ 0% │ 30.0mb │ root │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
To monitor your application, run the following command:
# pm2 monit
You should see the following screen:
To check your application logs, run the following command:
# pm2 logs
You should see the following output:
/root/.pm2/logs/hello-error-0.log last 15 lines:
/root/.pm2/logs/hello-out-0.log last 15 lines:
0|hello | Server running at http://your-server-ip:8000/
/root/.pm2/logs/hello-out-1.log last 15 lines:
1|hello | Server running at http://your-server-ip:8000/
To flush all logs, run the following command:
# pm2 flush
To display detailed information of your application, run the following command:
# pm2 show 0
You should see the following screen:
Manage Node.js Application with PM2
To start your Node.js application at system reboot, run the following command:
# pm2 startup
You should see the following output:
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service → /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup systemd
To remove the process to start at reboot, run the following command:
# pm2 unstartup
To save the process to start at system reboot, run the following command:
# pm2 save
To update the PM2, run the following command:
# pm2 update
You should see the following output:
Be sure to have the latest version by doing `npm install pm2@latest -g` before doing this procedure.
[PM2] Applying action deleteProcessId on app [all](ids: [ 0, 1 ])
[PM2] [hello](0) ✓
[PM2] [hello](1) ✓
[PM2] [v] All Applications Stopped
[PM2] [v] PM2 Daemon Stopped
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] Restoring processes located in /root/.pm2/dump.pm2
[PM2] Process /root/myapp/hello.js restored
[PM2] Process /root/myapp/hello.js restored
>>>>>>>>>> PM2 updated
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ hello │ default │ N/A │ cluster │ 3445 │ 0s │ 0 │ online │ 0% │ 37.9mb │ root │ disabled │
│ 1 │ hello │ default │ N/A │ cluster │ 3446 │ 0s │ 0 │ online │ 0% │ 37.9mb │ root │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
To stop all Node.js processes, run the following command:
# pm2 stop all
To stop the Node.js process with id 0, run the following command:
# pm2 stop 0
To restart all Node.js application processes, run the following command:
# pm2 restart all
To delete all processes, run the following command:
# pm2 delete all
To delete a process with id 1, run the following command:
# pm2 delete 1
Access Node.js Application
Now, open your web browser and access your Node.js application using the URL http://your-server-ip:8000. You should see your application in the following screen:
Conclusion
In the above guide, you've learned how to install and use PM2 to manage the Node.js application.
We hope that PM2 will help you to manage your Node.js applications easily!