Guideschevron_rightServers

Servers · 8 min read · Updated

How to Deploy a Node.js App on a VPS with PM2 and nginx

To deploy a Node.js app on a VPS: install a current Node.js, put the app in its own user's home, install dependencies and build it, run it with PM2 so it restarts on crashes and reboots, then put nginx in front with a free Let's Encrypt certificate. The app listens only on 127.0.0.1; nginx serves the public HTTPS site. The same steps work for Express, Next.js, NestJS and other frameworks.

1. Install Node.js and nginx

On Ubuntu 24.04, after securing the server:

curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs nginx
node -v && npm -v

2. Create a User and Deploy the Code

sudo useradd -m -s /bin/bash app
sudo -u app -i
git clone https://github.com/you/your-app.git ~/app
cd ~/app
npm ci
npm run build        # if your app has a build step

Put settings and secrets in ~/app/.env (with chmod 600), not in the repository. Make sure the app listens on 127.0.0.1 and a port such as 3000; most frameworks take a PORT variable.

3. Run It With PM2

sudo npm install -g pm2

# as the app user, in ~/app
pm2 start npm --name web -- start
pm2 logs web             # check it started
pm2 save                 # remember the process list

Then make PM2 start at boot. Run this as the app user and paste the command it prints:

pm2 startup systemd
# prints: sudo env PATH=... pm2 startup systemd -u app --hp /home/app

To use every core, run several instances in cluster mode instead: pm2 start dist/server.js -i max --name web (cluster mode needs a JavaScript entry file rather than npm start).

4. Put nginx in Front

Point your domain's A record at the server, then create /etc/nginx/sites-available/app:

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 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;     # WebSockets
        proxy_set_header Connection "upgrade";
    }
}
sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

5. Add HTTPS

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

Certbot edits the nginx file for HTTPS and renews the certificate automatically. If validation fails, fixing Certbot validation errors goes through the causes. Allow only SSH, 80 and 443 in the firewall.

6. Deploy Updates

sudo -u app -i
cd ~/app && git pull && npm ci && npm run build
pm2 reload web           # restarts without dropping requests in cluster mode

Put those lines in a script, or run them from a self-hosted GitHub Actions runner on the same server for push-to-deploy. Prefer a platform that does all of this for you? Coolify on a VPS builds and deploys from Git with a web interface.

Common Questions

  • PM2 or systemd?

    Either keeps a Node.js app running. PM2 adds cluster mode across cores, zero-downtime reloads and its own log commands, which suits Node.js apps; systemd needs nothing extra and runs any language. This guide uses PM2; the systemd service guide covers the other way.

  • Why put nginx in front of Node.js?

    nginx handles HTTPS certificates, serves static files quickly, buffers slow clients and lets several apps share ports 80 and 443 by domain name. The Node.js app listens only on the server itself, so it is never exposed directly.

  • How much memory does a Node.js app need?

    A small API or Next.js site often runs in 150 to 400 MB per process. PM2's cluster mode starts one process per core, so multiply by the number of instances, and leave room for the build step, which can briefly need more.

  • Does this work for Next.js, Express and NestJS?

    Yes. Any app started with a command such as npm start works the same way: PM2 runs the command, nginx forwards requests to the port it listens on.

Plans and prices

VPS Plans

KVM servers with NVMe storage, full root access and a fixed IPv4 address, built in minutes.

See all plans
  • Bronze

    3 vCPU · 6 GB RAM · 75 GB NVMe

    2 TB transfer

    $37.50/mo

    Configure inAshburnDallas
  • Silver

    4 vCPU · 8 GB RAM · 100 GB NVMe

    4 TB transfer

    $46.50/mo

    Configure inAshburnDallas
  • Gold

    6 vCPU · 12 GB RAM · 150 GB NVMe

    6 TB transfer

    $60.00/mo

    Configure inAshburnDallas
  • Diamond

    8 vCPU · 16 GB RAM · 200 GB NVMe

    8 TB transfer

    $73.50/mo

    Configure inAshburnDallas

Network port: 10 Gbps in Ashburn, VA, 50 Gbps in Dallas, TX. Pick the location when you configure.

Keep Reading

All guides