PM2 in Practice: Running, Updating and Auto-Starting Node.js Projects
Author's articles

PM2 in Practice: Running, Updating and Auto-Starting Node.js Projects

A Next.js site or a Strapi CMS runs fine after yarn dev on a laptop. On a server, though, you can't just leave a command running in the terminal: the process dies with your SSH session, doesn't come back after a reboot and doesn't restart after a crash. PM2, a process manager for Node.js, takes care of all of that. This cheat sheet collects the commands we use on our own servers every day. Every command can be copied with the button in the corner of its block.

Installation

Install PM2 globally, once per server. If the server has several Node.js versions via nvm, install PM2 into the one you use by default.

bash
npm install -g pm2
pm2 -v

Starting a project from its directory

The typical case: the project is already on the server, and you need to build it and run it in the background. Go to the folder, install dependencies, build, and hand the process over to PM2.

bash
cd /var/www/my-site
yarn install --frozen-lockfile
yarn build
pm2 start yarn --name my-site -- start

Here is what the last line does:

  • pm2 start yarn: PM2 runs yarn;
  • --name my-site: the process name you will use to manage the project;
  • --: everything after the double dash goes to yarn itself, so yarn start is what runs.

Build and start can be combined into one line. && runs the next command only if the previous one succeeded, so a broken build never goes live:

bash
yarn build && pm2 start yarn --name my-site -- start

The same with npm:

bash
npm run build && pm2 start npm --name my-site -- run start

Port and environment variables

PM2 remembers variables set before the command and passes them to the process:

bash
PORT=3001 NODE_ENV=production pm2 start yarn --name my-site -- start

For Next.js, the port can also be passed as an argument:

bash
pm2 start yarn --name my-site -- start -p 3001

Starting Strapi

Strapi builds its admin panel with build and runs in production with start:

bash
cd /var/www/my-api
yarn install --frozen-lockfile
NODE_ENV=production yarn build
NODE_ENV=production pm2 start yarn --name my-api -- start

A specific Node.js version per project

If a project needs a particular Node.js version (for example, Strapi on Node 20 and the site on Node 24), set the interpreter explicitly:

bash
pm2 start yarn --name my-api --interpreter ~/.nvm/versions/node/v20.20.0/bin/node -- start

Managing processes

List all processes with their status, restart count (↺), memory and CPU:

bash
pm2 list

Restart, stop or delete a process by name:

bash
pm2 restart my-site
pm2 stop my-site
pm2 delete my-site

Restart with updated environment variables. Without the flag, PM2 keeps the old values even if you changed .env or the variables in the command:

bash
pm2 restart my-site --update-env

Details about a process: path, command, interpreter, log files and uptime:

bash
pm2 describe my-site

Real-time monitoring of all processes (press q to quit):

bash
pm2 monit

Bulk operations:

bash
pm2 restart all
pm2 stop all

Logs

The latest log lines plus live output (press Ctrl+C to quit):

bash
pm2 logs my-site --lines 100

Errors only:

bash
pm2 logs my-site --err --lines 200

Logs live in ~/.pm2/logs/. Without rotation they grow forever and eventually fill the disk. The pm2-logrotate module fixes that with a single install:

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

Clear all current logs:

bash
pm2 flush

Auto-start after a server reboot

Skip this step and every site stays down after a reboot. First, generate the startup service:

bash
pm2 startup

PM2 prints a command for you to run: copy it and run it. Then save the current process list. This is the list that comes back after a reboot:

bash
pm2 save

Note: run pm2 save again every time you add or remove a process. To restore the saved list manually:

bash
pm2 resurrect

Updating a project (deploy)

The standard update cycle: pull the code, install dependencies, build and restart.

bash
cd /var/www/my-site
git pull
yarn install --frozen-lockfile
yarn build && pm2 restart my-site --update-env

If the build fails, && prevents the restart and the site keeps running the old version.

Near-zero-downtime updates for Next.js

The commands above have one drawback: yarn build overwrites the .next folder while the old process is still running, which can cause errors for visitors during the build. The fix is to build into a separate folder and swap it in instantly. Add this to next.config:

js
const nextConfig = {
  distDir: process.env.NEXT_DIST_DIR || '.next',
};

And update the project like this:

bash
rm -rf .next-build
NEXT_DIST_DIR=.next-build yarn build
rm -rf .next-old && mv .next .next-old && mv .next-build .next
pm2 reload my-site --update-env
rm -rf .next-old

reload restarts processes one at a time. In cluster mode (below), this gives you updates with no downtime.

The ecosystem config file

With several projects, keeping launch options in your head gets awkward. Describe them in an ecosystem.config.js file at the project root:

js
module.exports = {
  apps: [
    {
      name: 'my-site',
      cwd: '/var/www/my-site',
      script: 'yarn',
      args: 'start',
      env: { NODE_ENV: 'production', PORT: 3000 },
      max_memory_restart: '700M',
      time: true,
    },
    {
      name: 'my-api',
      cwd: '/var/www/my-api',
      script: 'yarn',
      args: 'start',
      interpreter: '/root/.nvm/versions/node/v20.20.0/bin/node',
      env: { NODE_ENV: 'production' },
    },
  ],
};
  • max_memory_restart restarts the process when it uses more memory than the limit, a safeguard against leaks;
  • time: true adds a timestamp to every log line.

Start everything in the file, or just one project:

bash
pm2 start ecosystem.config.js
pm2 start ecosystem.config.js --only my-site

Apply changes from the file to processes that are already running:

bash
pm2 reload ecosystem.config.js --update-env

Cluster mode

Node.js uses a single CPU core. PM2 can run several copies of the app and spread requests between them. For cluster mode, start Next.js itself rather than the yarn wrapper:

bash
pm2 start node_modules/next/dist/bin/next --name my-site -i max -- start -p 3000

-i max runs one copy per core, -i 2 runs two. Keep in mind that each copy then has its own in-memory state (sessions, caches, rate limits).

When something goes wrong

A process is errored, or its restart count ↺ keeps climbing? Check the errors:

bash
pm2 logs my-site --err --lines 100

The port is already taken by another process (EADDRINUSE)? Find out what holds it:

bash
lsof -i :3000

Update PM2 itself without losing running processes:

bash
npm install -g pm2@latest && pm2 update

Cheat sheet

TaskCommand
Start a projectpm2 start yarn --name my-site -- start
List processespm2 list
Restartpm2 restart my-site --update-env
Graceful restartpm2 reload my-site
Stop / deletepm2 stop my-site / pm2 delete my-site
Logspm2 logs my-site --lines 100
Monitoringpm2 monit
Auto-startpm2 startup → pm2 save

PM2 covers nearly everything you need to run Node.js projects on a VPS: background processes, restarts after crashes, logs and auto-start. If you need help deploying or maintaining a Next.js and Strapi project, get in touch.

More articles

VPN client apps

VPN Client Apps for Windows, Android, macOS and iPhone

A selection of VPN clients for Windows, Android, macOS and iPhone: Hiddify, Happ, v2rayN, v2rayNG, NekoBox, Clash Verge, sing-box, Streisand and more, with links to official sources.

Author's articles
103 AI services

103 AI Services: Links and What Each One Does

A catalog of 103 popular AI services grouped by task: assistants, images, video, writing, SEO, code, automation, voice and social media, with official links and descriptions.

Author's articles