(Last Updated On: March 20, 2019)

How do I install PM2 on RHEL / CentOS 8?. PM2 is an open source production process manager for Node.js applications with a built-in load balancer. It is designed to be efficient and to enable you run applications in cluster mode for easy HA. With PM2, you can keep applications alive forever, reload them without causing downtime and easily perform other Node.js common system admin tasks.

PM2 supports all major versions of Node.js starting from Node.js 4.X. In addition, it works on Linux, macOS, and Windows operating systems. This article will explain how to install and use PM2 to run Applications in RHEL / CentOS production server.

Step 1: Install Nodejs and NPM

We need Node.js and NPM installed before we can install PM2. RHEL / CentOS ships with both Node.js 10 & 8. Choose the version you want to install it with yum or dnf package manager.

$ sudo yum module list | grep nodejs
nodejs 10 [d] development, minimal, s2i, default [d] Javascript runtime
nodejs 8 development, minimal, s2i, default [d] Javascript runtime

The default version installed is Nodejs 10 but you can explicitly specify the version of Node.js to install.

sudo yum module install nodejs:10

For Node.js 8, it will be

sudo yum module install nodejs:8

Wait for the installation to finish then check the version:

$ node --version
v10.11.0

You may also need to install Development Tools for building Node software.

sudo yum install -y gcc-c++ make

Step 2: Install PM2

Once Node.js is installed, proceed to install PM2 process manager. The option -g is used for global install.

sudo npm i -g pm2 

Sample installation output.

....................
/usr/bin/pm2 -> /usr/lib/node_modules/pm2/bin/pm2
/usr/bin/pm2-dev -> /usr/lib/node_modules/pm2/bin/pm2-dev
/usr/bin/pm2-docker -> /usr/lib/node_modules/pm2/bin/pm2-docker
/usr/bin/pm2-runtime -> /usr/lib/node_modules/pm2/bin/pm2-runtime
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/pm2/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
[email protected]
updated 1 package in 16.992s

PM2 should be installed to /usr/bin path.

$ which pm2
/usr/bin/pm2

Step 3: Create a test Node.js Application

Create a project folder.

mkdir hello-world-nodejs

Change to Project folder and create app.js file with below data.

cd hello-world-nodejs
vi app.js

Add

const http = require('http');

http.createServer(function(request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end("Hello, World!\n");
}).listen(process.env.PORT);

console.log('App is running...');

Step 4: How to Use PM2 on RHEL / CentOS 8

Start the Application with PM2.

$ pm2 start app.js 

                        -------------

__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
 _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
  _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
   _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
    _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
     _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
      _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
       _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
        _\///______________\///______________\///__\///////////////__


                          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/node-hello-world/app.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬─────────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name │ id │ version │ mode │ pid  │ status │ restart │ uptime │ cpu │ mem       │ user │ watching │
├──────────┼────┼─────────┼──────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ app      │ 0  │ N/A     │ fork │ 4618 │ online │ 0       │ 0s     │ 0%  │ 31.2 MB   │ root │ disabled │
└──────────┴────┴─────────┴──────┴──────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘

List running Applications

$ pm2 list
┌──────────┬────┬─────────┬──────┬──────┬────────┬─────────┬────────┬──────┬───────────┬──────┬──────────┐
│ App name │ id │ version │ mode │ pid  │ status │ restart │ uptime │ cpu  │ mem       │ user │ watching │
├──────────┼────┼─────────┼──────┼──────┼────────┼─────────┼────────┼──────┼───────────┼──────┼──────────┤
│ app      │ 0  │ N/A     │ fork │ 4618 │ online │ 0       │ 17m    │ 0.4% │ 47.5 MB   │ root │ disabled │
└──────────┴────┴─────────┴──────┴──────┴────────┴─────────┴────────┴──────┴───────────┴──────┴──────────┘

To monitor target process, use

$ sudo pm2 monitor

View Node.js Process information.

First get process ID

$ sudo pm2 list

Then use the process ID to pull more information.

$ sudo pm2 show <ProcessID>

See example below.

Set Application to start at boot.

Use pm2 startup command.

$ sudo pm2 startup
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target
[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/sbin:/bin:/usr/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill
[Install]
WantedBy=multi-user.target
Target path
/etc/systemd/system/pm2-root.service
Command list
[ '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 disable run:

sudo pm2 unstartup

View App Logs.

$ sudo pm2 logs
[TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
/root/.pm2/pm2.log last 15 lines:
PM2 | 2019-03-20T16:28:15: PM2 log: Time : Wed Mar 20 2019 16:28:15 GMT+0300 (East Africa Time)
PM2 | 2019-03-20T16:28:15: PM2 log: PM2 version : 3.4.0
PM2 | 2019-03-20T16:28:15: PM2 log: Node.js version : 10.11.0
PM2 | 2019-03-20T16:28:15: PM2 log: Current arch : x64
PM2 | 2019-03-20T16:28:15: PM2 log: PM2 home : /root/.pm2
PM2 | 2019-03-20T16:28:15: PM2 log: PM2 PID file : /root/.pm2/pm2.pid
PM2 | 2019-03-20T16:28:15: PM2 log: RPC socket file : /root/.pm2/rpc.sock
PM2 | 2019-03-20T16:28:15: PM2 log: BUS socket file : /root/.pm2/pub.sock
PM2 | 2019-03-20T16:28:15: PM2 log: Application log path : /root/.pm2/logs
PM2 | 2019-03-20T16:28:15: PM2 log: Process dump file : /root/.pm2/dump.pm2
PM2 | 2019-03-20T16:28:15: PM2 log: Concurrent actions : 2
PM2 | 2019-03-20T16:28:15: PM2 log: SIGTERM timeout : 1600
PM2 | 2019-03-20T16:28:15: PM2 log: ===============================================================================
PM2 | 2019-03-20T16:28:15: PM2 log: App [app:0] starting in -fork mode-
PM2 | 2019-03-20T16:28:15: PM2 log: App [app:0] online
/root/.pm2/logs/app-error.log last 15 lines:
/root/.pm2/logs/app-out.log last 15 lines:
0|app | App is running…
0|app | App is running…
0|app | App is running…

Update PM2

Use pm2 update command to pull PM2 updates.

sudo pm2 update

Other relevant articles:

Install GCC and Development Tools on RHEL / CentOS 8

Install MyWebSQL on RHEL / CentOS 8

How to Install Open Web Analytics on RHEL / CentOS 8

How to Install Scala on RHEL / CentOS 8