Getting Started with PM2
Welcome to PM2
PM2 is a production-grade process manager for Node.js. When you deploy a Node.js app to a server, you can't just run node app.js in a terminal — the process dies when you close the SSH session, and it won't restart if it crashes. PM2 solves this.
PM2 is the standard way to run Node.js apps in production on Linux servers. If you're on EC2 or Lightsail, you almost certainly want PM2.
What PM2 Does
- Keeps your app alive — automatically restarts on crashes
- Survives server reboots — generates a startup script so your app relaunches on boot
- Cluster mode — forks your app across all CPU cores for better throughput
- Log management — captures stdout/stderr with rotation support
- Zero-downtime reloads — restart without dropping active connections
PM2 vs. Running Node Directly
node app.js | PM2 | |
|---|---|---|
| Survives crashes | No | Yes |
| Survives reboots | No | Yes |
| Multi-core usage | Single core | All cores (cluster mode) |
| Log management | None | Built-in with rotation |
| Monitoring | None | CPU, memory, uptime |
What You'll Learn
- Installation — installing PM2 globally with npm
- Starting apps —
pm2 start, naming processes, and passing arguments - Monitoring —
pm2 status,pm2 logs,pm2 monit - Cluster mode — scaling across CPU cores with a single flag
- Startup scripts — ensuring PM2 restarts on server reboot
- Ecosystem file — managing multiple apps with a single config file
- Log rotation — preventing disk exhaustion from log files
Prerequisites
- Node.js and npm installed on your server
- A Node.js application to deploy
- SSH access to a Linux server (EC2, Lightsail, or any VPS)