Ali.
HomeAboutSkillsProjectsBlogContact
Ali.

Full-stack developer passionate about creating beautiful, functional, and user-centered digital experiences.

Quick Links

  • Home
  • Projects
  • Blog
  • Contact

Services

  • Web Development
  • Frontend Development
  • Backend Development
  • UI/UX Design
  • Mobile App Development

Stay Updated

Subscribe to my newsletter for the latest updates and articles

© 2026 Mohammad Ali. All rights reserved.

REST API

Complete A to Z Guide: Deploy MERN Stack Application on VPS

Md. Ali
March 30, 2026
4 min read
1
0
Complete A to Z Guide: Deploy MERN Stack Application on VPS

Deploying a MERN stack application (MongoDB, Express, React, Node.js) is one of the most important steps in making your project production-ready.

Many developers can build applications, but deployment is where real engineering starts. In this guide, I will explain everything step by step in simple English.

By the end of this article, you will be able to:

  • Set up a VPS server
  • Deploy backend (Node + Express)
  • Deploy frontend (React)
  • Configure Nginx
  • Setup PM2 process manager
  • Add domain and SSL

What is MERN Stack?

MERN stands for:

  • MongoDB → Database
  • Express → Backend framework
  • React → Frontend
  • Node.js → Runtime

All technologies use JavaScript, which makes development faster and consistent :contentReference[oaicite:1]{index=1}


Architecture Overview

Before deployment, understand how your app works:

  • Frontend (React) → Runs in browser
  • Backend (Node API) → Runs on server
  • Database (MongoDB) → Stores data
  • Nginx → Reverse proxy

Step 1: Buy VPS

You can use any provider:

  • DigitalOcean
  • Hostinger
  • AWS EC2

Choose Ubuntu 22.04 or 24.04 server.


Step 2: Connect to Server (SSH)

ssh root@your_server_ip

This will connect your local machine to VPS securely.


Step 3: Update Server

sudo apt update && sudo apt upgrade -y

Step 4: Install Node.js

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs

Check version:

node -v
npm -v

Step 5: Install MongoDB

sudo apt install mongodb -y
sudo systemctl start mongod
sudo systemctl enable mongod

MongoDB will store your application data :contentReference[oaicite:2]{index=2}


Step 6: Project Structure

project/
  backend/
  frontend/

Step 7: Deploy Backend

Clone Project

git clone https://github.com/your-repo.git
cd backend

Install Dependencies

npm install

Create .env file

PORT=5000
MONGO_URI=your_mongodb_url
JWT_SECRET=your_secret

Start Server

node server.js

Step 8: Use PM2 (Important)

PM2 keeps your app running even if server crashes.

npm install -g pm2
pm2 start server.js --name backend
pm2 save
pm2 startup

Step 9: Deploy Frontend

cd ../frontend
npm install
npm run build

React build creates static files.


Step 10: Install Nginx

sudo apt install nginx -y

Step 11: Configure Nginx

sudo nano /etc/nginx/sites-available/default
server {
  listen 80;
  server_name yourdomain.com;

  location / {
    root /var/www/frontend/build;
    index index.html;
    try_files $uri /index.html;
  }

  location /api {
    proxy_pass http://localhost:5000;
  }
}

Nginx works as reverse proxy and serves frontend efficiently :contentReference[oaicite:3]{index=3}


Step 12: Restart Nginx

sudo systemctl restart nginx

Step 13: Domain Setup

Go to your domain DNS panel and add:

  • A Record → your server IP

Step 14: SSL Setup (HTTPS)

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com

Now your site is secure (HTTPS).


Step 15: Production Optimization

  • Use environment variables
  • Enable gzip in Nginx
  • Use MongoDB Atlas for scalability
  • Cache static files

Optimizing frontend, backend and database is important before deployment :contentReference[oaicite:4]{index=4}


Step 16: Security Best Practices

  • Disable root login
  • Use firewall (ufw)
  • Use strong SSH keys
  • Keep server updated

Step 17: Monitoring

pm2 logs
pm2 monit

This helps track errors and performance.


Step 18: Scaling Strategy

  • Use load balancer
  • Use Docker
  • Use CI/CD pipeline

Modern deployment often uses Docker and automation tools :contentReference[oaicite:5]{index=5}


Common Mistakes

  • Not using PM2
  • Wrong Nginx config
  • Environment variables missing
  • Port mismatch

Final Thoughts

Deploying a MERN stack app may look difficult at first, but once you understand the steps, it becomes very simple.

The key idea is:

  • Backend runs on Node
  • Frontend is static build
  • Nginx connects everything

Once you master this, you can deploy any full stack application confidently.

Practice this process multiple times and try automation with CI/CD.


Conclusion

This was a complete A to Z guide for deploying a MERN stack application. If you follow all steps carefully, your application will be live, secure, and production-ready.

Keep building. Keep deploying. That is how you grow as a developer.

Comments (0)

No comments yet. Be the first to share your thoughts!