Keeping a Node.js app running on a VPS with PM2

If you run node app.js in an SSH session, the application dies when the session closes, and does not return if the server reboots. PM2 is a process manager for Node.js that fixes this: it starts the application in the background, restarts it if it fails, keeps the logs, and brings it back up when the server boots. It is for a VPS with root access; on shared hosting, “Setup Node.js App” plays that role (Node.js in cPanel).

Installing and starting

1 Install Node.js. Follow the instructions at nodejs.org for your system: the versions in the distribution’s repositories are often behind. Check with node -v.
2 Create a user for the application instead of running it as root, and log in as that user. A compromised application only reaches what that user can reach.
3 Install PM2: sudo npm install -g pm2
4 Start the application from its folder: pm2 start app.js --name my-appIf the application starts through a script in package.json: pm2 start npm --name my-app -- start.
5 Check the state and the logs: pm2 list
pm2 logs my-app
pm2 monit
pm2 logs is the first place to look when the application goes down.
6 Make it survive a server reboot. Run pm2 startup: it prints a line you must copy and run with sudo. Then, with the application running, save the list: pm2 save. Test it for real by rebooting the VPS.

Everyday commands

Command What it does
pm2 restart my-app Stops and starts again. Use it after updating the code.
pm2 reload my-app Reloads without cutting the service, when the application allows it.
pm2 stop my-app Stops it but keeps it in the list.
pm2 delete my-app Removes it from the list. Then run pm2 save.
pm2 logs --lines 100 The last lines of the logs of everything PM2 manages.
pm2 env 0 The environment variables of process number 0 (the number comes from pm2 list).

For a setup you repeat, PM2 accepts an ecosystem.config.js file with the name, the startup file and the variables, started with pm2 start ecosystem.config.js. Generate the file with pm2 ecosystem.

Forgot pm2 save? Without it, PM2 brings back the list it had saved, and the new application is not in it. It is the most common cause of “it worked until I rebooted the server”. Another: running PM2 as root but saving the list under another user; pm2 startup has to be run for the user who runs the application.
The application should not be exposed directly. Make it listen on 127.0.0.1 and put a proxy with HTTPS in front: nginx as a reverse proxy. On an unmanaged VPS, PM2 and the application are yours to maintain: how far our support goes.
If you already use Docker, you do not need PM2: Docker itself restarts the container with restart: unless-stopped. Pick one of the two for each application, not both. For secrets and variables, see environment variables and secrets.

No VPS for the application yet? See the plans and pick the operating system when you order.

See the VPS servers

SEE ALSO

Node.js in cPanel: choosing the version and using npm

Ports and firewall on a VPS: why your app is not reachable

First steps with your VPS

RECOMMENDED PRODUCT

VPS server with root access

Resources of your own, the OS you choose, reinstall whenever you like. from 7.560,00 Kz/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?