Before You Start
- Node.js installed: see how to install Node.js on Ubuntu.
- A user for the app: an ordinary account such as
deploythat owns the application folder. Create one as in how to create a sudo user on Ubuntu, without sudo rights if the app account needs none. - An app that listens on a local port: the examples use port 3000 on
127.0.0.1.
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
| Command | What it does |
|---|---|
pm2 list | Shows all apps and their state |
pm2 logs api | Follows the logs |
pm2 restart api | Stops and starts the app |
pm2 reload api | Restarts workers one by one |
pm2 stop api | Stops the app |
pm2 delete api | Removes it from PM2 |
pm2 show api | Shows details and paths |
pm2 monit | Live 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.