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.
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).
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
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
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 .
Install dependencies and create the production build of your React application.
cd client
npm install
npm run build
Prepare your environment variables and install server-side dependencies.
cd ../server
npm install
nano .env # Populate with MONGO_URI, PORT, JWT_SECRET, etc.
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
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
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
No comments yet. Be the first to share your thoughts!