Your Node.js app runs perfectly on your laptop. You SSH into your new VPS, run node app.js, and it works there too, right up until you close the terminal and the app dies with it. A production deployment needs three things that a bare node command does not give you: a process manager that restarts the app when it crashes or the server reboots, a web server in front to handle your domain and HTTPS, and a repeatable way to ship updates.
This guide shows how to deploy a Node.js app on a VPS with PM2 and an Nginx reverse proxy on Ubuntu.
The architecture
Visitor → Nginx (ports 80/443, HTTPS) → Node.js app on 127.0.0.1:3000 (managed by PM2)
Nginx faces the internet, terminates TLS, serves static files if you want, and forwards everything else to your app. The app only listens on localhost, so nobody can reach it directly. PM2 keeps it running.
Step 1: Prepare the server
Log in as a non-root user with sudo, update the system, and open the firewall for SSH and web traffic:
sudo apt update && sudo apt upgrade -y
sudo apt install nginx git -y
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
Step 2: Install Node.js
Ubuntu's default repository often carries an older Node.js release. Two good options:
- NodeSource packages give you a system-wide Node version installed through apt. Follow the instructions on NodeSource's distribution page for the LTS line you want.
- nvm (Node Version Manager) installs Node per user and makes switching versions easy. Check the nvm project's README for the current install command, then run
nvm install --lts.
Either way, stick with an even-numbered LTS release for production and confirm:
node -v
npm -v
Step 3: Get your code onto the server
Clone your repository into a directory your user owns:
mkdir -p ~/apps && cd ~/apps
git clone https://github.com/yourname/yourapp.git
cd yourapp
npm ci --omit=dev
npm ci installs exactly what is in package-lock.json, which keeps production in step with what you tested. If your app has a build step (TypeScript, Next.js, a bundler), run it now, for example npm run build. Note that build tools often live in devDependencies; in that case install everything, build, and then prune, or build in CI and deploy the output.
Put configuration in environment variables, not in code. A .env file that your app loads (and that is listed in .gitignore) is the usual approach. Make sure the app listens on the port you choose and, ideally, binds to 127.0.0.1.
Step 4: Run the app with PM2
Install PM2 globally:
npm install -g pm2
Start your app:
pm2 start npm --name yourapp -- start
# or, for a plain entry file:
pm2 start app.js --name yourapp
Useful commands:
pm2 list # what's running
pm2 logs yourapp # stream logs
pm2 restart yourapp
pm2 monit # live CPU and memory view
Use an ecosystem file
For anything beyond a quick test, describe the app in ecosystem.config.js so the setup lives in version control:
module.exports = {
apps: [{
name: "yourapp",
script: "dist/server.js",
instances: 1,
env: {
NODE_ENV: "production",
PORT: 3000
},
max_memory_restart: "300M"
}]
};
Start it with pm2 start ecosystem.config.js. Setting instances to "max" with exec_mode: "cluster" runs one process per CPU core, which can help a CPU-bound app, but only if your app is stateless or stores sessions somewhere shared like Redis.
Start PM2 on boot
pm2 startup
PM2 prints a command beginning with sudo env PATH=.... Copy and run it exactly. Then save the current process list:
pm2 save
Reboot the VPS once (sudo reboot) and check pm2 list afterward to prove it comes back on its own.
Rotate logs
PM2 logs grow forever by default. Install the log rotation module:
pm2 install pm2-logrotate
Step 5: Configure Nginx as a reverse proxy
Create a server block:
sudo nano /etc/nginx/sites-available/yourapp
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
}
}
The Upgrade and Connection headers let WebSockets (Socket.IO and similar) work through the proxy. The X-Forwarded-* headers tell your app the visitor's real IP and whether they used HTTPS; in Express, set app.set('trust proxy', 1) so it reads them.
Enable and reload:
sudo ln -s /etc/nginx/sites-available/yourapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Step 6: Add HTTPS
With your domain's A record pointing at the server:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
Certbot updates the server block, adds a redirect from HTTP to HTTPS if you choose, and renews automatically.
Step 7: Ship updates
A simple, reliable deploy routine:
cd ~/apps/yourapp
git pull
npm ci --omit=dev
npm run build # if needed
pm2 reload yourapp
pm2 reload restarts processes one at a time in cluster mode, which can avoid downtime; in fork mode it behaves like a restart. Wrap these commands in a deploy.sh script, and once you are comfortable, trigger it from a CI pipeline over SSH.
Troubleshooting
- 502 Bad Gateway: the app is not running or is on a different port. Check
pm2 logsandcurl http://127.0.0.1:3000. - App works, but redirects loop under HTTPS: your app does not trust the proxy headers. Configure trust proxy.
- Crash loop after reboot: missing environment variables. Put them in the ecosystem file or make sure your
.envis loaded.
Wrapping up
To deploy a Node.js app on a VPS properly, let each tool do one job: PM2 keeps the process alive and restarts it on boot, Nginx handles the public side and HTTPS, and a short deploy script makes updates boring. Once that pattern is in place, adding a second or third app is just another PM2 entry and another server block.