Skip to main content
⚡ OpenDevDocs
DevOps & Deployment
devopsdeploymentRecipeIntermediate

Deploy Node.js & Express API on Ubuntu VPS (PM2 + Nginx + SSL)

Deploy a Node.js Express/NestJS REST API server on Ubuntu VPS with PM2 cluster mode, automatic reboot recovery, Nginx reverse proxy, and SSL.

Verified: 2026-10-01
Tested on:node 22.x, express 4.x, nginx 1.26
Platforms:
linuxall
#nodejs#express#vps#pm2#nginx#api

Goal

Deploy a production Node.js Express or NestJS API backend on an Ubuntu VPS with multi-core clustering, automated zero-downtime reloads, and secure Nginx proxying.


git clone https://github.com/your-username/api-backend.git .
npm ci --only=production
```
```javascript
module.exports = {
  apps: [
    {
      name: "api-backend",
      script: "./dist/index.js", // or index.js
      instances: "max",          // Cluster across all CPU cores
      exec_mode: "cluster",
      watch: false,
      max_memory_restart: "500M",
      env_production: {
        NODE_ENV: "production",
        PORT: 5000,
      },
    },
  ],
};
```

Start and persist PM2:
```bash
pm2 start ecosystem.config.cjs --env production
pm2 save
pm2 startup
```
```nginx
server {
    listen 80;
    listen [::]:80;
    server_name api.example.com;

    # Max payload size for file uploads
    client_max_body_size 25M;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_http_version 1.1;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # Timeouts for heavy API queries
        proxy_connect_timeout 60s;
        proxy_read_timeout 60s;
    }
}
```

Enable and reload:
```bash
sudo ln -s /etc/nginx/sites-available/api.example.com.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d api.example.com
```

On this page