agyanadda@gmail.com

Deploying Node.js Apps

Advanced Node.js 1 min read Updated Dec 3, 2026 Topic 29 of 30

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 api
  • pm2 logs api, pm2 status, pm2 restart api
  • pm2 startup and pm2 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.

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which tool keeps a Node.js app running and restarts it after crashes?

Exercise 2 True / False

Nginx is commonly used as a reverse proxy in front of Node.js.

Exercise 3 Fill in the Blank

Install only production dependencies with npm ci --omit=___

Frequently Asked Questions

No. nodemon is for development: it restarts on file changes. In production use PM2, Docker or systemd, which restart on crashes and handle reboots and logs.