Developer Guide

Intermediate35–50 min

Deploy NestJS with PM2, Nginx, and SSL

Build a NestJS API, keep it alive with PM2, reverse proxy it through Nginx, and issue an HTTPS certificate.

1

Install and build

Install exactly the locked dependencies, run project checks, and create the production build.

cd /var/www/<app-name>
npm ci
npm run build
2

Start with PM2

Run the compiled NestJS entry point under a stable process name.

pm2 start dist/main.js --name <app-name>
pm2 save
pm2 startup

Note: Run the additional sudo command printed by pm2 startup; it is specific to your user and host.

3

Add the Nginx reverse proxy

Proxy the public domain to the local NestJS port and preserve request metadata.

server {
  listen 80;
  server_name <domain>;

  location / {
    proxy_pass http://127.0.0.1:<port>;
    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;
  }
}
4

Enable and validate Nginx

Link the site only after reviewing it, then test syntax before reload.

sudo ln -s /etc/nginx/sites-available/<app-name> /etc/nginx/sites-enabled/<app-name>
sudo nginx -t && sudo systemctl reload nginx
5

Issue the TLS certificate

Use Certbot's Nginx integration after DNS and HTTP are working.

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d <domain>
6

Check runtime health

Verify HTTPS, the process, proxy logs, and certificate renewal.

curl -I https://<domain>
pm2 status
sudo certbot renew --dry-run

Final verification

  • ✓ HTTPS returns the expected status without a certificate warning
  • ✓ PM2 shows the application online
  • ✓ sudo nginx -t succeeds
  • ✓ The NestJS port is not publicly exposed