Host a Node.js app on your VPS with PM2, Nginx and free SSL
Updated 27 Nov 2026
This guide puts a Node.js app online at https://app.yourdomain.co.tz, keeps it running after crashes and reboots, and adds a free SSL certificate. It's written for Ubuntu and assumes you've followed Secure a new Cloud VPS in 10 minutes.
1. Point the domain
Add an A record for app (or @) pointing to your server's IPv4 address. See Point your domain to your hosting or server.
2. Install Node.js
Install the current LTS version from NodeSource:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
node -v
3. Upload and start your app
Clone your project (or upload it with SFTP), install packages and build:
cd ~
git clone https://github.com/you/your-app.git
cd your-app
npm ci
npm run build # if your app has a build step
Make sure the app listens on a port, for example 3000, and only on 127.0.0.1.
4. Keep it running with PM2
sudo npm install -g pm2
pm2 start npm --name myapp -- start
pm2 save
pm2 startup
pm2 startup prints one command. Copy and run it. The app now restarts by itself after a crash or reboot.
Useful commands: pm2 status, pm2 logs myapp, pm2 restart myapp.
5. Put Nginx in front
sudo apt install -y nginx
sudo nano /etc/nginx/sites-available/myapp
Paste, with your own domain:
server {
listen 80;
server_name app.yourdomain.co.tz;
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-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Turn it on:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
6. Add free SSL
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d app.yourdomain.co.tz
Choose to redirect HTTP to HTTPS. The certificate renews automatically.
Deploying updates
cd ~/your-app && git pull && npm ci && npm run build && pm2 restart myapp
Troubleshooting
- 502 Bad Gateway: the app isn't running or uses a different port. Check
pm2 logs myapp. - Certbot fails: the domain doesn't point to this server yet, or port 80 is closed in the firewall (
sudo ufw allow 80,443/tcp).
Need a server? Order a Cloud VPS and get root access within minutes. For help, open a ticket and choose Technical.