PM2: Production Process Management for Node.js
Complete guide to using PM2 in production, cluster mode, ecosystem.config.js, zero-downtime reload, log rotation, and server boot startup scripts.
PM2 is an advanced, production-proven process manager for Node.js applications with built-in load balancer cluster mode, runtime metrics, zero-downtime reloads, and log rotation.
Installation
npm install -g pm2Production Configuration (ecosystem.config.js)
Create ecosystem.config.js in your project root:
module.exports = {
apps: [
{
name: "web-api",
script: "./dist/server.js",
instances: "max", // Spawns 1 worker per CPU core
exec_mode: "cluster", // Enables Node.js cluster mode
autorestart: true,
max_memory_restart: "1G", // Auto-restarts if memory leak exceeds 1GB
env_production: {
NODE_ENV: "production",
PORT: 3000,
},
env_staging: {
NODE_ENV: "staging",
PORT: 3001,
},
error_file: "./logs/err.log",
out_file: "./logs/out.log",
merge_logs: true,
log_date_format: "YYYY-MM-DD HH:mm:ss Z",
},
],
};Essential PM2 Commands
# Start application using ecosystem file
pm2 start ecosystem.config.js --env production
# Zero-Downtime Reload (restarts workers sequentially)
pm2 reload web-api
# Live terminal monitoring dashboard
pm2 monit
# View real-time logs
pm2 logs web-api --lines 50
# Restart or stop processes
pm2 restart web-api
pm2 stop web-api
pm2 delete web-apiAuto-Start on System Boot
To automatically restore PM2 processes after a Linux server reboot:
# 1. Save currently running process list
pm2 save
# 2. Generate and run the OS startup systemd hook
pm2 startup
# (Copy and run the sudo command output by the above line)Automatic Log Rotation (pm2-logrotate)
Prevent log files from exhausting disk space:
pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 50M
pm2 set pm2-logrotate:retain 7Related Guides
Deploying a Node.js App to a Linux VPS
Complete end-to-end production guide for deploying a Node.js Express/TypeScript backend to an Ubuntu VPS using systemd, Nginx reverse proxy, and Let's Encrypt SSL.
Configuring an Nginx Reverse Proxy
Complete production guide to configuring Nginx as a reverse proxy for Node.js/Next.js apps, WebSocket upgrades, gzip compression, and security headers.