PM2 — Production Process Manager for Node.js
Keep your Node.js API running forever — auto-restart on crashes, survive server reboots:
Install & Start:
```cmd
npm install -g pm2
pm2 start server.js --name "nextsem-api"
pm2 startup # Configure auto-start on boot
pm2 save # Save current process list
`
Essential PM2 Commands:
```cmd
pm2 list # Show all processes and status
pm2 logs api # Stream live logs
pm2 restart api # Restart the process
pm2 reload api # Zero-downtime reload
pm2 stop api # Stop (keeps in list)
pm2 delete api # Remove from PM2
pm2 monit # Real-time CPU/RAM dashboard
`
ecosystem.config.js (Production Config):
```javascript
module.exports = {
apps: [{
name: 'nextsem-api',
script: 'server.js',
instances: 'max', // Use all CPU cores
exec_mode: 'cluster',
env_production: {
NODE_ENV: 'production',
PORT: 5000
}
}]
}
`
PM2 is how real production Node.js servers stay online 24/7! 💪
