Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Monitor Node.js Applications Using PM2 Web Dashboard

Node.js is an open-source JavaScript runtime environment built on Chrome’s V8. It is mainly used to execute JavaScript code outside a web browser. It uses an asynchronous, event-driven model, making it perfect for data-intensive applications.

After deploying a Node.js application, you may need a tool to monitor your application. This is where PM2 can help. PM2 is a popular process manager used for managing and monitoring Node.js applications. It allows you to monitor Node.js applications via CLI as well as a web-based dashboard.

In this post, we will show you how to install and use PM2 to monitor Node.js applications.

In This Article

  • Install Node.js
  • Create a Simple Node.js Application
  • Install and Use PM2 to Manage Node.js Application
  • Monitor Node.js Application Using PM2 Dashboard
  • Monitor Node.js Using pm2-server-monit

Step 1 – Install Node.js

First, install all the Node.js dependencies using the following command.

dnf install -y gcc-c++ make

Then, add the Node source repo to get the latest Node.js LTS version.

curl -sL https://rpm.nodesource.com/setup_18.x | bash -

Next, install Node.js with the following command.

dnf install nodejs git -y

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

node -v

Output:

v18.11.0

Step 2 – Create a Simple Node.js Application

Next, you will need a Node.js application that you want to monitor using PM2. First, create a directory for your application.

mkdir app

Next, create an application file.

cd app
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 file when you are done.

Step 3 – Install and Use PM2 to Manage Node.js Application

First, install PM2 using the following command.

npm install pm2 -g

Next, start the Node.js application using PM2.

pm2 start app.js

Output.

[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /root/app/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    │ 55173    │ 0s     │ 0    │ online    │ 0%       │ 33.0mb   │ root     │ disabled │
└────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

You can now list your application using the following command.

pm2 list

Output

┌────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name   │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ app    │ default     │ N/A     │ fork    │ 55173    │ 16s    │ 0    │ online    │ 0%       │ 51.2mb   │ root     │ disabled │
└────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

To stop and delete the application, run the following command.

pm2 stop app.js
pm2 delete app.js

To reload the application after making changes, run the following command.

pm2 reload app.js

To add the application at system startup, run the following command.

pm2 startup

To check the application log, run the following command.

pm2 logs

Output.

PM2        | 2023-07-30T10:23:06: PM2 log: BUS socket file      : /root/.pm2/pub.sock
PM2        | 2023-07-30T10:23:06: PM2 log: Application log path : /root/.pm2/logs
PM2        | 2023-07-30T10:23:06: PM2 log: Worker Interval      : 30000
PM2        | 2023-07-30T10:23:06: PM2 log: Process dump file    : /root/.pm2/dump.pm2
PM2        | 2023-07-30T10:23:06: PM2 log: Concurrent actions   : 2
PM2        | 2023-07-30T10:23:06: PM2 log: SIGTERM timeout      : 1600
PM2        | 2023-07-30T10:23:06: PM2 log: ===============================================================================
PM2        | 2023-07-30T10:23:06: PM2 log: App [app:0] starting in -fork mode-
PM2        | 2023-07-30T10:23:06: PM2 log: App [app:0] online
PM2        | 2023-07-30T10:23:36: PM2 log: Process 0 in a stopped status, starting it
PM2        | 2023-07-30T10:23:36: PM2 log: Stopping app:app id:0
PM2        | 2023-07-30T10:23:36: PM2 log: App [app:0] exited with code [0] via signal [SIGINT]
PM2        | 2023-07-30T10:23:37: PM2 log: pid=55173 msg=process killed
PM2        | 2023-07-30T10:23:37: PM2 log: App [app:0] starting in -fork mode-
PM2        | 2023-07-30T10:23:37: 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      | Server running at http://localhost:3000/
0|app      | Server running at http://localhost:3000/

Step 4 – Monitor Node.js Application Using PM2 Dashboard

PM2 also provides a web-based dashboard that allows you to monitor and diagnose Node.js applications in real-time.

To use the PM2 dashboard, go to https://app.pm2.io, then sign up as shown in the following screenshot.

After the successful login, copy the pm2 link command from the above screenshot and run it on your server.

pm2 link 30ew4cpwzxopx3v tzc9tp9s2oc0wkf

You will see the following output:

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

Now, refresh the PM2 dashboard page. You should see that your server is connected and showing a list of all your Nodejs applications in expanded mode.

Step 5 – Monitor Node.js Using pm2-server-monit

You can also use the pm2-server-monit module to monitor your application’s CPU, disk, and memory usage, network speed, and other key aspects of your server.

You can install the pm2-server-monit module with the following command.

pm2 install pm2-server-monit

Output:

⇆ PM2+ activated | Instance Name: fedora-e930 | 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    │ 55202    │ 5m     │ 1    │ online    │ 0%       │ 52.4mb   │ root     │ disabled │
└────┴─────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
Module
┌────┬──────────────────────────────┬───────────────┬──────────┬──────────┬──────┬──────────┬──────────┬──────────┐
│ id │ module                       │ version       │ pid      │ status   │ ↺    │ cpu      │ mem      │ user     │
├────┼──────────────────────────────┼───────────────┼──────────┼──────────┼──────┼──────────┼──────────┼──────────┤
│ 1  │ pm2-server-monit             │ 3.0.0         │ 55367    │ online   │ 0    │ 0%       │ 26.9mb   │ root     │
└────┴──────────────────────────────┴───────────────┴──────────┴──────────┴──────┴──────────┴──────────┴──────────┘

Once installed, go back to your PM2 dashboard. You should see your server’s key metrics on the following screen.

If you want to remove your server from the PM2 dashboard, run the following command:

pm2 unlink

Output:

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

Conclusion

In this post, we showed you how to install Node.js, create a Node.js application, then manage it via the command line. Then, we explained how to install the PM2 dashboard and monitor your application via a web-based dashboard. You can now deploy the Node.js application on virtual private server hosting from Atlantic.Net! and start monitoring it using the PM2 dashboard.

The post How to Monitor Node.js Applications Using PM2 Web Dashboard appeared first on Atlantic.Net.



This post first appeared on The Atlantic.Net, please read the originial post: here

Share the post

How to Monitor Node.js Applications Using PM2 Web Dashboard

×

Subscribe to The Atlantic.net

Get updates delivered right to your inbox!

Thank you for your subscription

×