All Guides

Node.js App Management with PM2

Manage your Node.js applications in production with PM2 process manager.

Intermediate15 min

Setup Steps

1. Install PM2 globally:

npm install -g pm2

2. Start your application:

pm2 start app.js --name "myapp"

3. Start a Next.js application:

pm2 start npm --name "nextjs" -- start

4. Basic commands:

pm2 list                # List applications
pm2 stop myapp          # Stop
pm2 restart myapp       # Restart
pm2 delete myapp        # Delete
pm2 logs myapp          # Show logs
pm2 monit               # Live monitor

5. Auto-startup configuration:

pm2 startup
pm2 save

6. Ecosystem file (ecosystem.config.js):

module.exports = {
  apps: [{
    name: 'myapp',
    script: 'app.js',
    instances: 'max',
    exec_mode: 'cluster',
    env: {
      NODE_ENV: 'production',
      PORT: 3000
    }
  }]
}

7. Start with ecosystem:

pm2 start ecosystem.config.js

8. Zero-downtime update:

pm2 reload myapp