Deploying an Express.js Website to a VPS with Nginx, PM2, and Ubuntu
This guide shows a production-friendly way to deploy an Express.js app on an Ubuntu VPS. You’ll run Node.js behind Nginx (reverse proxy) and keep it alive with PM2 (process manager), then optionally add HTTPS via Let’s Encrypt (Certbot). By the end, your app will survive reboots, serve a real domain, and be easy to update.
I. What you’re building
1. Architecture overview
Express.js listens on a private port (e.g.,
127.0.0.1:3000)PM2 runs and monitors the Node process, and can restore it on reboot pm2.keymetrics.io+1
Nginx listens on ports
80/443and proxies requests to Express (and can handle WebSockets) DigitalOcean+1Certbot can automatically configure HTTPS for Nginx and renew certificates certbot.eff.org+1
2. Prerequisites
Ubuntu VPS (20.04/22.04/24.04)
A domain name pointing to your VPS IP (recommended for HTTPS)
SSH access with sudo privileges
II. Prepare the Ubuntu VPS
1. Update packages
sudo apt update
sudo apt -y upgrade
2. Create a non-root deploy user (recommended)
sudo adduser deploy
sudo usermod -aG sudo deploy
Log out and SSH back in as deploy.
3. Enable firewall (UFW)
Allow SSH first:
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status
III. Install Node.js and build tools
1. Install Node.js
There are many valid approaches; a common production choice is NodeSource packages (or NVM if you prefer). NodeSource provides Debian/Ubuntu distributions and documentation pointers. GitHub+1
If you’re using Ubuntu’s repo:
sudo apt -y install nodejs npm
node -v
npm -v
2. Install build tools (often required for native deps)
sudo apt -y install build-essential
IV. Upload your Express.js project
1. Create the app directory
sudo mkdir -p /var/www/my-express-app
sudo chown -R $USER:$USER /var/www/my-express-app
cd /var/www/my-express-app
2. Deploy code (Git example)
sudo apt -y install git
git clone <your-repo-url> .
3. Install dependencies
If you have a lockfile, prefer npm ci:
npm ci
Production install (skip dev dependencies):
npm ci --omit=dev
4. Ensure your app respects PORT
Example server entry:
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
app.get("/", (req, res) => res.send("OK"));
app.listen(port, "127.0.0.1", () => {
console.log(`Listening on http://127.0.0.1:${port}`);
});
5. Quick local test on the VPS
NODE_ENV=production PORT=3000 node server.js
curl -I http://127.0.0.1:3000
Stop the process after verifying.
V. Run Express.js with PM2
PM2 is a process manager that keeps your app online and provides an easy CLI workflow. pm2.keymetrics.io
1. Install PM2 globally
sudo npm install -g pm2@latest
pm2 -v
2. Start the app with a stable name
If your package.json has "start": "node server.js":
cd /var/www/my-express-app
pm2 start npm --name "my-express-app" -- start
pm2 status
3. Enable PM2 startup on reboot (systemd)
PM2 can generate startup scripts for systemd (Ubuntu uses systemd). pm2.keymetrics.io+1
pm2 startup
PM2 will print a command. Copy/paste it (it usually starts with sudo env ... pm2 startup systemd ...).
Then persist the process list:
pm2 save
4. Handy PM2 operations
pm2 logs my-express-app
pm2 restart my-express-app
pm2 reload my-express-app
pm2 stop my-express-app
VI. Install and configure Nginx as a reverse proxy
1. Install Nginx
sudo apt -y install nginx
sudo systemctl enable --now nginx
Allow HTTP/HTTPS traffic:
sudo ufw allow "Nginx Full"
sudo ufw status
2. Create an Nginx server block
Create:
sudo nano /etc/nginx/sites-available/my-express-app
Example configuration:
server {
listen 80;
server_name example.com www.example.com;
# Optional: serve static files directly via Nginx
# root /var/www/my-express-app/public;
location / {
proxy_pass http://127.0.0.1:3000;
# Preserve host + client information
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;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Nginx’s official documentation describes WebSocket proxying and the HTTP/1.1 upgrade mechanism. nginx.org
Enable the site:
sudo ln -s /etc/nginx/sites-available/my-express-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Test:
curl -I http://example.com
VII. Add HTTPS with Let’s Encrypt (Certbot)
1. Install Certbot and obtain a certificate
The EFF’s Certbot instructions for Nginx cover installation and usage on Ubuntu. certbot.eff.org
Run:
sudo certbot --nginx
DigitalOcean also notes that --nginx allows Certbot to analyze and modify Nginx server blocks to enable TLS and optional HTTP→HTTPS redirect. DigitalOcean
2. Test auto-renewal
sudo certbot renew --dry-run
VIII. Deployment workflow (safe and repeatable)
1. Typical update routine
cd /var/www/my-express-app
git pull
npm ci --omit=dev
pm2 reload my-express-app
2. Keep Node private
Express should bind to
127.0.0.1(not0.0.0.0)Do not open port
3000in your firewallOnly Nginx is public (ports 80/443)
IX. Troubleshooting
1. Nginx shows “502 Bad Gateway”
Common causes:
App isn’t running
App is listening on the wrong port/IP
Nginx proxies to the wrong upstream
Check:
pm2 status
pm2 logs my-express-app
sudo tail -n 200 /var/log/nginx/error.log
2. WebSockets (Socket.IO) don’t connect
Make sure you included:
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
Socket.IO’s documentation includes reverse proxy configuration examples for Nginx. socket.io
Conclusion
Deploying Express.js on an Ubuntu VPS with Nginx and PM2 is a strong baseline for production: Nginx handles the public-facing layer (domain, TLS, WebSockets, buffering), while PM2 keeps your Node process stable and restores it after reboots. Once this is working, you can evolve into CI/CD, multi-instance scaling, or separating the database onto its own server.
References
https://pm2.keymetrics.io/docs/usage/quick-start/
https://pm2.keymetrics.io/docs/usage/startup/
https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps
https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-reverse-proxy-on-ubuntu-22-04
https://nginx.org/en/docs/http/websocket.html
https://socket.io/docs/v4/reverse-proxy/
https://certbot.eff.org/instructions?os=ubuntufocal&ws=nginx
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04
https://github.com/nodesource/distributions