All posts

April 18, 2025

Deploying a MERN App on a VPS with NGINX, PM2 & SSL

Step-by-step: Ubuntu VPS, Node and PM2, deploying your app, NGINX as a reverse proxy, and HTTPS with Let's Encrypt (Certbot).

πŸš€ Introduction

Deploying a MERN app on a VPS can seem complex, but once you understand the flow, it's straightforward.

Here’s how I deployed my app using:

  • VPS (Ubuntu)
  • NGINX
  • PM2
  • SSL (Let’s Encrypt)

πŸ–₯️ Step 1: Setup VPS

  • Connect via SSH
ssh root@your-server-ip
  • Update system
apt update && apt upgrade

πŸ“¦ Step 2: Install Node & PM2

apt install nodejs npm
npm install -g pm2

πŸ“ Step 3: Upload Project

  • Upload files to /home/myapp/backend
  • Install dependencies
npm install

▢️ Step 4: Start App with PM2

pm2 start server.js
pm2 save
pm2 startup

🌐 Step 5: Configure NGINX

server {
    listen 80;
    server_name api.yourdomain.com;

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
    }
}

Restart NGINX:

systemctl restart nginx

πŸ”’ Step 6: Add SSL (HTTPS)

apt install certbot python3-certbot-nginx
certbot --nginx -d api.yourdomain.com

πŸ“Š Final Result

  • βœ… App live with custom domain
  • πŸ” HTTPS enabled
  • πŸ”„ Auto-restart with PM2
  • ⚑ Production-ready setup

🧠 Key Learnings

  • NGINX acts as a reverse proxy
  • PM2 ensures uptime
  • SSL is critical for security

🎯 Conclusion

Deploying your own backend gives you full control and deeper understanding of how production systems work.

If you're deploying apps or need help scaling, feel free to reach out.