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.

MERN Stack

The Ultimate Guide to Hosting a MERN Stack Application on a VPS

Mohammad Ali
March 27, 2026
4 min read
4
0
The Ultimate Guide to Hosting a MERN Stack Application on a VPS

The Ultimate Guide to Hosting a MERN Stack Application on a VPS (2026)

A comprehensive, step-by-step walkthrough for deploying production-ready applications.

Transitioning from a local development environment to a live production server is a pivotal moment for any developer. While platforms like Vercel or Heroku offer simplicity, hosting your MERN (MongoDB, Express, React, Node.js) stack on a Virtual Private Server (VPS) provides unparalleled control, better performance, and significantly lower costs as your application scales.

In this guide, we will walk through the entire journey—from an empty Linux server to a fully secured, SSL-enabled application with automated process management.


Prerequisites

  • A VPS running Ubuntu 22.04 LTS or 24.04.
  • A domain name pointing to your VPS IP address.
  • Basic familiarity with the Linux command line.

Phase 1: Initial Server Hardening

Security is paramount. Before installing our stack, we must ensure our server is up-to-date and accessible.

# Login to your VPS
ssh root@your_server_ip

# Update the local package index
sudo apt update && sudo apt upgrade -y

Consider setting up a non-root user with sudo privileges and a firewall (UFW) to only allow necessary traffic (SSH, HTTP, HTTPS).

Phase 2: Installing the Core Environment

1. Installing Node.js (via NVM)

Node Version Manager (NVM) is the preferred method as it allows you to switch between versions easily and avoids permission issues with global packages.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts

2. Installing and Configuring MongoDB

MongoDB is the "M" in our stack. We will install the official Community Edition and ensure it starts automatically on boot.

# Import the public GPG key and add the repo (specific to your Ubuntu version)
sudo apt install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod

Phase 3: Deploying Your Application Code

Clone your repository directly into the server. It is recommended to keep your application in the /var/www/ directory.

sudo mkdir -p /var/www/my-app
sudo chown $USER:$USER /var/www/my-app
cd /var/www/my-app
git clone https://github.com/your-username/your-repo.git .

Building the Frontend

Install dependencies and create the production build of your React application.

cd client
npm install
npm run build

Setting Up the Backend

Prepare your environment variables and install server-side dependencies.

cd ../server
npm install
nano .env # Populate with MONGO_URI, PORT, JWT_SECRET, etc.

Phase 4: Process Management with PM2

If your Node.js process crashes or the server reboots, your application will go down. PM2 (Process Manager 2) ensures your backend runs in the background and restarts automatically.

npm install -g pm2
pm2 start index.js --name "my-backend"
pm2 save
pm2 startup # Follow the instructions on screen

Phase 5: Nginx as a Reverse Proxy

Nginx will act as the gateway for your server, serving your static React build and routing API calls to your Express backend.

sudo apt install nginx
sudo nano /etc/nginx/sites-available/my-app

Insert the following configuration:

server {
    listen 80;
    server_name example.com www.example.com;

    # Serve static React files
    location / {
        root /var/www/my-app/client/dist;
        try_files $uri /index.html;
    }

    # Proxy API requests to Express
    location /api {
        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;
        proxy_cache_bypass $http_upgrade;
    }
}

Enable the configuration and restart Nginx:

sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Phase 6: Securing with SSL (HTTPS)

To encrypt traffic and improve SEO, use Certbot to obtain a free SSL certificate from Let's Encrypt.

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

Conclusion

Congratulations! You have successfully deployed a production-grade MERN stack application. By managing your own VPS, you now have the freedom to optimize your environment, manage scaling, and deeply understand the infrastructure that powers your code.

Happy coding!

Comments (0)

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