After you deploy a Node.js app to a VPS, running node app.js directly stops the moment your SSH session closes. Production needs process management, auto-start on boot, and a reverse proxy. This guide does it with PM2 + Nginx.

1. Install Node.js (nvm recommended)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts
node -v && npm -v

2. Deploy your code

git clone https://your-repo.git app
cd app
npm install --production

3. Keep it alive with PM2

npm install -g pm2
pm2 start app.js --name myapp
pm2 status
pm2 logs myapp

Make PM2 relaunch your app after a reboot:

pm2 save
pm2 startup   # then run the sudo line it prints

4. Reverse proxy with Nginx (port 80/443)

Assuming the app listens on 3000, create a site config:

server {
    listen 80;
    server_name yourdomain.com;
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Test and reload: sudo nginx -t && sudo systemctl reload nginx. Then issue an SSL cert with Certbot to serve HTTPS.

5. Common issues

  • Port in use: find and free it with lsof -i:3000.
  • Memory leak restarts: pm2 start app.js --max-memory-restart 300M.
  • Updating code: git pull && npm install && pm2 reload myapp (zero-downtime reload).

Need help troubleshooting a deploy? Contact sales on Telegram @aliyun370.