PM2 is a production process manager for Node.js. It keeps applications running after crashes, handles log management, enables zero-downtime reloads, and integrates with systemd for auto-start on boot. This guide deploys a Node.js application with PM2 and Nginx on Ubuntu 26.04 LTS.

Tested and valid on:

  • Ubuntu 26.04 LTS

Prerequisites

  • Ubuntu 26.04 LTS with Node.js 22 LTS installed
  • Nginx installed
  • A Node.js application ready to deploy

Step 1 – Install PM2 Globally

sudo npm install -g pm2
pm2 --version

Step 2 – Create a Sample Node.js Application

mkdir -p /var/www/myapp && cd /var/www/myapp
cat > app.js < {
  res.writeHead(200); res.end('Hello from PM2!n');
}).listen(3000, () => console.log('Server on :3000'));
EOF

Step 3 – Start the Application with PM2

pm2 start /var/www/myapp/app.js --name myapp
pm2 list

Step 4 – Configure Auto-Start on Boot

pm2 startup systemd -u $USER --hp $HOME

PM2 outputs a command — run it as instructed (it typically starts with sudo env PATH=...:

pm2 save

Step 5 – Set Up Nginx as a Reverse Proxy

sudo nano /etc/nginx/sites-available/myapp

Add:

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

Step 6 – Common PM2 Commands

pm2 logs myapp          # view logs
pm2 restart myapp       # restart
pm2 reload myapp        # zero-downtime reload
pm2 stop myapp          # stop
pm2 delete myapp        # remove from PM2
pm2 monit               # real-time monitor

Step 7 – Deploy Updates (Zero-Downtime)

cd /var/www/myapp && git pull
pm2 reload myapp

Conclusion

Your Node.js application is now running with PM2 and served via Nginx on Ubuntu 26.04 LTS. PM2 ensures the app restarts on crashes and survives server reboots.