Where to host
- VPS (Hostinger, DigitalOcean, AWS EC2) — full control; you set up Node, PM2 and Nginx
- Platforms (Render, Railway, Fly.io) — connect Git and they deploy for you
- Hostinger / cPanel Node.js hosting — some shared plans include a Node.js app manager
Prepare the app
- Read the port from
process.env.PORT NODE_ENV=production- Install with
npm ci --omit=dev - Build step if needed (
npm run build)
PM2 — keep the app running
PM2 restarts the app if it crashes and starts it after a server reboot:
pm2 start src/index.js --name apipm2 logs api,pm2 status,pm2 restart apipm2 startupandpm2 save— start on boot-i max— cluster mode, one process per CPU core
Nginx reverse proxy
Nginx listens on ports 80/443 and forwards to Node on port 3000. It also handles HTTPS (free certificates with Certbot / Let's Encrypt), compression and static files.
Updating
git pull && npm ci --omit=dev && pm2 reload api — reload restarts with zero downtime in cluster mode.
Example
Example — VPS setup with PM2 and Nginx
Bash
# On the server (Ubuntu)
cd /var/www/my-api
git pull origin main
npm ci --omit=dev
# Start and keep it running
npm install -g pm2
pm2 start src/index.js --name my-api -i max
pm2 save
pm2 startup # run the command it prints
# /etc/nginx/sites-available/my-api
# server {
# server_name api.example.com;
# location / {
# proxy_pass http://127.0.0.1:3000;
# proxy_http_version 1.1;
# proxy_set_header Upgrade $http_upgrade; # needed for WebSockets
# proxy_set_header Connection 'upgrade';
# proxy_set_header Host $host;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# }
# }
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d api.example.com # free HTTPS
The API runs under PM2 on port 3000, restarts on crash or reboot, and is reached at https://api.example.com through Nginx with a free SSL certificate.