How to install and configure PM2 on Linux servers



Deployment is one of the most crucial and successful stages in software development. An appropriate deployment strategy is essential to deliver the best experience to your users while effectively using your services. However, this process also brings its share of challenges.

Node.js, the open source JavaScript runtime environment, is a popular choice for building your application’s core infrastructure by allowing you to run JavaScript outside of web browsers. But what if your Node.js application crashes in production? Find out how you can avoid such scenarios in this article.

What is PM2 and why do you need it?

PM2 is a daemon process manager that allows you to keep Node.js applications alive forever, reload them without downtime, manage application logging, monitoring and clustering.

Imagine that your Node.js web server crashes due to a runtime error. Without using something like PM2, you will have to manually track the status of your app periodically and restart it whenever it crashes. PM2 helps you automate this process and keeps your app online 24/7, even if something goes wrong.

Installing PM2 on Linux Servers

Before you can install PM2, make sure that Node.js and NPM are installed on your Linux server to download the PM2 package from the Node Package Manager repositories.

Once Node.js is up and running on your server, install PM2 by running this command:

npm install -g pm2

Go out:

Installing PM2 on Linux using NPM

The -g flag in the above command means Global and allows you to use PM2 in all your Node.js applications. You may need to add sudo at the start of the order, depending on the permissions available to your user account.

Run node applications with PM2

After the installation process is complete, navigate to the directory where your Node.js application resides using the cd command. In the project directory, you can run a JavaScript file using PM2 with the command below:

pm2 start filename.js

For example, if the entry point for your Node.js application is in the index.js file, here’s how you can run the file:

pm2 start index.js

If you need to make any changes to your app, you can do so using a command line text editor to change the code, and then restart your PM2 app instance with this command:

pm2 restart filename.js

To completely close the app instead, use this command:

pm2 stop filename.js

Instead of running a single file with PM2, you can also run NPM scripts present in the package.json file of your Node.js application. For example, if you want to run the start script, structure the command like this:

pm2 start npm --name  -- start

Viewing logs and statistics

To monitor errors that may occur during execution, PM2 provides you with an option to check the logs generated by your application. Here’s how you can visualize them:

pm2 logs 

Viewing logs on PM2

To monitor all running processes, PM2 comes with a handy terminal-based dashboard to observe CPU and RAM usage, which you can access with this command:

pm2 monit

Using the PM2 Monitor Dashboard

To view the rest of the details and metadata like app version, number of restarts, uptime, script arguments, etc. :

pm2 show 

PM2 application metadata verification

Easily deploy Node.js applications in production

Using a process manager like PM2 can significantly reduce the overhead of constantly monitoring the health and availability of your Node.js application. With its built-in load balancer, you can expect increased performance and reliability for your services.

Speaking of deployment, you would most likely be using a Linux VM to deploy applications in the cloud. Don’t you know how you could do it? Here is a guide to get you started with virtual machines on Azure.


engineer deploying a virtual machine in azur
Learn how to deploy and configure a virtual machine in the cloud using Azure

Want to learn how to create and configure a virtual machine in the cloud? This guide will help you through the process.

Read more


About the Author



Source link

Previous Why everyone should have their own website
Next Victoria Royals to hit the ice for their home opener at SOFMC this Saturday