Guide To Setup A Node.js Production Environment Using PM2

Explore our detailed guide to set up a Node.js production environment using PM2. Learn the steps for seamless deployment and efficient process management in this blog.

date

Published On: 18 December, 2023

time

6 min read

In This Article:

Introduction

Node.js, a robust back-end JavaScript runtime, is pivotal for web and mobile app development. Achieving a seamless production deployment of Node.js is made effortless with the PM2 process manager, ensuring optimal performance and scalability, even in large-scale environments.

PM2, renowned for features like automatic restarts and load balancing, enhances the reliability of your Node.js applications. This article provides a comprehensive guide to setting up Node.js production with PM2, simplifying management for sustained, efficient operation with precision and ease.

Understanding Node.js Production Environment

By default, Node.js runs in a development environment. Both environments are based on different configurations, such as logging, error handling, database connections, etc. Which is controlled by setting the NODE_ENV=production environment variable.

PM2 Introduction

PM2 is the process manager mainly used to manage and deploy Node.js applications to handle different tasks like start, stop, restart, etc., and keep applications running with minimal downtime.

Pre-Requisites

Before starting, make sure that you have the latest version of Node.js and npm installed on your machine. If not, you can install using the NVM node version manager, a tool that is used to manage node versions on your machine. easy to install both at the same time.

Steps to Install Node & NPM using NVM

To install or update nvm, you can use the following cURL or Wget command:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Usage & Verification

You should restart the shell or run the following commands based on which shell is being used.

bash: source ~/.bashrc

zsh: source ~/.zshrc

ksh: . ~/.profile

Run the following command to verify the installation.

command -v nvm

Node Installation

To list available versions use the following command.

nvm ls-remote

To install the latest version use the following, by default it installs the latest version of Node.js

nvm install node or <specify the required version>

Now, you are ready to install the PM2 on your machine.

Steps to Setup Node.js Production Environment Using PM2

Here are the step-by-step instructions on How to use PM2 in Node.JS.

Step 1: Install and configure PM2

You can install the pm2 package globally by using the following command:

npm install pm2@latest -g

To verify the installation, run the following command:

pm2 --version

pm2 version

Step 2: Launch Your Application in Development Mode

Starting an application in development mode, helps you to continuously monitor the latest changes. You can start your application using the following command:

pm2-dev sample-app.js

pm2 de

Once the pm2 process has been started, it starts monitoring the current working directory of your application and you will notice the process restart on file changes. After successful testing, you should stop using Ctrl+C

pm2 var express

pm2 dev sample

Step 3: Start Your Application in the Production Environment

To launch an application in a production environment, use the pm2 start command, which starts your application in the background. Use the --name flag to give a proper name and the --watch flag to auto-restart the application when file content changes.

pm2 start sample-app.js --name "sample-app"

You should see the output of the application running as a process under PM2.

pm2 start sample

Step 4:  Keeping Tabs on Application Metrics

Use --help flag to view all other available parameters. Likewise, to list all running processes managed by PM2 with an overview of uptime, status, etc. use the following command:

pm2 list

pm 2list

Similarly, you can use the pm2 show command to get detailed metrics of specific applications.

pm2 show sample-app or <app id>

pm2 show sample

Moreover, you can view a real-time dashboard for monitoring application logs, resource usage, etc. by using the following command:

pm2 monit


Step 5: Ensure Application Uptime With PM2

You need to restart the application in most cases to take effect of new changes by using the restart command. PM2 by default restarts your application even if it exited intentionally or crashed. Earlier, it supported control of the restart behavior based on exit codes but now it's not functional.

pm2 restart sample-app

 

pm2 restart sample

pm sample app

Step 6: Fine-tuning Auto Restart Strategies

Starting an application using the pm2 start command doesn’t let you control the auto restart behavior but it’s supported with ecosystem.config.js by setting the auto restart flag to false. To create a configuration file, use the following command:

pm2 init simple

 

pm2 init sample

This creates an ecosystem.config.js in your current working directory and updates it with a restart flag.

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 auto-restart: false

  }]

}

By utilizing the ecosystem.config.js file, you were able to configure multiple applications in one file and start once by executing the pm2 start command:

pm2 start ecosystem.config.js

Restart Strategies:

Configuring restart strategies will give more control over your applications like restarting an application at a specific interval by defining a CRON time or memory threshold etc.

CRON Time:

It supports the CRON schedule for application restart like restarting an application at the start of every day. You can set a CRON time to achieve this by adding the cron_restart command:

module. exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 cron_restart: '0 */24 * * *'

  }]

}

Memory Based:

PM2 provides an option to restart your which when reaching the maximum threshold of memory to avoid heap memory issues. Use the following command to set:

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 max_memory_restart: '1G'

  }]

}

File Change:

By setting the watch command, your application automatically restarts upon file changes in the current working directory. It also provides an option to mention which folder to be watched or exclude.

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

watch: true

  }]

}

Delay Restart:

You can set a delay for restarting your application by using the restart_delay command:

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 restart_delay: 1000

  }]

}

Exponential Backoff Restart Delay:

This restarts your application in a smart way at the time of some exceptions like database is down. This time increased incrementally between restarts up to 15 seconds.

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 exp_backoff_restart_delay: 100

  }]

}

Once an exception occurred, exp_backoff_restart_delay was activated and a new application status showing waiting restart appeared.

Disable Auto Restart:

You can turn it off by setting the auto-restart option to false. This is very useful for executing one-time scripts.

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 autorestart: false

  }]

}

Step 7: Launch Applications on System Startup

You can achieve application auto restart upon system reboot due to crashes or upgrades by generating a script using the PM2 startup command, which generates a startup script for all the applications running on PM2.

pm2 startup systemd

 

pm2 startup sys

Now, run the generated command to enable Systemd startup:

sudo env PATH=$PATH:/usr/bin /user/local/lib/node_modules/pm2/bin/pm2 startup systemd -u devops --hp /home/devops

After successfully executing the script, run the pm2 save command to capture the list of all processes, to ensure auto start on bootup.

pm2 devops

If you are planning to upgrade the Node.js version make sure to deactivate the startup script by using pm2 unstartup and execute it again to ensure the latest version has been used.

pm2 unstartup

This ensures maximum uptime for your applications in case of any disruptions.

Step 8: Stop and Delete Your Application

To stop or delete your application from the pm2 list, you can use pm2 stop and pm2 delete respectively by mentioning the application ID, and name or using all for all applications. 

pm2 stop <id or app name>


pm2 stop all

 

pm2 stop all 1

Similarly, you can delete by doing the same.

pm2 delete <id or app name>

pm2 delete all

 

pm2 delete

Step 9: Manage Your Application Logs

By default, PM2 stores logs in the user home directory $HOME/.pm2/logs and stores them as a log file <app name>.out.log and <app name>.error.log for both standard output and errors respectively.

pm2 logs

To store a logs at custom location or format, you can define it in the ecosystem.config.js file:

module.exports = {

  apps : [{

     name   : "sample-app",

     script : "./sample-app.js",

     log_type: 'json',

     out_file: './sample-app-out.log',

     error_file: './sample-app-error.log'

  }]

}

 

pm2 sampleapp output

Logs Rotation:

Log files are getting large with time, to avoid this issue, the pm2-logrotate module is used to automatically rotate and utilize less space on the disk.

pm2 install pm2-logrotate

 

pm2 install pm2

Flush Logs:

To flush the logs of any application, you can use the following command:

pm2 flush <id or app name>

 

pm2 flush

Step 10: Clustering with PM2

Clustering allows applications to be scaled across multiple CPUs available on the machine, which increases performance and reliability by distributing the workloads among child processes. You can enable this by passing the --instances or -i flag while starting the application or mentioning it in the ecosystem.config.js file.

module.exports = {

  apps : [{

     name   : "sample-app",

     script : "./sample-app.js",

     log_type: 'json',

     out_file: './sample-app-out.log',

     error_file: './sample-app-error.log',

     instances: 'max', //automatically detects available CPUs

     exec_mode: 'cluster'

  }]

}

 

pm2 start eco

pm2 logs sampleapps

Step 11: Deploy your application in Production

PM2 offers a powerful deployment system that allows you to provision and deploy on multiple servers at once. You can simply modify the existing configuration file or generate a new one by using the following command:

pm2 ecosystem

The sample configuration looks like this:

module.exports = {

  apps : [{

 script: 'index.js',

 watch: '.'

  }, {

 script: './service-worker/',

 watch: ['./service-worker']

  }],

 

// Deployment Configuration

  deploy : {

 production : {

   user : 'SSH_USERNAME', //remote server authentication username

   host : 'SSH_HOSTMACHINE', //array of IP addresses

   ref  : 'origin/master',  //git branch and remote to deploy

   repo : 'GIT_REPOSITORY', //git repo remote URL

   path : 'DESTINATION_PATH', //remote server path for repo clone

   'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production' //commands or scripts to run after deployment.

 }

  }

};

Provisioning:

PM2 must be installed on a remote server and SSH key setup to clone the GIT repository.

Start provisioning your remote machine using the following command:

pm2 deploy production setup

 

pm2 deploy production

Deployment:

After successful provisioning, start deploying your application using the following command:

pm2 deploy production --force # if there are local changes

 

pm2 production force 1

Now, you can check on your remote server, and the pm2 application starts. 

pm2 status

Final Words

 PM2 in Node.JS offers a streamlined experience no matter whether you are working in a Dockerized environment or prefer daemonized deployments.

Because of its Docker compatibility, integrating it into containerized workflows is quite easy, and its demonization features make sure your Node.js application works smoothly in the background, improving system stability.

All things considered, the PM2 is a comprehensive solution for deploying, scaling, and managing Node.js applications—it's more than simply a process manager. 

It enables developers to create high-end, durable applications in a range of settings with its many comprehensive features, including load balancing and multi-server deployments.

Empower Your DevOps Journey With InvoZone As Your Partner

DevOps services from InvoZone are always the best when it comes to multi-server deployments. 

Our team of professionals ensures seamless installations across several servers and distributes updates in an equitable way to minimize downtime and enhance user experience overall. 

By choosing InvoZone as your DevOps partner, you'll have access to an abundance of expert information and a helpful ally who is fully invested in the real-world success of your Node.js projects. 

With a focus on constant optimization and strategic implementation, InvoZone's DevOps services and solutions significantly improve the scalability of your Node.js ecosystem.

Frequently Asked Questions

For production-level Node.js applications, PM2 is a potent process manager. It has a lot of helpful features, including load balancing, easy setup, and effective server management. 

Because it guarantees constant uptime, efficient use of resources, and simplified deployment procedures, PM2 is the preferred tool for developing reliable Node.js applications in a production setting.

To allow users to access your application, you must immediately deploy Node.js to production. You make your program accessible to people and allow them to interact with it by moving your project from a development environment to a production server. 

Production deployment ensures your Node.js application is ready for use, dependable, and accessible so that users have a platform to test and use it.

Benefits of using PM2 in a production setting include smooth deployment, enhanced process management, load balancing for greater performance, and horizontal scaling across multiple servers. 

Assuring consistent uptime, streamlining application maintenance, and optimizing resource usage make it an invaluable instrument for developing and maintaining resilient Node.js applications in real-world settings.

Yes, it is possible! You may monitor and assess the functionality of your Node.js application with PM2's monitoring features.  

The well-known "pm2 monit" command may be used to easily access an integrated dashboard, giving you important insights into resource usage and guaranteeing that your application is running as efficiently as possible.

Of course! PM2 is not the only option available for Node.js production environments. Among the most popular ones are:

  • Forever
  • Supervisor
  • Nodemon

For the deployment of applications and process administration, each of these environments has special advantages. The decision is based on the particular needs and preferences of the project.

DevOps Services

Don’t Have Time To Read Now? Download It For Later.

Introduction

Node.js, a robust back-end JavaScript runtime, is pivotal for web and mobile app development. Achieving a seamless production deployment of Node.js is made effortless with the PM2 process manager, ensuring optimal performance and scalability, even in large-scale environments.

PM2, renowned for features like automatic restarts and load balancing, enhances the reliability of your Node.js applications. This article provides a comprehensive guide to setting up Node.js production with PM2, simplifying management for sustained, efficient operation with precision and ease.

Understanding Node.js Production Environment

By default, Node.js runs in a development environment. Both environments are based on different configurations, such as logging, error handling, database connections, etc. Which is controlled by setting the NODE_ENV=production environment variable.

PM2 Introduction

PM2 is the process manager mainly used to manage and deploy Node.js applications to handle different tasks like start, stop, restart, etc., and keep applications running with minimal downtime.

Pre-Requisites

Before starting, make sure that you have the latest version of Node.js and npm installed on your machine. If not, you can install using the NVM node version manager, a tool that is used to manage node versions on your machine. easy to install both at the same time.

Steps to Install Node & NPM using NVM

To install or update nvm, you can use the following cURL or Wget command:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Usage & Verification

You should restart the shell or run the following commands based on which shell is being used.

bash: source ~/.bashrc

zsh: source ~/.zshrc

ksh: . ~/.profile

Run the following command to verify the installation.

command -v nvm

Node Installation

To list available versions use the following command.

nvm ls-remote

To install the latest version use the following, by default it installs the latest version of Node.js

nvm install node or <specify the required version>

Now, you are ready to install the PM2 on your machine.

Steps to Setup Node.js Production Environment Using PM2

Here are the step-by-step instructions on How to use PM2 in Node.JS.

Step 1: Install and configure PM2

You can install the pm2 package globally by using the following command:

npm install pm2@latest -g

To verify the installation, run the following command:

pm2 --version

pm2 version

Step 2: Launch Your Application in Development Mode

Starting an application in development mode, helps you to continuously monitor the latest changes. You can start your application using the following command:

pm2-dev sample-app.js

pm2 de

Once the pm2 process has been started, it starts monitoring the current working directory of your application and you will notice the process restart on file changes. After successful testing, you should stop using Ctrl+C

pm2 var express

pm2 dev sample

Step 3: Start Your Application in the Production Environment

To launch an application in a production environment, use the pm2 start command, which starts your application in the background. Use the --name flag to give a proper name and the --watch flag to auto-restart the application when file content changes.

pm2 start sample-app.js --name "sample-app"

You should see the output of the application running as a process under PM2.

pm2 start sample

Step 4:  Keeping Tabs on Application Metrics

Use --help flag to view all other available parameters. Likewise, to list all running processes managed by PM2 with an overview of uptime, status, etc. use the following command:

pm2 list

pm 2list

Similarly, you can use the pm2 show command to get detailed metrics of specific applications.

pm2 show sample-app or <app id>

pm2 show sample

Moreover, you can view a real-time dashboard for monitoring application logs, resource usage, etc. by using the following command:

pm2 monit


Step 5: Ensure Application Uptime With PM2

You need to restart the application in most cases to take effect of new changes by using the restart command. PM2 by default restarts your application even if it exited intentionally or crashed. Earlier, it supported control of the restart behavior based on exit codes but now it's not functional.

pm2 restart sample-app

 

pm2 restart sample

pm sample app

Step 6: Fine-tuning Auto Restart Strategies

Starting an application using the pm2 start command doesn’t let you control the auto restart behavior but it’s supported with ecosystem.config.js by setting the auto restart flag to false. To create a configuration file, use the following command:

pm2 init simple

 

pm2 init sample

This creates an ecosystem.config.js in your current working directory and updates it with a restart flag.

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 auto-restart: false

  }]

}

By utilizing the ecosystem.config.js file, you were able to configure multiple applications in one file and start once by executing the pm2 start command:

pm2 start ecosystem.config.js

Restart Strategies:

Configuring restart strategies will give more control over your applications like restarting an application at a specific interval by defining a CRON time or memory threshold etc.

CRON Time:

It supports the CRON schedule for application restart like restarting an application at the start of every day. You can set a CRON time to achieve this by adding the cron_restart command:

module. exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 cron_restart: '0 */24 * * *'

  }]

}

Memory Based:

PM2 provides an option to restart your which when reaching the maximum threshold of memory to avoid heap memory issues. Use the following command to set:

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 max_memory_restart: '1G'

  }]

}

File Change:

By setting the watch command, your application automatically restarts upon file changes in the current working directory. It also provides an option to mention which folder to be watched or exclude.

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

watch: true

  }]

}

Delay Restart:

You can set a delay for restarting your application by using the restart_delay command:

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 restart_delay: 1000

  }]

}

Exponential Backoff Restart Delay:

This restarts your application in a smart way at the time of some exceptions like database is down. This time increased incrementally between restarts up to 15 seconds.

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 exp_backoff_restart_delay: 100

  }]

}

Once an exception occurred, exp_backoff_restart_delay was activated and a new application status showing waiting restart appeared.

Disable Auto Restart:

You can turn it off by setting the auto-restart option to false. This is very useful for executing one-time scripts.

module.exports = {

  apps : [{

 name   : "app1",

 script : "./app.js",

 autorestart: false

  }]

}

Step 7: Launch Applications on System Startup

You can achieve application auto restart upon system reboot due to crashes or upgrades by generating a script using the PM2 startup command, which generates a startup script for all the applications running on PM2.

pm2 startup systemd

 

pm2 startup sys

Now, run the generated command to enable Systemd startup:

sudo env PATH=$PATH:/usr/bin /user/local/lib/node_modules/pm2/bin/pm2 startup systemd -u devops --hp /home/devops

After successfully executing the script, run the pm2 save command to capture the list of all processes, to ensure auto start on bootup.

pm2 devops

If you are planning to upgrade the Node.js version make sure to deactivate the startup script by using pm2 unstartup and execute it again to ensure the latest version has been used.

pm2 unstartup

This ensures maximum uptime for your applications in case of any disruptions.

Step 8: Stop and Delete Your Application

To stop or delete your application from the pm2 list, you can use pm2 stop and pm2 delete respectively by mentioning the application ID, and name or using all for all applications. 

pm2 stop <id or app name>


pm2 stop all

 

pm2 stop all 1

Similarly, you can delete by doing the same.

pm2 delete <id or app name>

pm2 delete all

 

pm2 delete

Step 9: Manage Your Application Logs

By default, PM2 stores logs in the user home directory $HOME/.pm2/logs and stores them as a log file <app name>.out.log and <app name>.error.log for both standard output and errors respectively.

pm2 logs

To store a logs at custom location or format, you can define it in the ecosystem.config.js file:

module.exports = {

  apps : [{

     name   : "sample-app",

     script : "./sample-app.js",

     log_type: 'json',

     out_file: './sample-app-out.log',

     error_file: './sample-app-error.log'

  }]

}

 

pm2 sampleapp output

Logs Rotation:

Log files are getting large with time, to avoid this issue, the pm2-logrotate module is used to automatically rotate and utilize less space on the disk.

pm2 install pm2-logrotate

 

pm2 install pm2

Flush Logs:

To flush the logs of any application, you can use the following command:

pm2 flush <id or app name>

 

pm2 flush

Step 10: Clustering with PM2

Clustering allows applications to be scaled across multiple CPUs available on the machine, which increases performance and reliability by distributing the workloads among child processes. You can enable this by passing the --instances or -i flag while starting the application or mentioning it in the ecosystem.config.js file.

module.exports = {

  apps : [{

     name   : "sample-app",

     script : "./sample-app.js",

     log_type: 'json',

     out_file: './sample-app-out.log',

     error_file: './sample-app-error.log',

     instances: 'max', //automatically detects available CPUs

     exec_mode: 'cluster'

  }]

}

 

pm2 start eco

pm2 logs sampleapps

Step 11: Deploy your application in Production

PM2 offers a powerful deployment system that allows you to provision and deploy on multiple servers at once. You can simply modify the existing configuration file or generate a new one by using the following command:

pm2 ecosystem

The sample configuration looks like this:

module.exports = {

  apps : [{

 script: 'index.js',

 watch: '.'

  }, {

 script: './service-worker/',

 watch: ['./service-worker']

  }],

 

// Deployment Configuration

  deploy : {

 production : {

   user : 'SSH_USERNAME', //remote server authentication username

   host : 'SSH_HOSTMACHINE', //array of IP addresses

   ref  : 'origin/master',  //git branch and remote to deploy

   repo : 'GIT_REPOSITORY', //git repo remote URL

   path : 'DESTINATION_PATH', //remote server path for repo clone

   'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production' //commands or scripts to run after deployment.

 }

  }

};

Provisioning:

PM2 must be installed on a remote server and SSH key setup to clone the GIT repository.

Start provisioning your remote machine using the following command:

pm2 deploy production setup

 

pm2 deploy production

Deployment:

After successful provisioning, start deploying your application using the following command:

pm2 deploy production --force # if there are local changes

 

pm2 production force 1

Now, you can check on your remote server, and the pm2 application starts. 

pm2 status

Final Words

 PM2 in Node.JS offers a streamlined experience no matter whether you are working in a Dockerized environment or prefer daemonized deployments.

Because of its Docker compatibility, integrating it into containerized workflows is quite easy, and its demonization features make sure your Node.js application works smoothly in the background, improving system stability.

All things considered, the PM2 is a comprehensive solution for deploying, scaling, and managing Node.js applications—it's more than simply a process manager. 

It enables developers to create high-end, durable applications in a range of settings with its many comprehensive features, including load balancing and multi-server deployments.

Empower Your DevOps Journey With InvoZone As Your Partner

DevOps services from InvoZone are always the best when it comes to multi-server deployments. 

Our team of professionals ensures seamless installations across several servers and distributes updates in an equitable way to minimize downtime and enhance user experience overall. 

By choosing InvoZone as your DevOps partner, you'll have access to an abundance of expert information and a helpful ally who is fully invested in the real-world success of your Node.js projects. 

With a focus on constant optimization and strategic implementation, InvoZone's DevOps services and solutions significantly improve the scalability of your Node.js ecosystem.

Frequently Asked Questions

For production-level Node.js applications, PM2 is a potent process manager. It has a lot of helpful features, including load balancing, easy setup, and effective server management. 

Because it guarantees constant uptime, efficient use of resources, and simplified deployment procedures, PM2 is the preferred tool for developing reliable Node.js applications in a production setting.

To allow users to access your application, you must immediately deploy Node.js to production. You make your program accessible to people and allow them to interact with it by moving your project from a development environment to a production server. 

Production deployment ensures your Node.js application is ready for use, dependable, and accessible so that users have a platform to test and use it.

Benefits of using PM2 in a production setting include smooth deployment, enhanced process management, load balancing for greater performance, and horizontal scaling across multiple servers. 

Assuring consistent uptime, streamlining application maintenance, and optimizing resource usage make it an invaluable instrument for developing and maintaining resilient Node.js applications in real-world settings.

Yes, it is possible! You may monitor and assess the functionality of your Node.js application with PM2's monitoring features.  

The well-known "pm2 monit" command may be used to easily access an integrated dashboard, giving you important insights into resource usage and guaranteeing that your application is running as efficiently as possible.

Of course! PM2 is not the only option available for Node.js production environments. Among the most popular ones are:

  • Forever
  • Supervisor
  • Nodemon

For the deployment of applications and process administration, each of these environments has special advantages. The decision is based on the particular needs and preferences of the project.

Share to:

Sana Kayani

Written By:

Sana Kayani

With a passion for playing around with words and always exploring different domains, she's... Know more

Contributed By:

M. Atif Hasnain 

DevOps Engineer

Get Help From Experts At InvoZone In This Domain

Book A Free Consultation

Related Articles


left arrow
right arrow