Log inSign up

Running Node.js apps under the PM2 process manager

Install PM2 globally with npm, start your app with pm2 start, then run pm2 startup and pm2 save so the same process list comes back after a reboot. Put settings in an ecosystem.config.js file, add the pm2-logrotate module to keep logs small, and use cluster mode to spread a stateless app over every vCPU of the instance.

Topic: Node.js · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What does PM2 do that plain node does not?

PM2 is a process manager for Node.js. It keeps your app alive when it crashes, restarts it on boot, captures stdout and stderr into log files, reloads without dropping requests in cluster mode, and shows live CPU and memory per process. You could do part of this with a plain systemd service; PM2 is handy when one user runs several Node apps and wants one tool for all of them.

How do you install PM2?

Install Node.js first, as on installing Node.js and npm, then add PM2 as a global package. With a system Node use sudo; with nvm install it without sudo inside the user account.

# system-wide Node.js
sudo npm install -g pm2@latest
# nvm in the deploy account
npm install -g pm2@latest

pm2 --version

Run all following commands as the user that owns the app, here deploy. Each user has its own PM2 daemon and process list under ~/.pm2, so apps started by root are invisible to deploy and the other way around.

How do you start and manage an app?

Point PM2 at the entry file and give the process a name. The name is what you use in every later command.

cd ~/apps/shop
pm2 start server.js --name shop
pm2 ls
pm2 show shop
pm2 restart shop
pm2 stop shop
pm2 delete shop

For an npm script, start npm itself: pm2 start npm --name web -- run start. For apps in other languages, add --interpreter, for example python3. pm2 monit opens a live dashboard in the terminal with logs and resource use side by side.

How do you make apps start again after a reboot?

Generate a boot script once, then save the current process list. pm2 startup detects systemd and prints a command for you to run with sudo; copy it exactly, because it carries your PATH and home directory.

pm2 startup systemd
# PM2 prints a line similar to this; run the line it gives you:
sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u deploy --hp /home/deploy
pm2 save

The result is a unit called pm2-deploy.service that resurrects everything saved in ~/.pm2/dump.pm2. Prove it with a real restart: sudo systemctl reboot, log back in, and run pm2 ls. Whenever you add or remove apps, run pm2 save again. If you used nvm and later change the Node version, run pm2 unstartup and repeat the startup step so the unit points to the new binary path.

What goes in an ecosystem file?

The ecosystem file stores everything you would otherwise type as flags, so the setup lives in git with the code. Create a template with pm2 init simple or write one by hand:

module.exports = {
  apps: [
    {
      name: "shop",
      script: "server.js",
      cwd: "/home/deploy/apps/shop",
      instances: 1,
      max_memory_restart: "400M",
      env: {
        NODE_ENV: "production",
        PORT: 3000
      }
    },
    {
      name: "worker",
      script: "jobs/worker.js",
      cwd: "/home/deploy/apps/shop",
      cron_restart: "0 4 * * *"
    }
  ]
};

Start, reload or stop the whole set from the file:

pm2 start ecosystem.config.js
pm2 reload ecosystem.config.js --update-env
pm2 save

max_memory_restart restarts a process that grows past the limit, a useful guard on a 1 or 2 GB instance. Keep secrets out of this file if it is committed: load them from an environment file the app reads, or from the shell before starting.

How do you read and rotate logs?

PM2 writes each app to ~/.pm2/logs/NAME-out.log and NAME-error.log. Follow them live or print the tail:

pm2 logs shop
pm2 logs shop --lines 200 --nostream
pm2 flush shop

Those files grow without limit. Install the rotation module once per user and tune it:

pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 20M
pm2 set pm2-logrotate:retain 10
pm2 set pm2-logrotate:compress true

With these values each log is rotated at 20 MB and ten compressed copies are kept, which keeps a small SSD from filling up. Check disk use from time to time with the commands in checking disk usage.

How does cluster mode work?

Cluster mode starts several copies of an HTTP app that share one port, using the Node.js cluster module behind the scenes. PM2 spreads incoming connections across them, so a 4 vCPU instance can use all four cores.

pm2 start server.js --name api -i max
# or in the ecosystem file
#   instances: "max",
#   exec_mode: "cluster"
pm2 reload api

pm2 reload restarts workers one after another, so the app keeps answering during a deploy. Cluster mode suits stateless apps. Anything kept in process memory, such as sessions or WebSocket rooms, has to move to Redis or the database first, otherwise users bounce between workers that do not know them. On a 1 vCPU plan, one instance is the right number.

How do you update PM2 and troubleshoot it?

After installing a newer PM2 or switching Node.js, update the running daemon in place so it loads the new code without losing your list:

npm install -g pm2@latest
pm2 update
  • App restarts in a loop: look at pm2 logs NAME --err; the app is crashing at startup, often because of a missing environment variable.
  • Nothing after reboot: systemctl status pm2-deploy shows whether the unit exists; you may have skipped pm2 save.
  • Port already in use: another copy, possibly under root, holds the port. Check with sudo ss -ltnp | grep 3000.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A2-4two workers in cluster mode with headroom2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
A4-8four workers for a busier API4 vCPU, 8 GB RAM, 180 GB NVMe$28.80/mo$86.40 for 3 months

Questions about Keep Node.js apps running with PM2

Is PM2 better than a systemd service?

Neither is better in general. systemd needs no extra software and suits one app. PM2 adds cluster mode, zero downtime reloads and a shared view of several Node apps for one user.

Should PM2 run as root?

No. Run it as the app user so a bug in the app cannot touch the rest of the system. Bind to a high port and let Nginx listen on 80 and 443.

How many instances should cluster mode start?

Start with one per vCPU (-i max), then lower it if memory gets tight. Every worker loads its own copy of the app into RAM.

Where does PM2 store its data?

In ~/.pm2 of the user running it: logs, PID files, the saved process list and installed modules.

Can PM2 load a .env file?

Not by itself in the basic setup. Use a library such as dotenv inside the app, or the Node.js flag --env-file=.env through node_args in the ecosystem file.