10 min read
How to Deploy Node.js App to Linux Server: Ubuntu Guide
Key takeaways
- Install a current Node.js LTS on Ubuntu via NodeSource rather than the outdated default apt package.
- Run your app under PM2 with pm2 startup and pm2 save so it restarts after crashes and server reboots.
- Put Nginx in front as a reverse proxy with correct proxy_set_header lines so WebSockets and visitor IPs work.
- Secure the server with SSH key-only login, a non-root deploy user, and UFW allowing only ports 22, 80, and 443.
- Deploy updates with git pull plus pm2 reload for zero-downtime restarts, wrapped in a one-command deploy script.
To deploy a Node.js app to a Linux server, install Node.js on a fresh Ubuntu VPS, run your app under PM2 with a startup script so it survives reboots, put Nginx in front as a reverse proxy, and lock the box down with UFW. This guide gives you every command and config file for that full chain, copy-paste ready.
What do you need before deploying a Node.js app to an Ubuntu server?
Four things: an Ubuntu VPS you can SSH into, a Node.js app in a git repo, SSH key access to the server, and optionally a domain name pointed at it. Any $5-tier VPS works; the distro version matters because it decides which Node.js packages are available. For installing Node itself, the NodeSource setup instructions are the standard route on Ubuntu, and we cover them step by step later.
If you would rather skip the server work entirely, a PaaS like Omeganodes gives you git push-to-deploy with automatic HTTPS, billed via MTN or Airtel mobile money. But if you want to own the box, the walkthrough at How to deploy a Node.js app with a single git push | Omeganodes PaaS pairs well with what follows. Everything else in this guide assumes a fresh Ubuntu server and root or sudo access.
How do you secure the server with SSH keys and a user account?
Before you deploy nodejs app to linux server, lock the box down. These steps take five minutes and remove the two easiest ways in: password logins and root access.
Create a deploy user
Log in as root and create a non-root user for deployments:
adduser deploy
usermod -aG sudo deploy
rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy
The rsync line copies your root SSH key to the new user so you can log in as deploy immediately. All app work from here on happens under this account.
Harden SSH
Edit /etc/ssh/sshd_config and set three lines:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
Then reload: sudo systemctl reload ssh. Test the login as deploy from a new terminal before closing your current session, or you can lock yourself out.
If you would rather skip server admin entirely, Omeganodes PaaS — push-to-deploy hosting with fair prices worldwide handles this layer for you with git push to deploy on EU servers.
How do you install Node.js on Ubuntu the right way?
Ubuntu's default apt repository ships an old Node.js release that misses modern syntax and security patches, so skip it. Use the NodeSource setup script to pin a current LTS version, or install nvm if you want per-project versions:
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs
Verify what you got with node -v and npm -v, then install PM2 globally: sudo npm i -g pm2. PM2 handles process management, startup-on-reboot, and zero-downtime reloads later in this walkthrough (see the PM2 documentation for its full command set).
If you would rather skip server setup entirely, push-to-deploy PaaS options like Introducing Omeganodes: deploy at the speed of Africa | Omeganodes PaaS handle the Node runtime for you.
How do you get your app onto the server and running?
Clone and install
Pull your code straight from GitHub on the server:
git clone https://github.com/yourname/your-app.git
cd your-app
npm install --omit=dev
If the repo is private, add a deploy key or use a personal access token so the server can read it without your password. Confirm the app starts before touching anything else:
node app.js
Visit http://YOUR_SERVER_IP:3000 in a browser. If you see your app, the hard part is done.
Environment variables
Never hardcode secrets. Create a .env file in the project root:
PORT=3000
DATABASE_URL=postgres://user:pass@host:5432/db
JWT_SECRET=change-me
Load it with node --env-file=.env app.js (Node 20+) or dotenv in your entry file. Restart the app after editing so the new values load.
If this manual chain feels tedious, a PaaS like Omeganodes does push-to-deploy from GitHub with no YAML, and you can pay with MTN or Airtel Money. Check the acceptable use rules first at Terms of Service | Omeganodes PaaS.
How do you keep a Node.js app running with PM2 after a server reboot?
A crash or reboot should not take your app offline. PM2 restarts a process if it dies, and its startup command makes Ubuntu relaunch PM2 (and your app) whenever the server boots.
Start and save the process
From your app directory, start the app and register it:
pm2 start app.js --name myapp
pm2 save
pm2 save writes the current process list to disk. Without it, PM2 has nothing to restore after a reboot, so run it every time you add or change an app.
Enable startup on reboot
pm2 startup systemd
PM2 prints one command. Copy and run it exactly as shown; it registers a systemd service that starts PM2 on boot. Reboot the server (sudo reboot) and confirm with pm2 status that your app came back up.
If you would rather never touch PM2, Nginx, or firewall rules at all, push-to-deploy platforms like Omeganodes handle this layer for you under their standard terms, which you can review in either English or Italian at Termini di servizio | Omeganodes PaaS.
How do you put Nginx in front of a Node.js app as a reverse proxy?
Your app is listening on a port like 3000, but visitors should reach it on port 80 and 443 without typing a port number. Nginx sits in front, accepts web traffic, and forwards it to your app. Install it with sudo apt install nginx, then write one server block per site.
The Nginx server block
Create /etc/nginx/sites-available/myapp with this content:
server {
listen 80;
server_name myapp.com;
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';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
}
}
The proxy_set_header lines matter more than they look. Without Upgrade and Connection, WebSockets break silently. Without X-Real-IP, your app logs Nginx's address instead of the visitor's, so rate limiting and geo features misbehave. The Nginx reverse proxy documentation covers every directive here if you need to tune timeouts or buffering.
Enable the site and reload:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
nginx -t catches typos before they take the site down. Visiting your domain should now show your app.
Automatic HTTPS with Certbot
Install Certbot and request a certificate for your domain:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d myapp.com
Certbot edits your server block to redirect HTTP to HTTPS and renews the certificate automatically. If your domain's DNS does not point at this server yet, fix that first, because Certbot verifies ownership over the public internet.
How do you secure the server with UFW?
UFW ships with Ubuntu but is disabled by default, so your server is exposed until you fix it. First allow SSH so you don't lock yourself out:
sudo ufw allow OpenSSH
Then allow Nginx on ports 80 and 443 in one rule:
sudo ufw allow 'Nginx Full'
Enable the firewall and check the result:
sudo ufw enable
sudo ufw status verbose
You should see OpenSSH and Nginx Full listed as ALLOW, and everything else blocked by default. Your Node.js app itself never needs a public port. Nginx listens on 80 and 443, then proxies to your app on localhost, so keep port 3000 (or whatever your app uses) closed. If you ever connect from a new IP and get locked out, most VPS providers offer a console you can use to disable UFW and retry.
How do you update a deployed Node.js app without downtime?
Once your app is running under PM2, updates come down to three commands: git pull, npm install --production, and pm2 reload app. The reload flag is what keeps users online: PM2 restarts workers one at a time, so old processes keep serving requests while new ones start, unlike pm2 restart, which drops every connection at once.
Wrap the steps in a small script so deploying a side project is one command:
#!/bin/bash
cd /var/www/myapp
git pull origin main
npm install --production
npm run build
pm2 reload app
Save it as deploy.sh, run chmod +x deploy.sh, and push-to-deploy becomes a single SSH command. If a build fails, the old processes are still running, so you can fix and retry without an outage. This is the same trade-off managed platforms automate for you; on your own VPS, the script is the automation.
What are the most common deployment errors and how do you fix them?
Most first deploys fail on the same handful of errors, and almost all of them trace back to one of four layers: the process manager, the proxy, the firewall, or file permissions. Before you debug anything, get precise about which layer is failing. If Nginx returns a 502, the proxy is fine and your app is the problem. If the connection times out entirely, the firewall is probably blocking you before Nginx ever sees the request.
If the app starts and then dies minutes later, look at process management and environment variables. Working top-down like this turns an hour of random guessing into a two-minute diagnosis, because each layer either passes the request along or visibly fails, and the failure mode tells you where to look. Here are the five errors you will hit most often, with the fix for each.
| Error | Cause | Fix |
|---|---|---|
| 502 Bad Gateway | Nginx is running but your app is not, or it listens on the wrong port | Check pm2 status and your app's port; make sure the Nginx proxy_pass points to it |
| EADDRINUSE | Another process already holds your port | Run sudo lsof -i :3000, kill the old PID, or change ports |
| App dies after reboot | PM2 has no startup entry | Run pm2 startup, then pm2 save after your app is running |
| Connection timed out | UFW blocks the port | Allow only 22, 80 and 443; keep the app port closed and let Nginx proxy to it |
| Permission denied (EACCES) | Binding to port 80 as a non-root user, or wrong file ownership | Keep Node on a high port behind Nginx; chown -R youruser:youruser the app directory |
Two habits prevent most of these: check pm2 logs before anything else, and never run your Node app as root. If you would rather skip this chain entirely, a push-to-deploy PaaS like Omeganodes handles PM2, Nginx and HTTPS for you.
FAQ
How much does it cost to deploy a Node.js app on a Linux server?
The software is free: Ubuntu, Node.js, PM2, Nginx, and Certbot cost nothing. Your only real expense is the VPS itself, which small providers offer for a few dollars a month, plus a domain name. If you use a PaaS instead, you trade a higher monthly fee for not managing any of the stack yourself.
Do I need Nginx or can Node.js serve traffic directly?
Technically Node can listen on port 80, but you should not. Binding low ports requires running as root, a security risk, and Node alone handles HTTPS certificates, static files, and WebSocket headers worse than Nginx. The reverse proxy keeps your app on a high port behind the firewall, lets Certbot manage HTTPS, and survives app crashes.
Is PM2 better than systemd for running Node.js in production?
For most solo developers, yes. PM2 gives you zero-downtime reloads with pm2 reload, clustered processes across CPU cores, and log management out of the box. Systemd is fine if you only need restart-on-crash and boot persistence, but its restart drops every connection during deploys, while PM2's reload keeps users online.
How do I deploy a Node.js app to Ubuntu without touching the server at all?
Use a push-to-deploy PaaS. Connect your GitHub repo, and the platform builds and runs the app on every push, handling the runtime, process management, HTTPS, and reboots for you. Omeganodes and Render both work this way. You give up shell access and fine-grained control, but for a side project the time saved over the manual PM2, Nginx, and UFW chain is substantial.
Deploy at the speed of Africa
World-class hosting, open to every developer, wherever you build and however you pay. Start free in a couple of minutes — no credit card, no lock-in.