Setting Up a Node.js Application for Production with PM2

When deploying a Node.js application in a production environment, ensuring its continuous operation and effective resource management is crucial. PM2 (Process Manager 2) is a popular process manager for Node.js applications that helps in managing and keeping applications online 24/7. This article will guide you through setting up your Node.js application for production using PM2, including configuration, updating, monitoring, and logging.

Initial Setup with PM2:

1. Installing PM2:

Before deploying your application, you need to have PM2 installed on your server. Install PM2 globally using npm:

npm install pm2@latest -g

2. Creating an Ecosystem File:

PM2 utilizes an ecosystem.config.js file which allows you to manage configurations for your applications. Here’s how you can set up this file:

Create a file named ecosystem.config.js in your project’s root directory with the following content:

module.exports = {
    apps : [{
      name: "firstnodeapp",
      script: "./dist/app.js",
      instances: "max", // This will use all CPUs. Adjust as needed.
      autorestart: true,
      watch: false, // Set to true if you want PM2 to restart on file changes
      max_memory_restart: '1G',
      dotenv: '.env'
    }]
  };

In this configuration:

  • name: Sets the application name in PM2.
  • script: Specifies the entry file of your application.
  • instances: Defines the number of instances to run. Using "max" utilizes all available CPUs, enhancing performance and load distribution.
  • autorestart: Enables the application to restart automatically if it crashes.
  • watch: Monitors changes in the application files, which is useful during development.
  • max_memory_restart: Automatically restarts your app if it reaches the specified memory limit, helping to keep memory leaks in check.
  • env: Specifies environment variables that are necessary for running your application in production.

3. Starting the Application:

Once your ecosystem.config.js is configured, you can start your application by running:

pm2 start ecosystem.config.js

Updating the Application:

When you make changes to your application and push them to your production server (via Git or other deployment methods), you must restart the application to apply these changes. Follow these steps:

1. Pull the latest changes:

git pull origin main

2. Rebuild your application if necessary (for example, when using TypeScript):

npm run build

3. Restart the application using PM2:

pm2 restart firstnodeapp

Monitoring and Logging:

Monitoring:

PM2 provides a built-in monitoring tool that can be accessed via:

pm2 monit

This command opens a dashboard where you can monitor CPU and memory usage for each instance of your application.

Logging:

PM2 automatically handles standard output and error logs. You can view these logs using:

pm2 logs firstnodeapp

You can also specify log file paths in your ecosystem.config.js if you need more control over log management.

Ensuring Maximum Resource Utilization:

To ensure that your application efficiently utilizes server resources:

  • Set instances to "max" to use all CPU cores.
  • Monitor memory and CPU usage to prevent overutilization.
  • Consider enabling the watch feature in development for instant feedback on code changes.

Conclusion

PM2 is a powerful tool for managing Node.js applications in production. It not only ensures that your application stays online but also provides essential features such as load balancing, monitoring, and logging. By configuring ecosystem.config.js appropriately and following best practices for deployment and maintenance, you can achieve a stable and efficient production environment for your Node.js applications.

Leave a Reply

Your email address will not be published. Required fields are marked *