TutorialsAugust 4, 20264 min read

How to Deploy a Node.js App with PM2

Keep your Node app running forever with PM2: process management, auto-restart on crash and reboot, logs, and zero-downtime reloads.

NBy Nxeon

Running node app.js in an SSH session works until you close the terminal — then your app dies. PM2 is the process manager that keeps Node apps alive: it restarts them on crash, brings them back after a reboot, manages logs, and can run a cluster across CPU cores. This guide takes you from a working app to a resilient, always-on deployment.

Prerequisites

You need Node installed — ideally via NVM, as covered in installing Node.js with NVM — and an app that listens on a local port. We will use 127.0.0.1:3000 throughout and put Nginx in front later.

Install PM2

Install it globally with npm (no sudo needed if you used NVM):

npm install -g pm2
pm2 --version

Start your app under PM2

From your project directory, start the entry file and give it a name:

pm2 start app.js --name myapp

Check the status table:

pm2 status

You will see your app listed as online. If it crashes, PM2 restarts it automatically and increments a restart counter so you can spot instability.

The Nxeon game-server control panel — live console, player slots, and TPS
The Nxeon game-server control panel — live console, player slots, and TPS

Survive reboots

By default PM2 forgets everything on reboot. Two commands fix that. First generate and install a startup service:

pm2 startup

PM2 prints a sudo command tailored to your user — copy and run it. Then save your current process list as the set to resurrect:

pm2 save

Now your app comes back automatically after any reboot or power cycle. This is the single most important step people forget.

Read logs and monitor

PM2 aggregates stdout and stderr per app:

pm2 logs myapp          # live tail
pm2 logs myapp --lines 200
pm2 monit               # live CPU/memory dashboard

Logs are stored under ~/.pm2/logs/. To stop them growing forever, install log rotation:

pm2 install pm2-logrotate

Zero-downtime reloads

When you deploy new code, reload cycles workers without dropping requests:

git pull
npm install
pm2 reload myapp

Use pm2 restart for a hard restart (brief downtime) and pm2 reload for graceful. For CPU-bound apps you can run one worker per core with pm2 start app.js -i max, which load-balances across a cluster automatically.

Use an ecosystem file

For anything beyond a single script, define an ecosystem.config.js so config lives in version control:

module.exports = {
  apps: [{
    name: "myapp",
    script: "app.js",
    instances: 2,
    env: { NODE_ENV: "production", PORT: 3000 }
  }]
};

Then start everything with pm2 start ecosystem.config.js.

Put Nginx in front

PM2 keeps the app alive, but you still need a public front door with TLS. Follow how to set up Nginx as a reverse proxy to forward port 80/443 to 127.0.0.1:3000, then add HTTPS with Certbot. To automate pushes, combine PM2 with git-based deploys.

Guard against memory leaks

Long-running Node processes can slowly leak memory. PM2 can restart a worker automatically once it crosses a threshold, buying resilience while you hunt the root cause:

pm2 start app.js --name myapp --max-memory-restart 300M

Now if the process passes 300 MB, PM2 restarts it gracefully. You can also schedule a nightly restart in the ecosystem file with cron_restart: '0 4 * * *' for apps that misbehave over days.

Inspect a running app

When something looks wrong, pm2 describe myapp shows the full picture — uptime, restarts, memory, the exact script and interpreter, and the log paths. Pair it with pm2 logs myapp --err to see only error output. A climbing restart counter in pm2 status is your earliest warning that an app is crash-looping, long before users notice.

FAQ

PM2 vs systemd — which should I use for Node?

Both keep apps alive across reboots. PM2 is Node-native with clustering, log management, and a friendly CLI, which most Node developers prefer. If you want one tool for every service on the box, a systemd service is the leaner, dependency-free option.

Why did my app not come back after a reboot?

You ran pm2 startup but forgot pm2 save, or you added the app after saving. Run pm2 save again whenever your process list changes.

How do I set environment variables in PM2?

Use an ecosystem file's env block, or pass them inline. Avoid committing secrets — load them from a .env file your app reads, and keep that file out of git.

Does PM2 need root?

No, if you installed Node via NVM. The one exception is the pm2 startup step, which needs sudo once to register the boot service.

Deploy on Nxeon Node.js hosting or a plain developer VPS with full root and NVMe SSDs — PM2, Nginx, and your app, all under your control, with free migration help to move an existing app over.

#nodejs#pm2#vps#deployment#ubuntu#seobatch

Deploy your first server in under a minute

Creating an account is free and takes no card details. You pay when you deploy — choose a billing term and pay from your wallet or by card at checkout.