Deploy Node.js App on VPS with PM2 Process Manager

Running node app.js directly on a VPS has a critical flaw — the app stops when you close the terminal, and won't restart if it crashes. PM2 (Process Manager 2) solves all of these problems: auto-restart, cluster mode, log management, and startup scripts all in one tool.

1. What is PM2 and Why Use It

PM2 is the most popular Production Process Manager for Node.js with these key features:

FeatureDescription
Auto-restartAutomatically restarts the app on crash or memory overflow
Cluster ModeUses multiple CPU cores for higher throughput
Log ManagementSeparate stdout/stderr logs with real-time viewing
Startup ScriptApp starts automatically after VPS reboot
Zero-downtime DeployReload app without dropping connections
MonitoringView CPU/Memory usage per process

2. Prerequisites

Tip: Use nvm to install Node.js for easy version management.

3. Install Node.js via NVM

# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc

# Install Node.js LTS
nvm install --lts
nvm use --lts

# Verify installation
node -v
npm -v

4. Install PM2

npm install -g pm2
pm2 -v

5. Start Your App with PM2

# Basic start
pm2 start app.js

# Start with a name (recommended)
pm2 start app.js --name "myapp"

# Start with arguments
pm2 start app.js --name "myapp" -- --port 3000

# Check status
pm2 status

# View logs
pm2 logs myapp
Best Practice: Always use --name so you can easily manage multiple apps on the same VPS.

6. Use ecosystem.config.js for Configuration

// ecosystem.config.js
module.exports = {
  apps: [{
    name: 'myapp',
    script: './app.js',
    instances: 'max',        // cluster mode — use all CPU cores
    exec_mode: 'cluster',
    watch: false,
    max_memory_restart: '500M',
    env: {
      NODE_ENV: 'production',
      PORT: 3000
    },
    error_file: './logs/err.log',
    out_file: './logs/out.log',
    log_date_format: 'YYYY-MM-DD HH:mm:ss'
  }]
};
# Start from config file
pm2 start ecosystem.config.js

# Zero-downtime reload
pm2 reload ecosystem.config.js

7. Cluster Mode — Boost Performance

Node.js is single-threaded. Cluster Mode allows PM2 to use multiple CPU cores:

# Use all CPU cores
pm2 start app.js -i max

# Use 4 cores
pm2 start app.js -i 4

# Zero-downtime reload
pm2 reload myapp
Warning: Cluster Mode only works with stateless apps. If your app stores sessions in memory, use Redis for session storage instead.

8. Startup Script — Run on Reboot

# Generate startup script (copy and run the output command with sudo)
pm2 startup

# After starting your apps, save the state
pm2 save
Important: Run pm2 save after every add/remove to persist your current process list.

9. Log Management

# View all logs
pm2 logs

# View specific app logs
pm2 logs myapp --lines 100

# Clear logs
pm2 flush

# Install automatic log rotation
pm2 install pm2-logrotate

10. Monitoring and Basic Commands

# Real-time dashboard
pm2 monit

# List all processes
pm2 list

# Detailed app info
pm2 show myapp

# Restart / Stop / Delete
pm2 restart myapp
pm2 stop myapp
pm2 delete myapp

# Restart all
pm2 restart all
CommandDescription
pm2 startStart a new process
pm2 restartRestart (brief downtime)
pm2 reloadZero-downtime reload
pm2 stopStop (stays in list)
pm2 deleteRemove from list
pm2 saveSave current state

11. Nginx Reverse Proxy Setup

# /etc/nginx/sites-available/myapp
server {
    listen 80;
    server_name yourdomain.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_set_header X-Real-IP $remote_addr;
        proxy_cache_bypass $http_upgrade;
    }
}

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

12. Zero-Downtime Deployment

# Pull new code
git pull origin main

# Install dependencies
npm install --production

# Zero-downtime reload (Cluster Mode)
pm2 reload myapp

# Or restart if not using Cluster Mode
pm2 restart myapp
AsiaGB VPS: AsiaGB VPS runs on SSD storage for high I/O performance — perfect for Node.js + PM2 requiring low response times. Available in Thailand and Singapore datacenters with 99% uptime.

Frequently Asked Questions

What is PM2 and why use it with Node.js?

PM2 is a production process manager that keeps Node.js apps running 24/7 with auto-restart, cluster mode, and log management — no terminal needed.

What is the difference between PM2 and nodemon?

nodemon is for development (auto-restart on file changes). PM2 is for production — it keeps apps running, uses multiple CPU cores, and manages logs.

What is PM2 Cluster Mode?

Cluster Mode spawns multiple workers matching CPU cores, dramatically increasing throughput for high-traffic Node.js apps.

Will PM2 run after a VPS restart?

Yes, after running pm2 startup and pm2 save, PM2 automatically starts your apps on every VPS reboot.

Where are PM2 logs stored?

Logs are in ~/.pm2/logs/. View real-time with pm2 logs and manage rotation with pm2 install pm2-logrotate.

Can I deploy Node.js on AsiaGB VPS?

Absolutely. AsiaGB VPS supports Node.js, PM2, and Nginx on Ubuntu with SSD storage and 99% uptime guarantee.

Summary

PM2 is an essential tool for deploying Node.js in production on a VPS. The core workflow is: install PM2 → start app with ecosystem config → enable Cluster Mode → set up startup script → add Nginx reverse proxy. For a reliable VPS to run your Node.js apps, check out AsiaGB VPS with SSD storage, 99% uptime, and dedicated support.