Top Banner
← Back to Blog

PM2 Complete Guide for Node.js Applications

PM2 Complete Guide for Node.js Applications

Learn how to install PM2, manage Node.js applications, configure startup scripts, use ecosystem files, enable cluster mode and monitor production workloads.

PM2Node.jsCluster ModeEcosystem FileDevOps

Install PM2

npm install -g pm2

Verify Installation

pm2 --version

Start Application

pm2 start app.js
pm2 start server.js

Start Application With Name

pm2 start app.js --name backend-api

View Running Processes

pm2 list

Restart Application

pm2 restart backend-api

Stop Application

pm2 stop backend-api

Delete Application

pm2 delete backend-api

Monitor Logs

pm2 logs
pm2 logs backend-api

Real-Time Monitoring

pm2 monit

Enable Startup On Server Reboot

pm2 startup
pm2 save

This ensures applications automatically start after reboot.

Ecosystem File Example-1

module.exports = {
  apps: [
    {
      name: "backend-api",
      script: "./server.js",
      instances: 2,
      exec_mode: "cluster",
      watch: false,
      autorestart: true,
      max_memory_restart: "500M",
      env: {
        NODE_ENV: "production",
        PORT: 3000
      }
    }
  ]
};

Start Using Ecosystem File

pm2 start ecosystem.config.js

Reload Zero Downtime

pm2 reload ecosystem.config.js

Fork Mode Example

Fork mode runs a single instance of your application. This is ideal for development environments, small websites, internal tools, or applications with low traffic.

pm2 start server.js --name backend-api

When To Use Fork Mode

  • βœ“ Development environments
  • βœ“ Low traffic websites
  • βœ“ Background workers
  • βœ“ Cron jobs
  • βœ“ Small APIs

Cluster Mode Example

Cluster mode allows PM2 to use multiple CPU cores. This improves performance, scalability, and application availability for production workloads.

pm2 start server.js -i max --name backend-api

The max option automatically uses all available CPU cores.

When To Use Cluster Mode

  • βœ“ Production APIs
  • βœ“ High traffic websites
  • βœ“ SaaS applications
  • βœ“ Node.js microservices
  • βœ“ Load-balanced applications

Useful PM2 Commands

Show Process Details
pm2 show backend-api
Flush Logs
pm2 flush
Restart All Apps
pm2 restart all
Stop All Apps
pm2 stop all
Update PM2
npm install pm2@latest -g && pm2 update
Scale Up Instances
pm2 scale backend-api 8

PM2 Best Practices

Follow these production-ready PM2 recommendations to improve reliability, performance, monitoring, and scalability of your Node.js applications.

πŸš€

Use Cluster Mode

Utilize all CPU cores by running multiple instances for improved scalability and performance.

πŸ”„

Enable Auto Restart

Automatically recover applications after crashes without manual intervention.

πŸ“„

Use Ecosystem Files

Store all application configurations in a central ecosystem.config.js file.

🧠

Set Memory Limits

Prevent runaway processes by configuring max_memory_restart values.

πŸ“Š

Monitor Regularly

Use pm2 monit and logs to proactively identify performance bottlenecks.

πŸ”

Protect Secrets

Store API keys, database credentials, and tokens in environment variables.

⚑

Zero Downtime Deployments

Use PM2 reload commands to deploy updates without service interruptions.

πŸ’Ύ

Enable Startup Scripts

Configure PM2 startup and save commands to survive server reboots.

πŸ›‘οΈ

Keep PM2 Updated

Always run the latest stable PM2 version for security fixes and improvements.