01
Why PM2
Started with node server.js in a terminal, an application stops as soon as you close the SSH session, and does not come back after a crash or a VPS reboot. PM2 solves these three problems and adds a few tools:
- automatic restart after a crash;
- startup with the server;
- logs organized per application, viewable live;
- a dashboard in the terminal (CPU, memory);
- cluster mode, which runs one copy of the application per core, and reloads without downtime.
PM2 can also run a Python script or any other program, but it is designed for Node.js first. For a simple bot, a systemd service is also enough: the guide hosting a Discord bot compares the two.
02
Install Node.js and PM2, start the application
Start from a secured VPS (see securing a Linux VPS) and a regular user, not root: the application and PM2 will run under that account. Install an LTS version of Node.js, using one of the methods from the official Node.js download page (nvm or packages), then PM2:
node -v
npm install pm2@latest -g
With nvm, the command runs without sudo. If Node.js comes from the system packages, the global install requires sudo.
Then start the application from its folder:
cd ~/mon-api
pm2 start server.js --name mon-api
pm2 start npm --name mon-site -- start
The second form runs the start script from package.json. Everyday commands:
pm2 list
pm2 logs mon-api
pm2 logs mon-api --lines 100
pm2 restart mon-api
pm2 stop mon-api
pm2 delete mon-api
pm2 describe mon-api
pm2 monit
pm2 list: the applications, their status, restart count and memory;
pm2 logs: live logs (Ctrl+C to exit). They are stored in ~/.pm2/logs;
pm2 delete: removes the application from PM2's list, without touching its files.
A restart count that keeps climbing in pm2 list points to an application crashing in a loop: read pm2 logs.
03
The ecosystem.config.js file
Rather than remembering every command's options, describe your applications in a file. pm2 init simple creates an ecosystem.config.js template; adapt it, for example:
module.exports = {
apps: [{
name: 'mon-api',
script: './server.js',
cwd: '/home/app/mon-api',
max_memory_restart: '300M',
env: {
NODE_ENV: 'development',
PORT: 3000
},
env_production: {
NODE_ENV: 'production',
PORT: 3000
}
}]
};
cwd: the folder the application is started from;
max_memory_restart: restarts the application if it goes over this memory, a useful safety net against leaks;
env and env_production: the environment variables. The second set applies with --env production.
pm2 start ecosystem.config.js --env production
pm2 restart ecosystem.config.js --env production
pm2 start ecosystem.config.js --only mon-api
The file name must end in .config.js for PM2 to recognize it. Do not put passwords in a file versioned in a public repository.
04
Automatic startup and maintenance
Coming back with the server. Run, as your user and without sudo:
pm2 startup
PM2 detects systemd and prints a command that starts with sudo env PATH=…: copy it and run it as is. It creates a service that restarts PM2 at boot. Then save the list of applications to bring back:
pm2 save
Run pm2 save again whenever you add or remove an application. To test, reboot the VPS and check pm2 list. pm2 resurrect reloads the saved list by hand.
- After a Node.js update, the service still points to the old version: run
pm2 unstartup, then pm2 startup (running the sudo command shown each time, if there is one), and finally pm2 save.
- Updating PM2:
npm install pm2@latest -g, then pm2 update, which replaces the in-memory PM2 without losing the applications.
- Log size: by default, logs grow without limit. The
pm2-logrotate module rotates them: pm2 install pm2-logrotate.
05
Cluster mode and reverse proxy
Cluster mode runs several copies of the application, sharing the same port, to use several cores:
pm2 start server.js --name mon-api -i max
pm2 reload mon-api
-i max runs one copy per available core, -i 2 exactly two. In the ecosystem file, it is instances: 2 with exec_mode: 'cluster'. pm2 reload restarts the copies one after the other, without downtime, unlike pm2 restart. Two precautions:
- the application must be stateless: a session or data kept in memory in one copy does not exist in the others. Store them in a database or a service such as Redis;
- each copy uses its own memory: on a small VPS, two copies are often enough.
Cluster mode only applies to Node.js (and Bun) applications: a Python script stays in simple mode.
The reverse proxy. Make the application listen on 127.0.0.1:3000, without opening that port, and put a web server in front for the domain name and HTTPS. With Nginx installed on the VPS, one server block is enough:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Then add the certificate with Certbot and Let's Encrypt. You can also use Nginx Proxy Manager, with one caveat: it runs in a Docker container, where 127.0.0.1 means the container and not the VPS. The application must then be reachable from the Docker network; for a single application, Nginx installed directly remains the simplest.
06
On CloudFive
On a VPS, you manage everything: Node.js, PM2, the proxy and updates. The Ryzen VPS, from €5.99 per month, is well suited to Node.js applications:
- an AMD Ryzen 9 9950X, with DDR5 memory and NVMe storage;
- root access and a dedicated IP address;
- basic DDoS protection included.
Without administration, our Node.js and Python hosting is the alternative: a managed plan where you deploy your application without maintaining a server. The other VPS plans are on the VPS page.
The servers are in France, on our own network, monthly with no commitment, with French-speaking support 7 days a week by ticket, on Discord and by phone.
07
Frequently asked questions
How do I install PM2?
First install Node.js, then run npm install pm2@latest -g. With nvm, the command runs without sudo; with Node.js from the system packages, the global install requires sudo. Then check with pm2 list.
How do I restart a Node.js application when the server boots with PM2?
Run pm2 startup as your user, copy and run the sudo command it prints, then save the list of applications with pm2 save. Run pm2 save again after adding or removing an application.
What is the difference between pm2 restart and pm2 reload?
pm2 restart stops and starts all copies of the application, with a short interruption. pm2 reload, in cluster mode, restarts them one after the other to avoid downtime; it suits network applications such as an API or a website.
Where are PM2 logs stored?
In the user's ~/.pm2/logs folder, with an output file and an error file per application. The pm2 logs command shows them live, and the pm2-logrotate module limits their size.
Do I need a VPS to run a Node.js application?
No. A VPS with PM2 gives full control, but you have to maintain the system. If you do not want to manage anything, a managed plan such as CloudFive's Node.js and Python hosting runs the application for you.