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.