Running node server.js in a terminal is fine for testing, but the app dies when you close the session, crashes stay down, and nothing starts it again after a reboot. PM2 is a widely used process manager for Node.js that fixes all three, and adds cluster mode, log handling and memory limits. This guide is for a Linux VPS or server you manage yourself, not for shared hosting.
Install PM2 globally with npm, describe your app in an ecosystem.config.js file, and start it with pm2 start ecosystem.config.js --env production. Use cluster mode with pm2 reload for restarts without dropped requests, install pm2-logrotate so logs don't fill the disk, and run pm2 startup followed by pm2 save so your apps come back after a reboot. Put nginx or Caddy in front for HTTPS, and keep the Node port closed to the internet.
1. Before you start
- A server you control. A VPS with SSH access and the right to install software. On Domain India shared hosting you can't run your own long-lived processes; see section 9 for the options there.
- A current Node.js LTS release. Use Node.js 24 LTS or 22 LTS. Node.js 18 and 20 have reached end of life; don't start new projects on them.
- A non-root user that owns the app, for example
deploy. Run your app as that user, not as root. - An app that starts from the command line, with
node server.jsornpm start.
2. Install PM2 and start your app
sudo npm install -g pm2
pm2 --versionThen, as your app user, from the app folder:
pm2 start server.js --name my-app # a single script
pm2 start npm --name my-app -- start # or an npm script
pm2 list # status, CPU, memory, restartsThe day-to-day commands:
pm2 logs my-app # follow the logs
pm2 restart my-app # stop and start again
pm2 reload my-app # zero-downtime restart in cluster mode
pm2 stop my-app # stop, keep it in the list
pm2 delete my-app # remove it from PM23. Describe the app in an ecosystem file
Command-line flags are easy to forget. Put the configuration in ecosystem.config.js in your project and commit it to Git:
module.exports = {
apps: [{
name: 'my-app',
cwd: '/home/deploy/my-app',
script: './server.js',
instances: 2,
exec_mode: 'cluster',
max_memory_restart: '500M',
env_production: { NODE_ENV: 'production', PORT: 3000 },
time: true,
}],
};Start it with pm2 start ecosystem.config.js --env production. Anyone on your team can now reproduce the exact setup from one file. Keep secrets such as database passwords in a .env file or your server's environment, not in the ecosystem file you commit.
4. Cluster mode and zero-downtime reloads
One Node.js process uses one CPU core. In cluster mode, PM2 runs several workers that share the same port:
instances: 'max'starts one worker per core; a number such as2sets it exactly. On a small VPS, leave room for the database and the OS.pm2 reload my-appreplaces workers one at a time, so a worker is always answering.pm2 restartstops everything at once.
Cluster mode needs a little care:
- In-memory state is per worker. Sessions, caches and rate limits held in memory are not shared. Keep them in a database or a shared store.
- Scheduled jobs run once per worker. Run cron-style jobs in a separate app with
instances: 1, or behind a lock. - WebSockets need sticky sessions across workers, for example with
@socket.io/sticky, or run WebSocket apps withinstances: 1.
For deploys that build each release in its own folder and switch over with a health check, see Zero-Downtime Deploys with PM2 and Git Hooks.
5. Logs and log rotation
PM2 writes each app's output to ~/.pm2/logs/. Read them with:
pm2 logs my-app --lines 200 # last 200 lines, then follow
pm2 logs my-app --err # errors onlyThose files grow forever unless you rotate them:
pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 10
pm2 set pm2-logrotate:compress trueThat rotates each log at 10 MB, keeps 10 old files and compresses them. For central logging across several servers, ship the logs to a tool such as Grafana Loki.
6. Start automatically after a reboot
- Generate the startup service.As your app user, run
pm2 startup systemd. PM2 prints asudo env PATH=...command. - Run the printed command.It installs a systemd service, named
pm2-plus your user name, that starts PM2 at boot. - Save the process list.With your apps running, run
pm2 save. PM2 restores exactly this list at boot. - Test it.Reboot the server, log in again and run
pm2 list. Check the service withsystemctl status pm2-deploy.
Run pm2 save again whenever you add or remove an app. After upgrading Node.js, run pm2 update and repeat pm2 startup, because the service records the path to Node.
7. Put a reverse proxy in front
Don't expose port 3000 to the internet. Bind the app to 127.0.0.1 and let nginx or Caddy handle HTTPS and forward requests to it. A minimal nginx block:
server {
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Add a certificate with Certbot, or use Caddy, which obtains one automatically. Allow only ports 22, 80 and 443 in your firewall.
8. Monitoring and common problems
pm2 monit shows live CPU, memory and logs for each process. max_memory_restart restarts a worker that grows past its limit, which buys time while you find a memory leak. For a hard limit on everything PM2 runs, set MemoryMax= on the systemd service.
| Problem | Likely cause | Fix |
|---|---|---|
| Port already in use | An old process still holds it | pm2 list, pm2 delete the old entry, or sudo ss -ltnp to find it |
| Apps missing after reboot | pm2 save never ran after pm2 startup | Start the apps, then run pm2 save |
| Disk full of logs | No log rotation | Install pm2-logrotate |
| Reload can't find the script | Relative paths | Set absolute cwd and script in the ecosystem file |
| Restart count keeps rising | The app crashes on start | pm2 logs my-app --err --lines 100 |
Alternatives: a plain systemd unit per app suits a single app with no cluster mode. Docker Compose suits you if everything else already runs in containers.
9. Running Node.js on Domain India
| Option | How Node.js runs | Use PM2? |
|---|---|---|
| VPS | You manage the whole server, with root access | Yes, as in this guide |
| App Platform | Node.js apps are detected and run for you; deploy from the client area or with a deploy token | No, the platform manages the process |
| cPanel shared hosting | Setup Node.js App, with Node.js 20, 22 or 24 (choose 22 or 24) | No, the panel manages the process |
VPS plans are self-managed and come without cPanel. See VPS plans, the App Platform, or How to Deploy a Node.js App on Shared Hosting.
- 1 vCPU
- 2 GB DDR4 RAM
- 64 GB NVMe SSD Storage
- 2 TB Monthly Bandwidth
- 512 MB RAM per app
- 1 vCPU
- 5 GB NVMe SSD
- PostgreSQL Database
Prices on the cards exclude 18% GST.
Does PM2 work on Domain India shared hosting?
No. Shared hosting doesn't let you run your own long-lived processes. On cPanel hosting, use the Setup Node.js App tool; for a managed option, use the App Platform; for full control, use a VPS with PM2.
Which Node.js version should I use with PM2?
A current LTS release: Node.js 24 LTS or 22 LTS. Node.js 18 and 20 have reached end of life and no longer receive security fixes.
What is the difference between pm2 restart and pm2 reload?
restart stops and starts all of the app's processes, so there is a short gap. reload replaces cluster-mode workers one at a time, so the app keeps answering requests. In fork mode, reload behaves like restart.
How do I make PM2 start my app after a server reboot?
Run pm2 startup systemd, run the sudo command it prints, start your apps, then run pm2 save. PM2 restores the saved list at every boot.
Can I run several Node.js apps on one server with PM2?
Yes. List each app in ecosystem.config.js, or start each with its own name. Give each app its own port and route them through nginx or Caddy.
Should I use cluster mode for every app?
Only if the app keeps no important state in memory. Apps with in-memory sessions, scheduled jobs or WebSockets need changes first, or should run as a single instance.
Ready to deploy? Compare VPS plans for full control with PM2, or the App Platform if you'd rather not manage a server at all.
Deploy Node.js apps with free SSL and your own domain, and leave the process management to us.
See the App Platform