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.
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
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
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
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:
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:
To view the rest of the details and metadata like app version, number of restarts, uptime, script arguments, etc. :
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.
Want to learn how to create and configure a virtual machine in the cloud? This guide will help you through the process.
About the Author