Node.js
Node.js deployments with PM2 and zero downtime
PM2 is the standard process manager for Node.js applications in production. It handles clustering, automatic restarts on crash, graceful reloads with zero downtime, and log management.
Installing PM2
npm install -g pm2
Starting your app
# Start in cluster mode (uses all CPU cores) pm2 start app.js -i max --name myapp # Or use an ecosystem file pm2 start ecosystem.config.js
Ecosystem file
module.exports = {
apps: [{
name: 'myapp',
script: 'app.js',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: 3000
}
}]
};
Zero-downtime reload
PM2’s reload command restarts workers one at a time, keeping the app available during updates:
pm2 reload myapp
Auto-start on reboot
pm2 startup pm2 save
Log management
# View logs pm2 logs myapp # Flush logs pm2 flush # Rotate logs (install pm2-logrotate) pm2 install pm2-logrotate
Monitoring
pm2 monit
Using Simplewala
Simplewala’s Node.js installer sets up PM2 for you and integrates with the Git App deployment system so your app reloads automatically on every push to your main branch.