Atlantic.Net Blog

How to Monitor Node.js Applications Using PM2 Web Dashboard

PM2 is a free and open-source process manager for Node.js applications. It is used to monitor the Node.js application and keep it alive 24/7. It allows you to start, stop, and restart the Node.js application via the command line.

PM2 also offers a web-based dashboard that allows you to monitor your Node.js application in real time via a web browser. It is used in a production environment to manage multiple Node.js applications. It offers a rich set of features including real-time logs, email and slack notifications, custom metrics, issues and exception tracking, deployment reporting, and more.

In this post, we will show you how to monitor the Node.js application using the PM2 dashboard.

Prerequisites

  • A fresh Arch Linux server on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Arch Linux as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Step 2 – Configure Repository

By default, the default repository is outdated in Arch Linux, so you will need to modify the default mirror list. You can do it by editing the mirror list configuration file:

nano  /etc/pacman.d/mirrorlist

Remove all lines and add the following lines:

## Score: 0.7, United States
Server = http://mirror.us.leaseweb.net/archlinux/$repo/os/$arch
## Score: 0.8, United States
Server = http://lug.mtu.edu/archlinux/$repo/os/$arch
Server = http://mirror.nl.leaseweb.net/archlinux/$repo/os/$arch
## Score: 0.9, United Kingdom
Server = http://mirror.bytemark.co.uk/archlinux/$repo/os/$arch
## Score: 1.5, United Kingdom
Server = http://mirrors.manchester.m247.com/arch-linux/$repo/os/$arch
Server = http://archlinux.dcc.fc.up.pt/$repo/os/$arch
## Score: 6.6, United States
Server = http://mirror.cs.pitt.edu/archlinux/$repo/os/$arch
## Score: 6.7, United States
Server = http://mirrors.acm.wpi.edu/archlinux/$repo/os/$arch
## Score: 6.8, United States
Server = http://ftp.osuosl.org/pub/archlinux/$repo/os/$arch
## Score: 7.1, India
Server = http://mirror.cse.iitk.ac.in/archlinux/$repo/os/$arch
## Score: 10.1, United States
Server = http://mirrors.xmission.com/archlinux/$repo/os/$arch

Save and close the file, then update all the package indexes with the following command:

pacman -Syu

Step 3 – Install Node.js and NPM

By default, both packages are available in the Arch Linux default repository. You can install them with the following command.

pacman -S nodejs npm

After the successful installation, you can verify the Node.js version using the following command.

node --version

You should get the following output.

v19.7.0

Step 4 – Create a Sample Node.js App

Next, you will need to create a sample Node.js application so we can monitor it via PM2.

First, create a project directory for the Node.js application.

mkdir project

Next, change the directory to the Project and create an app.js file.

cd project
nano app.js

Add the following code.

const http = require('http');

const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Your Nodejs App is Working!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Save and close the when you are done.

Next, install the PM2 using the NPM command.

npm install pm2 -g

Then, start the Node.js application using the following command.

pm2 start app.js

You will get 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/project/app.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name   │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ app    │ default     │ N/A     │ fork    │ 65084    │ 0s     │ 0    │ online    │ 0%       │ 14.0mb   │ root     │ disabled │
└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

By default, the Node.js application listens on port 3000. You can check it with the following command.

ss -antpl | grep node

You should see the following output.

LISTEN 0      0              [::1]:3000            *:*    users:(("node /root/proj",pid=65084,fd=20)) 

Step 5 – Monitor Node.js Application Using Using PM2 Dashboard

PM2 provides an advance web-based dashboard that monitors your application in real time via a web browser.

First, go to the PM2 website using the URL http://app.pm2.io/.
PM2 login page
Then, sign up and log in to the PM2 web dashboard. You should see the following screen.
PM2 create bucket page
Provide your bucket name and click on the Create button. You should see the following screen.
PM2 dashboard page
From the above screen, copy and run the following command on your terminal.

pm2 link 30ew4cpwzxopx3v tzc9tp9s2oc0wkf

This will add your Node.js application to the PM2 dashboard.

[PM2 I/O] Using: Public key: tzc9tp9s2oc0wkf | Private key: 30ew4cpwzxopx3v | Machine name: archlinux-74b9
[+] PM2+ activated!

Now, refresh your web browser. You should see the new Node.js application added to the dashboard.
Node.js application added to dashboard

Step 6 – Monitor Node.js Server Resources Using PM2 Monit

PM2 also provides a pm2-server-monit module that allows you to monitor key resources of the Node.js applications.

First, install the pm2-server-monit module with the following command.

pm2 install pm2-server-monit

You will get the following output.

⇆ PM2+ activated | Instance Name: archlinux-74b9 | Dash: https://app.pm2.io/#/r/tzc9tp9s2oc0wkf
┌─────┬─────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name                │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼─────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ app                 │ default     │ N/A     │ fork    │ 65084    │ 13m    │ 0    │ online    │ 0%       │ 52.9mb   │ root     │ disabled │
└─────┴─────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
Module
┌────┬──────────────────────────────┬───────────────┬──────────┬──────────┬──────┬──────────┬──────────┬──────────┐
│ id │ module                       │ version       │ pid      │ status   │ ↺    │ cpu      │ mem      │ user     │
├────┼──────────────────────────────┼───────────────┼──────────┼──────────┼──────┼──────────┼──────────┼──────────┤
│ 1  │ pm2-server-monit             │ 3.0.0         │ 65369    │ online   │ 0    │ 0%       │ 8.8mb    │ root     │
└────┴──────────────────────────────┴───────────────┴──────────┴──────────┴──────┴──────────┴──────────┴──────────┘

Next, refresh the PM2 dashboard. You should see Node.js resource usage on the following screen.
Node.js application resource usage
If you want to remove your added server from the PM2 dashboard, run the following command.

pm2 unlink

You will get the following output.

[PM2 I/O] Permanently disable agent...
[PM2 I/O] Agent interaction ended

Conclusion

In this post, we explained how to monitor the Node.js applications using the PM2 web dashboard. You can now try the PM2 dashboard in the production environment to monitor the Node.js application in real-time. Try the PM2 monitoring server on dedicated server hosting from Atlantic.Net!

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year