Skip to content
Log In Sign Up info@muvcloud.com

Before You Start

An application that runs as root can do anything on the server if it is compromised. PM2 runs your app as the user who starts it, so start it as deploy.

Install PM2

sudo npm install -g pm2
pm2 --version

With Node.js from the system or a vendor package, global npm packages need sudo to install. If you installed Node.js with nvm under the app user, run the command without sudo as that user.

Start an App

cd /home/deploy/api
pm2 start server.js --name api
pm2 start npm --name web -- start

The first form runs a script. The second runs npm start; the two dashes pass the rest of the line to npm. Give each app a name: every other command uses it.

Everyday Commands

CommandWhat it does
pm2 listShows all apps and their state
pm2 logs apiFollows the logs
pm2 restart apiStops and starts the app
pm2 reload apiRestarts workers one by one
pm2 stop apiStops the app
pm2 delete apiRemoves it from PM2
pm2 show apiShows details and paths
pm2 monitLive CPU and memory view

restart causes a short gap. reload avoids the gap for apps in cluster mode, where PM2 runs several copies and replaces them one at a time. Logs are written to ~/.pm2/logs of the user running PM2; they grow without limit until you add rotation, for example with the pm2-logrotate module, installed with pm2 install pm2-logrotate.

Start on Boot

PM2 itself must start when the server boots. As the app user, run:

pm2 startup

It prints a command that starts with sudo env PATH=. Copy and run that exact line: it creates a systemd service named pm2-deploy (after your user name) that starts PM2 as your user. Then save the list of running apps, which the service restores at boot:

pm2 save

Run pm2 save again whenever you add or remove an app. After a reboot, check with pm2 list. If you upgrade Node.js, run pm2 unstartup and pm2 startup again, because the service file contains the path to Node.js; see how to update Node.js.

The Ecosystem File

For settings you want to keep, describe the app in a file. pm2 init simple creates a starting point named ecosystem.config.js:

module.exports = {
  apps: [{
    name: 'api',
    script: './server.js',
    cwd: '/home/deploy/api',
    instances: 2,
    exec_mode: 'cluster',
    max_memory_restart: '400M',
    env: {
      NODE_ENV: 'production',
      PORT: 3000
    }
  }]
};
pm2 start ecosystem.config.js
pm2 reload ecosystem.config.js
pm2 save

Keep secrets out of this file if it is in Git; read them from an environment file that only the app user can read.

Nginx in Front

Let the app listen on 127.0.0.1:3000 and let Nginx take ports 80 and 443, handle HTTPS, and pass requests on. The setup is in Nginx reverse proxy, and the certificate in Certbot Nginx. Do not open port 3000 in the firewall. When Nginx answers with 502, PM2 usually shows the app as stopped or restarting; see 502 Bad Gateway in Nginx.

PM2 or systemd?

systemd can do the core job without extra software: a unit with User=deploy, WorkingDirectory, ExecStart=/usr/bin/node server.js, and Restart=on-failure keeps one app running, logs to the journal, and starts at boot. Units are described in systemd unit files. PM2 adds cluster mode, reloads without a gap, a live monitor, and one set of commands for many Node.js apps. For a single small app, systemd is enough; for several apps or cluster mode, PM2 saves work.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations