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

Running an app on your laptop is one thing. Keeping it up on a server that restarts, loses a connection, or gets a bad deploy is another, and that is where a clear process helps. To deploy node.js app to vps servers in a way you can repeat, you need four parts: a clean server, the app code on that server, a process manager that restarts it, and a web server in front that handles HTTPS. This guide walks through each part on Ubuntu 24.04, using PM2 and Nginx. It assumes you have a VPS with a domain name pointed at it, and a Node app that already runs with npm start on your own machine. No paid panel is needed, and each step can be checked before you move to the next one.

Start with a clean, locked-down server

Before you install Node, lock the server down. Update the packages, create a non-root user with sudo, and turn on the firewall with only SSH, HTTP, and HTTPS open. If you have not done this yet, follow the steps to set up a secure Ubuntu 24.04 VPS from scratch first, because a rushed setup here will cost you later. Disable password login for SSH and use a key, and set up automatic security updates so the base system stays patched without a weekly chore. A small app on a patched server with a firewall is far safer than a large app on a server that still allows root login with a password.

sudo apt update && sudo apt upgrade -y
sudo ufw allow OpenSSH
sudo ufw allow "Nginx Full"
sudo ufw enable
node -v
npm -v

Get the code onto the server

cd /var/www/myapp
git clone https://github.com/yourname/myapp.git .
git checkout v1.0.0
npm ci --omit=dev
cp .env.example .env
node src/index.js

Do not store secrets in Git. Keep the .env file on the server only, and keep a .env.example in the repo that lists the keys without values. If the app needs a database, set that up before the first real start, and test the connection with a short script. A deploy that starts the app before the database is ready will fail in a confusing way, so check the database first and keep the connection string in the env file, not in the code.

How to deploy node.js app to vps with PM2 keeping it alive

Node on its own will stop when the terminal closes or when the process crashes. PM2 fixes that by running the app in the background, restarting it on a crash, and starting it again after a reboot. A simple pm2 process manager setup starts with an ecosystem file that names the app, the script, the working folder, and the env file. That file is worth writing even for one app, because it records the exact start command and makes the next deploy a one-line change. Start the app with PM2, save the process list, and turn on the startup hook so PM2 returns after a reboot.

npm install -g pm2
pm2 start ecosystem.config.js
pm2 save
pm2 startup
pm2 status
pm2 logs myapp --lines 50
module.exports = {
  apps: [{
    name: "myapp",
    script: "src/index.js",
    cwd: "/var/www/myapp",
    instances: 1,
    exec_mode: "fork",
    env: { NODE_ENV: "production", PORT: 3000 }
  }]
};

Put Nginx in front of Node

Nginx should face the internet, not Node. It takes HTTPS, serves static files fast, and passes API calls to the local Node port. A nginx reverse proxy node.js setup is a short server block: listen on port 80, set the server name to your domain, and proxy requests to http://127.0.0.1:3000 with the right headers. Those headers matter, because they carry the real client IP and the original protocol to the app. Without them, the app may log the wrong IP or build the wrong URL for a redirect. If the idea is new, read what a reverse proxy in Nginx is before you edit the config, so each line has a clear purpose.

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;
  }
}

Save the file in /etc/nginx/sites-available/myapp, link it into sites-enabled, test the config with nginx -t, and reload. Test the site in a browser and check that a page loads, an API call returns JSON, and the app log shows the request. If you see a 502 error, the app may not be running or Nginx may point at the wrong port. The steps to troubleshoot a 502 bad gateway on Nginx cover the common causes, and most of them are a stopped PM2 process or a port mismatch.

Add free HTTPS with Certbot

HTTPS is not optional for a production site, and it costs nothing with Certbot. Install Certbot and the Nginx plugin, run it for your domain, and let it edit the Nginx config and set up auto-renewal. Test the renewal with a dry run, and check that the site loads over https with no mixed content warnings. For the full walkthrough, follow the guide to set up Let us Encrypt on Nginx, from install to auto-renewal. Keep port 80 open for the renewal check, and check the cert expiry date after the first run so you know the renewal timer is real.

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run
sudo nginx -t && sudo systemctl reload nginx

Deploy a new version without downtime

A safe deploy has a set order: pull the new code, install dependencies, run any build step, then restart PM2. Use pm2 reload when you want a graceful restart, and check the status and logs after the reload. If the new version fails to start, PM2 will keep the old process or log the error, and you can check out the last good tag and reload again. Keep the last good tag noted in your deploy log, because a fast return to it is the whole point of tagging releases. Test the site after each deploy with a real page load and one real action, not just a check that the process is running.

cd /var/www/myapp
git pull origin main
git checkout v1.1.0
npm ci --omit=dev
npm run build
pm2 reload myapp
curl -I https://example.com

A checklist for every deploy

A short node.js vps deployment checklist keeps a routine deploy from drifting into guesswork. Run it in order, and tick each item before you move on. The list should cover the server, the code, the process, and the web layer, because a miss in any one of them can take the site down. Keep the list in the repo or in your team docs, and update it when a new step becomes routine, such as a database migration or a cache clear.

  • Server is patched, firewall is on, and disk space is above 20 percent free.
  • Code is on the right tag, dependencies installed with npm ci, and the build passed.
  • Env file is present, with the right port, database URL, and secret keys.
  • PM2 shows the app online, logs are clean, and the health endpoint answers.
  • Nginx config passes nginx -t, HTTPS works, and a real page loads in a browser.
  • Deploy time, tag, and any migration are noted in the deploy log.

Watch logs and set up a simple backup

Keep the server tidy over time. Remove old Node versions, old PM2 logs, and old release folders on a schedule, and review the firewall rules when you add a new service. When the next deploy lands, you will pull the code, reload PM2, test a real page, and move on. That calm routine is the sign the setup works, and it is worth more than a clever deploy trick you have to relearn each time.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.