How to Install and Set Up PM2 on Linux Servers

How to Install and Set Up PM2 on Linux Servers

Hello Geeky, so today we are focusing on How you can Install and Set Up PM2 on Linux Servers. So please read this tutorial carefully so you may comprehend it in a better helpful way.

Guide: How you can Install and Set Up PM2 on Linux Servers

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.

Node.js, the open-source JavaScript runtime, is a popular choice for building the backend infrastructure of your application by allowing you to run JavaScript outside web browsers. But what if your Node.js application crashes in production?

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

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

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 P

Once the installation process is completed, navigate to the directory where your Node.js application resides using the cd command. Inside the project directory, you can execute a JavaScript file using PM2 with the command given below

pm2 start filename.

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.

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.

To completely shut down the application instead, use this command

pm2 stop filename.

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 <app_name> — sta

Viewing Logs and Statistic

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

pm2 logs <app_na

me:srt:js:js:js:js:M2me:srt:js:js:js:js:M2

pm2 logs <app_name>

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:

pm2 monit

To view the rest of the details and metadata such as the application version, the number of restarts, uptime, script arguments, and more:

pm2 show <app_name>

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.

Faq

Guide about How you can Install and Set Up PM2 on Linux Servers


In this guide, we told you about the How you can Install and Set Up PM2 on Linux Servers; please read all steps above so that you understand How you can Install and Set Up PM2 on Linux Servers in case if you need any assistance from us, then contact us.

How this tutorial or guide assisting you?


So in this guide, we discuss the How you can Install and Set Up PM2 on Linux Servers, which undoubtedly benefits you.




Final note

I hope you like the guide How you can Install and Set Up PM2 on Linux Servers. In case if you have any queries regards this article/tutorial you may ask us. Also, please share your love by sharing this article with your friends and family.

Trickbugs: Get the latest Trick and Tips
Logo
Enable registration in settings - general