Deployment is one of the most crucial and conclusive stages of software development. A proper deployment strategy is vital in delivering the best experience to your users while utilizing your services efficiently. However, this process also brings its own set of challenges with it.
What Is PM2 and Why Do You Need It?
PM2 is a daemon process manager that enables you to keep Node.js applications alive forever, reload them without downtime, manage application logging, monitoring, and clustering.
Imagine your Node.js web server crashes due to some runtime error. Without using something like PM2, you would have to manually track the status of your application periodically and restart it whenever it goes down. PM2 helps you automate this process and keeps your application online 24/7, even if things go wrong.
Installing PM2 on Linux Servers
Once you get Node.js up and running on your server, install PM2 by running this command:
npm install -g pm2
The -g flag in the above command stands for Global and allows you to use PM2 across all your Node.js applications. You might need to add sudo at the start of the command, depending on the permissions your user account has.
Running Node Apps With PM2
pm2 start filename.js
For example, if the entry point to your Node.js application resides inside the index.js file, this is how you can run the file:
pm2 start index.js
In case you need to make any changes to your application, you can do so using a command-line text editor to edit the code, and then restart your PM2 application instance with this command:
pm2 restart filename.js
To completely shut down the application instead, use this command:
pm2 stop filename.js
Instead of running a single file with PM2, you can also execute NPM scripts present inside the package.json file of your Node.js application. For example, if you want to run the start script, structure the command in this manner:
pm2 start npm --name
Viewing Logs and Statistics
To monitor the errors that may arise during runtime, PM2 provides you with an option to check out the logs your application generates. Here's how you can view them:
To monitor all the processes that are currently running, PM2 comes with a handy terminal-based dashboard to observe the CPU and RAM usage, that you can access with this command:
To view the rest of the details and metadata such as the application version, the number of restarts, uptime, script arguments, and more:
Deploy Node.js Apps to Production at Ease
Using a process manager like PM2 can massively reduce the overhead of constantly having to monitor the status and uptime 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 probably use a Linux virtual machine to deploy apps to the cloud. Unsure about how you could do that? Here's a guide to get you started with virtual machines on Azure.
Author: Nitin Ranganath
Source: Nitin Ranganath.” How to Install and Set Up PM2 on Linux Servers”. Retrieved From https://www.makeuseof.com/how-to-install-and-set-up-pm2-on-linux-servers/
All Rights Of This Article Reserved To MakeUseOf