The short version: Node on localhost, Nginx in front, a process manager keeping it alive
To deploy a Node.js app on a VPS, run the app on a local port such as 127.0.0.1:3000, keep it alive with PM2 or a systemd service so it restarts after crashes and reboots, and put Nginx in front on ports 80 and 443 as a reverse proxy. Certbot then adds a free Let's Encrypt certificate to Nginx, so visitors reach the app over HTTPS.
That gives three layers. Node.js runs your code (Express, Fastify, Next.js, NestJS or a plain http server). PM2 or systemd starts it at boot and restarts it if it dies. Nginx faces the internet, terminates HTTPS, can serve static files and passes everything else to Node. Node itself never listens on a public interface.
The steps below assume a fresh Ubuntu 24.04 LTS server that has been through the Ubuntu VPS initial setup (linked at the end of this guide), a sudo user called deploy for administration, and a domain whose A record already points at the server. The app itself runs as a separate user without sudo rights, created in the next section. Replace myapp and example.in with your own names.
Quick checklist
- Install the current Node.js LTS from NodeSource or with nvm, and create an app user without sudo rights.
- Clone the app as that user, install dependencies and make it listen on
127.0.0.1. - Keep secrets in an environment file that only the right user can read.
- Run the app under PM2 or a systemd unit, and check it survives a reboot.
- Add an Nginx server block with the WebSocket headers.
- Open ports 80 and 443, then run Certbot.
Install Node.js LTS and create an app user
Production servers should run an LTS release. On 23 November 2026 the Node.js releases page lists Node.js 26 (codename Lithium) as the Active LTS line, supported until April 2029. Node.js 24 is in maintenance and Node.js 22 is still supported, so either is acceptable for an existing app, but start new deployments on 26. Ubuntu 24.04's own nodejs package is Node.js 18, which is end-of-life upstream, so do not use it.
| Method | Where Node lives | Good for |
|---|---|---|
| NodeSource apt repository | /usr/bin/node, for every user | One app per server, updates through apt, simple systemd units |
| nvm | ~/.nvm, for one user only | Several apps that need different Node versions |
Option 1: NodeSource
curl -fsSL https://deb.nodesource.com/setup_26.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y nodejs git
node -v
The script adds NodeSource's signed apt repository for the 26.x line. Read it before running it if you prefer, and later Node.js updates within that line arrive with sudo apt upgrade.
Option 2: nvm
nvm installs Node for one user only, so create the app user first (next step) and run these commands as that user, after sudo -iu myapp:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.41.0/install.sh | bash
source ~/.bashrc
nvm install 26
node -v
v0.41.0 is the latest nvm release at the time of writing. Because nvm installs Node inside your home directory, systemd units and cron jobs must use the full path to the binary, which which node prints.
Create an app user, clone the app and bind it to localhost
Do not run the app as deploy, which is in the sudo group. Create a user with no password and no sudo rights, so a compromised Node process cannot use sudo, and switch to it:
sudo adduser --disabled-password --gecos "" myapp
sudo -iu myapp
Then, as myapp, clone the app into /home/myapp/myapp:
cd ~
git clone https://github.com/you/myapp.git
cd myapp
npm ci --omit=dev
npm ci installs exactly what package-lock.json says. If the app needs a build step (TypeScript, Next.js), run npm ci without --omit=dev, then npm run build. If you use pnpm instead, the npm vs pnpm guide linked at the end has the equivalent commands.
Make the app listen on the loopback address so it cannot be reached directly from the internet. In Express that is:
const port = process.env.PORT || 3000;
app.listen(port, '127.0.0.1', () => console.log(`listening on ${port}`));
Test it with node server.js as myapp, then from a second SSH session run curl -I http://127.0.0.1:3000.
Environment variables and secrets
Never commit database passwords, JWT secrets or API keys to Git. Keep them in a file on the server, one KEY=value per line:
NODE_ENV=production
PORT=3000
MONGODB_URI=mongodb://127.0.0.1:27017/myapp
JWT_SECRET=change-me
Where the file lives depends on the process manager:
- systemd: as
deploy, create the directory and the file, and make it readable by root only:sudo mkdir -p /etc/myapp && sudo nano /etc/myapp/env && sudo chmod 600 /etc/myapp/env. systemd reads it as root and passes the values to your process throughEnvironmentFile=. - PM2: PM2 runs as the app user, so as
myappsave the file as/home/myapp/myapp/.env, runchmod 600 .env, add.envto.gitignore, and load it at the top of your entry file with Node's built-in loader:process.loadEnvFile('/home/myapp/myapp/.env');
Node.js has supported .env files natively since 20.6 (--env-file) and 20.12 (process.loadEnvFile()), and both stopped being experimental in 24.10, so the dotenv package is optional on current versions. Values already set in the real environment take precedence over the file.
Keep it running: PM2 or a systemd unit
Running node server.js in an SSH session ties the process to that session, and it does not restart after a crash or a reboot. Pick one of the two tools below. Do not use both for the same app.
| PM2 | systemd unit | |
|---|---|---|
| Install | sudo npm install -g pm2 | Already part of Ubuntu |
| Start at boot | pm2 startup then pm2 save | systemctl enable |
| Several processes on all cores | Built-in cluster mode, -i max | Your own code, or several units |
| Reload without dropping requests | pm2 reload in cluster mode | Not built in; a restart has a short gap |
| Logs | ~/.pm2/logs, pm2 logs | The journal, journalctl -u myapp |
Option A: PM2
Install PM2 as deploy, then start the app as myapp:
sudo npm install -g pm2@latest
sudo -iu myapp
cd ~/myapp
pm2 start server.js --name myapp
pm2 startup
pm2 startup detects systemd and prints one command that begins with sudo env PATH=. Because myapp has no sudo rights, type exit to return to deploy and run that line there: it installs a boot service for the myapp user. Then switch back with sudo -iu myapp and save the current process list so it is restored at boot:
pm2 save
Reboot once with sudo reboot and check pm2 status afterwards. Run pm2 save again whenever you add or remove apps. After upgrading Node.js, PM2's docs say to run pm2 unstartup and then pm2 startup again, so the boot service points at the new binary. With nvm, install PM2 as myapp without sudo.
Option B: a systemd unit
Create /etc/systemd/system/myapp.service:
[Unit]
Description=myapp Node.js server
After=network.target
[Service]
User=myapp
Group=myapp
WorkingDirectory=/home/myapp/myapp
EnvironmentFile=/etc/myapp/env
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
With nvm, replace /usr/bin/node with the full path that which node prints for myapp. Then, as deploy, start it and enable it at boot:
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
systemctl status myapp
Nginx reverse proxy, WebSocket headers and HTTPS
Install Nginx with sudo apt install -y nginx and create /etc/nginx/sites-available/myapp:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name example.in www.example.in;
client_max_body_size 10m;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $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;
}
}
Upgrade and Connection are hop-by-hop headers, so Nginx does not pass them on unless you set them, and without them Socket.IO and other WebSocket connections fail. Nginx's documentation notes that proxy_http_version 1.1 is needed before version 1.29.7. Ubuntu 24.04 ships an older Nginx, so keep the line. Nginx also closes a proxied WebSocket that sends nothing for 60 seconds by default, so either send pings from the app or raise proxy_read_timeout. In Express, add app.set('trust proxy', 1) so req.ip and req.protocol reflect the visitor rather than Nginx.
Enable the site, remove the default one, test and reload:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx
Open ports 80 and 443 in every firewall in front of the server. With UFW, allow SSH first so you do not lock yourself out, and do not open port 3000:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
Then install Certbot from its snap, as Certbot's instructions recommend, and let it edit the Nginx block. If certbot was installed with apt, remove it first with sudo apt remove certbot. snapd must be installed; it is on standard Ubuntu 24.04 server images.
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/local/bin/certbot
sudo certbot --nginx -d example.in -d www.example.in
sudo certbot renew --dry-run
Certbot adds the certificate, redirects HTTP to HTTPS and sets up automatic renewal. Port 80 must stay open for validation. For when a paid certificate is worth it, see free vs paid SSL.
MERN extras: the React build and where MongoDB runs
Build the React front end once, then serve files
Do not run the React development server in production. Build static files with npm run build. Vite writes them to dist/ by default. Copy them to a folder Nginx can read, such as /var/www/myapp, because Ubuntu home directories are not readable by other users. As deploy, copy the build with sudo mkdir -p /var/www/myapp && sudo cp -r /home/myapp/myapp/client/dist/. /var/www/myapp/, adjusting the path to where your front end lives. Then let Nginx serve the files and send only API and Socket.IO traffic to Node. Replace the earlier location / block with the following, inside the same server block; two location / blocks make nginx -t fail with a duplicate location error:
root /var/www/myapp;
location / {
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:3000;
# same proxy_set_header lines as above
}
location /socket.io/ {
proxy_pass http://127.0.0.1:3000;
# same proxy_set_header lines, including Upgrade and Connection
}
The try_files fallback lets client-side routes such as /orders/42 load index.html instead of returning 404. Express can serve the build with express.static instead, but Nginx handles static files without involving Node.
MongoDB: Atlas or on the VPS
- MongoDB Atlas is the managed service. Atlas only accepts connections from addresses in the project's IP access list, so add your VPS's public IPv4 address there and keep the connection string in the environment file. Choose a region close to the VPS, because every query crosses the network.
- MongoDB on the VPS keeps data next to the app and costs nothing extra, but backups, updates and memory are your job. Install it from MongoDB's own repository using its Ubuntu guide, connect over
127.0.0.1:27017, and never open port 27017 in the firewall. Check that the plan has enough RAM for MongoDB alongside Node.js before you install it.
Zero-downtime deploys and logs
A simple deploy with PM2, run as myapp, looks like this:
cd ~/myapp
git pull
npm ci --omit=dev
pm2 reload myapp
If the app has a build step (TypeScript, Next.js), run npm ci without --omit=dev and then npm run build before pm2 reload, or the reload serves stale or missing compiled files. For a React front end, rebuild it and copy dist/ to /var/www/myapp again.
pm2 reload replaces processes one at a time, but only in cluster mode. The app started earlier runs in fork mode, and running the start command again with -i max only reports that it is already running, so switch it over once and save the new process list:
pm2 delete myapp && pm2 start server.js --name myapp -i max && pm2 save
PM2's docs say the app must be stateless for this: keep sessions and caches in Redis or the database, not in process memory. In fork mode, and with a single systemd service, a restart leaves a short gap during which Nginx returns 502. Many small apps accept that. If yours cannot, use PM2 cluster mode or run two instances behind an Nginx upstream and restart them one after the other.
Where to look when something breaks:
- PM2:
pm2 logs myapp, files in~/.pm2/logs. Install thepm2-logrotatemodule withpm2 install pm2-logrotateso the files do not fill the disk. - systemd:
sudo journalctl -u myapp -f - Nginx:
sudo tail -f /var/log/nginx/error.log
Common errors and their fixes
| Symptom | Likely cause | Fix |
|---|---|---|
502 Bad Gateway, Nginx log says connect() failed (111: Connection refused) | The app is not running, crashed at start-up, or listens on a different port | Check pm2 logs or journalctl -u myapp; match the port in proxy_pass |
Error: listen EADDRINUSE: address already in use 127.0.0.1:3000 | Another process already holds the port, often an old copy started by hand or both PM2 and systemd running the app | Find it with sudo ss -ltnp | grep 3000, stop it, and use only one process manager |
| App stops when you close the SSH window | It was started with node server.js in the session | Run it under PM2 or systemd as shown above |
| App is gone after a reboot under PM2 | pm2 startup line not run, or pm2 save forgotten | Run both, then reboot to test |
Error: listen EACCES: permission denied 0.0.0.0:80 | Ports below 1024 need root privileges on Linux | Keep Node on 3000 and let Nginx listen on 80 and 443; do not run Node as root |
| WebSocket or Socket.IO keeps falling back or reconnecting | Upgrade and Connection headers not set | Add the map and the two headers, then reload Nginx |
npm error code EACCES during a global install | Global npm folder belongs to root with NodeSource | Use sudo for global tools, or nvm; see clearing the npm cache for cache errors |
For a wider list of 502 causes, see the 502 Bad Gateway guide linked at the end.
Running Node.js on Bigdomainhost
Every step above works as written on the Ubuntu 24.04 image of a Bigdomainhost KVM VPS. The VPS control panel has its own firewall, and by default it only lets SSH (port 22) in. Open 80 and 443 there as well as in UFW, or Nginx and Certbot's HTTP check will time out (see the networking docs). If you lock yourself out with UFW, log in through the panel's out-of-band console and run sudo ufw allow OpenSSH.
Servers are in Mumbai (BOM-1) and Delhi NCR (DEL-1) and are prepaid in 30-day periods. The current price is on the pricing page. Support is on WhatsApp at +91 75994 50220 at any time, and by phone in IST working hours.
This guide was written with AI assistance and checked against the official documentation listed in its sources on 23 November 2026.
Sources (checked )
- Node.js: Node.js releases
- NodeSource: Node.js binary distributions
- PM2: Startup script
- PM2: Cluster mode
- nginx: WebSocket proxying
- Certbot (EFF): Certbot instructions: Nginx on Linux (snap)