Featured

How to Deploy MERN Stack App on VPS

T
Team
·12 min read
#mern#vps#deployment#pm2#nginx#ssl

How to Deploy MERN Stack App on VPS


Deploying a MERN app (MongoDB, Express, React, Node.js) on a VPS gives you full control and low cost. This guide walks from server setup to production.


Beginner: VPS and Access


1. Provision a VPS – Any provider (DigitalOcean, Linode, Vultr, etc.). Ubuntu 22.04 LTS is a solid choice.

2. SSH accessssh root@your-server-ip. Create a non-root user and use SSH keys; disable password login for root.

3. Basicsapt update && apt upgrade -y; set hostname and firewall (ufw: allow 22, 80, 443).


Intermediate: Node, MongoDB, and App


  • Node.js – Install via NodeSource or nvm: `curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - && apt install -y nodejs`.
  • MongoDB – Follow official Ubuntu docs; create a DB user and enable auth. Bind to localhost or private IP only.
  • App – Clone your repo; `npm ci --production`; set `NODE_ENV=production` and env vars (DB URL, JWT secret, etc.). Build the React app (`npm run build`) and serve the built files from Express or a static server.

  • bash
    1cd /var/www/myapp
    2git pull
    3npm ci --production
    4npm run build

    Advanced: PM2 and Nginx


  • PM2 – Run the Node server with `pm2 start server.js -i max` (cluster mode). `pm2 startup` and `pm2 save` for restart on reboot.
  • Nginx – Reverse proxy to Node (e.g. `proxy_pass http://127.0.0.1:3000`); serve static build from Express or Nginx. Add `proxy_http_version 1.1` and `proxy_set_header Upgrade $http_upgrade` if you use WebSockets.
  • SSL – `certbot --nginx` for Let’s Encrypt. Auto-renew with certbot’s timer.

  • Expert: CI/CD and Hardening


  • CI/CD – GitHub Actions or GitLab CI: run tests, build, then deploy via SSH (rsync + pm2 reload) or a small deploy script. Never commit secrets; use repo or server secrets.
  • Hardening – Fail2ban for SSH; keep OS and Node updated; run app as a non-root user; restrict MongoDB network access.

  • Validate configs and API payloads before deploy with our [JSON Formatter](/tools/json-formatter/) and [YAML Formatter](/tools/yaml-formatter/).


    Related tools

    Try these free developer tools from Codev Nexus.

    Enjoyed this article?

    Support our work and help us create more free content for developers.

    Stay Updated

    Get the latest articles and updates delivered to your inbox.

    How to Deploy MERN Stack App on VPS - Codev Nexus | codev nexus