Deploy a Node.js app with PM2 behind Nginx

Install a Node.js LTS release, keep your app running with PM2 across crashes and reboots, and serve it through Nginx with HTTPS.

  • Intermediate
  • Linux VPS
  • 3 min read

By RunRacks Team · Published

A typical production setup for Node.js on one server: the app listens on 127.0.0.1:3000, PM2 keeps it alive and restarts it on boot, and Nginx handles the public traffic and HTTPS.

Run these steps as your sudo user, not root. See Create a sudo user.

1. Install Node.js (LTS)

Use an LTS release from NodeSource. Check nodejs.org for the current LTS line and adjust the number in the URL if needed. Download the setup script, look through it, then run it.

Ubuntu and Debian:

Terminal
curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install -y nodejs

AlmaLinux and Rocky Linux:

Terminal
curl -fsSL https://rpm.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo dnf install -y nodejs

Check with node -v and npm -v.

2. Get your app onto the server

Terminal
cd ~
git clone https://github.com/you/myapp.git
cd myapp
npm ci --omit=dev

Make sure the app listens on 127.0.0.1 and reads its port from the environment, for example app.listen(process.env.PORT || 3000, "127.0.0.1"). Run any build step your framework needs.

3. Run it with PM2

Terminal
sudo npm install -g pm2
PORT=3000 NODE_ENV=production pm2 start server.js --name myapp
pm2 status
pm2 logs myapp

4. Start on boot

Terminal
pm2 startup systemd

PM2 prints a sudo env PATH=... pm2 startup ... command. Copy and run exactly that line, then save the current process list:

Terminal
pm2 save

5. Put Nginx in front

Install Nginx (guide) and create a server block:

NGINX
server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        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;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

The Upgrade and Connection lines let WebSockets work. Test and reload with sudo nginx -t && sudo systemctl reload nginx, then add HTTPS with Let's Encrypt.

Deploy an update

Terminal
cd ~/myapp
git pull
npm ci --omit=dev
pm2 reload myapp

pm2 reload restarts the app with minimal interruption.

Troubleshooting

  • 502 Bad Gateway: the app is not running or listens on another port. Check pm2 status, pm2 logs myapp and sudo ss -tlnp | grep 3000.
  • App does not come back after reboot: you skipped running the printed pm2 startup command or pm2 save.
  • Logs growing large: pm2 install pm2-logrotate rotates PM2 logs automatically.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.

Linux VPS

Create an SFTP-only user limited to one folder

Give someone file access over SFTP without a shell, locked inside one folder. Configure internal-sftp with a Match block, set the chroot ownership right and test with WinSCP.

  • Advanced
  • Linux VPS
  • 5 min read

Linux VPS

Add a new user on Linux (adduser vs useradd)

Create a new user account on your Linux VPS with adduser on Ubuntu and Debian or useradd on AlmaLinux and Rocky Linux, with a password, home folder and the right shell.

  • Beginner
  • Linux VPS
  • 5 min read