We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Install PM2 Node.js Process Manager on RHEL 8|CentOS 8|Rocky Linux 8

Learn how to – Install PM2 Node.js Process Manager on RHEL 8|CentOS 8|Rocky Linux 8.

How do I install PM2 on RHEL 8 / CentOS 8 / Rocky Linux 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 / Rocky Linux production server.

Step 1: Install Nodejs and NPM

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

$ sudo dnf module list | grep nodejs
nodejs               10 [d]          common [d], development, minimal, s2i    Javascript runtime
nodejs               12              common [d], development, minimal, s2i    Javascript runtime
nodejs               14              common [d], development, minimal, s2i    Javascript runtime
nodejs               16              common [d], development, minimal, s2i    Javascript runtime

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

$ sudo dnf module install nodejs:16
Dependencies resolved.
======================================================================================================================================================================================================
 Package                                     Architecture                      Version                                                                     Repository                            Size
======================================================================================================================================================================================================
Installing group/module packages:
 nodejs                                      x86_64                            1:16.13.1-3.module+el8.5.0+721+4c107270                                     appstream                             12 M
 npm                                         x86_64                            1:8.1.2-1.16.13.1.3.module+el8.5.0+721+4c107270                             appstream                            1.9 M
Installing weak dependencies:
 nodejs-docs                                 noarch                            1:16.13.1-3.module+el8.5.0+721+4c107270                                     appstream                            8.7 M
 nodejs-full-i18n                            x86_64                            1:16.13.1-3.module+el8.5.0+721+4c107270                                     appstream                            7.6 M
Installing module profiles:
 nodejs/common
Enabling module streams:
 nodejs                                                                        16

Transaction Summary
======================================================================================================================================================================================================
Install  4 Packages

Total download size: 30 M
Installed size: 144 M
Is this ok [y/N]: y

For Node.js 14, it will be

sudo dnf module install nodejs:14

Wait for the installation to finish then check the version:

$ node --version
v16.13.1

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

sudo dnf -y install -y gcc-c++ make

Step 2: Install PM2 on CentOS 8 / RHEL 8 / Rocky Linux 8

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.

....................

added 182 packages, and audited 183 packages in 2m

12 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.1.2 -> 8.5.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.5.2
npm notice Run npm install -g npm@8.5.2 to update!
npm notice

PM2 should be installed in /usr/local/bin directory:

$ which pm2
/usr/local/bin/pm2

Step 3: Create a test Node.js Application

Create a project folder.

mkdir nodejs

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

cd 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 8 / CentOS 8 / Rocky Linux 8

See below examples on usage of PM2 to manage Node.js application processes.

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

See below screenshot.

Other relevant articles:

Cart

Your Cart is Empty

Back To Shop