VPS & Nginx
Own your server — DigitalOcean/Hetzner VPS, Nginx reverse proxy, PM2 process management, and UFW firewall
Skills taught: deployment.vps, deployment.nginx, deployment.pm2, deployment.ufw, deployment.reverse-proxy
Why a VPS?
A VPS (Virtual Private Server) is a Linux VM you fully control. You install whatever you want, run any language, and it never sleeps. It's the bottom of the infrastructure ladder — more work than Railway, less complexity than AWS. Hetzner offers 2 vCPU / 4GB RAM VPS for €4.51/month (Europe datacenter). DigitalOcean's equivalent is $12/month. For apps that have outgrown free tiers or need always-on uptime without paying AWS prices, a VPS is the sweet spot.
Initial server setup
# After creating VPS (Ubuntu 22.04), SSH in as root:
ssh root@YOUR_SERVER_IP
# 1. Update everything
apt update && apt upgrade -y
# 2. Create a non-root user
adduser deploy
usermod -aG sudo deploy
# 3. Copy SSH key to new user
mkdir /home/deploy/.ssh
cp ~/.ssh/authorized_keys /home/deploy/.ssh/
chown -R deploy:deploy /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
chmod 600 /home/deploy/.ssh/authorized_keys
# 4. Disable root login and password auth
nano /etc/ssh/sshd_config
# Set: PermitRootLogin no
# Set: PasswordAuthentication no
systemctl restart ssh
# Now SSH as deploy user:
ssh deploy@YOUR_SERVER_IP
# 5. Install Node.js (via nvm)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
source ~/.bashrc
nvm install --lts
node --version # v22.x.x
# 6. Install PM2 globally
npm install -g pm2
UFW firewall
# UFW is Ubuntu's simplified iptables interface
# Install
sudo apt install ufw
# Default: deny incoming, allow outgoing
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Allow SSH (CRITICAL — do this before enabling!)
sudo ufw allow OpenSSH
# Or by port:
sudo ufw allow 22/tcp
# Allow HTTP and HTTPS
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Enable UFW (blocks everything except what you allowed)
sudo ufw enable
# Check status
sudo ufw status verbose
# IMPORTANT: Never block port 22 before SSH is allowed
# If you lock yourself out, use the VPS provider's web console
# Your Node.js app runs on port 3000 — don't expose it directly
# Nginx will proxy from port 80/443 → 3000
# So do NOT run: ufw allow 3000
Nginx as a reverse proxy
# Install Nginx
sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
# Create a site configuration
sudo nano /etc/nginx/sites-available/myapp
# --- paste the configuration below ---
# Enable the site
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
# Remove default site
sudo rm /etc/nginx/sites-enabled/default
# Test config syntax
sudo nginx -t
# Reload Nginx
sudo systemctl reload nginx
server {
listen 80;
server_name myapp.com www.myapp.com;
# Serve static files directly (fast)
location /static/ {
root /home/deploy/myapp/public;
expires 1y;
add_header Cache-Control "public, immutable";
}
# Proxy dynamic requests to Node.js
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'; # WebSocket support
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_cache_bypass $http_upgrade;
# Timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}
# After adding SSL with certbot:
# certbot --nginx -d myapp.com -d www.myapp.com
# Certbot modifies this file automatically to add SSL config
PM2 — process management
# Deploy app
cd /home/deploy/myapp
npm ci --production
npm run build
# Start with PM2 (auto-restarts on crash)
pm2 start npm --name myapp -- start
# Or use an ecosystem file for full control:
# pm2 start ecosystem.config.js
# Persist across reboots
pm2 startup # generates a systemd command — run it
pm2 save # saves the current process list
# Common PM2 commands:
pm2 status # list all processes
pm2 logs myapp # tail logs
pm2 logs myapp --lines 100 # last 100 lines
pm2 restart myapp # restart
pm2 reload myapp # zero-downtime reload (for web apps)
pm2 stop myapp # stop
pm2 delete myapp # remove from PM2
pm2 monit # interactive dashboard
module.exports = {
apps: [
{
name: 'myapp',
script: 'dist/server.js',
instances: 'max', // use all CPU cores
exec_mode: 'cluster', // Node.js cluster mode
autorestart: true,
watch: false, // don't watch files in production
max_memory_restart: '500M',
env: {
NODE_ENV: 'production',
PORT: 3000,
},
log_date_format: 'YYYY-MM-DD HH:mm:ss',
error_file: '/home/deploy/logs/myapp-error.log',
out_file: '/home/deploy/logs/myapp-out.log',
},
],
};
Tip: Use pm2 reload instead of pm2 restart in production. restart kills all processes then starts fresh — there's a brief downtime window. reload starts new workers before killing old ones (zero-downtime rolling reload). In cluster mode, PM2 keeps at least one worker alive the entire time. reload is safe to run during a deploy without any user impact.
Frequently Asked Questions
How do I deploy updates to my VPS?
The most common approach for a solo dev: SSH in, git pull, npm ci, npm run build, pm2 reload myapp. For teams, use GitHub Actions: SSH to your server from CI using a stored SSH key, run the same commands. For a more automated approach, tools like Kamal (used by Basecamp) or Deployer handle this workflow with rollback support.