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.