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
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:
curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install -y nodejsAlmaLinux and Rocky Linux:
curl -fsSL https://rpm.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo dnf install -y nodejsCheck with node -v and npm -v.
2. Get your app onto the server
cd ~
git clone https://github.com/you/myapp.git
cd myapp
npm ci --omit=devMake 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
sudo npm install -g pm2
PORT=3000 NODE_ENV=production pm2 start server.js --name myapp
pm2 status
pm2 logs myapp4. Start on boot
pm2 startup systemdPM2 prints a sudo env PATH=... pm2 startup ... command. Copy and run exactly that line, then save the current process list:
pm2 save5. Put Nginx in front
Install Nginx (guide) and create a server block:
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.
On AlmaLinux and Rocky Linux, SELinux stops Nginx from connecting to local app ports until you allow it: sudo setsebool -P httpd_can_network_connect 1.
Deploy an update
cd ~/myapp
git pull
npm ci --omit=dev
pm2 reload myapppm2 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 myappandsudo ss -tlnp | grep 3000. - App does not come back after reboot: you skipped running the printed
pm2 startupcommand orpm2 save. - Logs growing large:
pm2 install pm2-logrotaterotates 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.
Related guides
Linux VPS
Convert keys between PuTTY (.ppk) and OpenSSH formats
Use PuTTYgen to convert a PuTTY .ppk private key to OpenSSH format and back, understand PPK version 2 and 3, generate a new Ed25519 key and load it in PuTTY.
- Intermediate
- Linux VPS
- 5 min read
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