Besharamcode

Deploying a MERN Stack Application on AWS from Scratch (Step-by-Step Guide)

4/12/2025, 6:20:32 PM

Mohit Kushwah

Learn how to deploy a MERN Stack application (MongoDB, Express.js, React, Node.js) on AWS EC2 from scratch. Step-by-step tutorial with best practices for performance, security, and scalability.

Introduction

Are you ready to take your MERN Stack app live and want a reliable, scalable solution? AWS (Amazon Web Services) offers powerful cloud infrastructure perfect for hosting full-stack applications.

In this blog post, we’ll walk you through how to deploy a MERN Stack application on AWS EC2 from scratch. Whether you’re a beginner or an intermediate developer, this guide will help you get your app live and running with proper configurations.

What is the MERN Stack?

The MERN Stack is a popular JavaScript-based tech stack comprising:

  • MongoDB – NoSQL database
  • Express.js – Web framework for Node.js
  • React.js – Frontend library
  • Node.js – Runtime environment for executing JavaScript on the server

Prerequisites

Before starting, make sure you have:

  • A MERN Stack application ready
  • An AWS account
  • Basic knowledge of Linux commands
  • A domain name (optional but recommended)

Step 1: Create an EC2 Instance

  • Log in to your AWS Management Console
  • Go to EC2 > Instances > Launch Instance
  • Choose an Amazon Linux 2 AMI (or Ubuntu)
  • Select an instance type (e.g., t2.micro for free tier)
  • Configure storage and security group
  • Allow inbound rules for SSH (22), HTTP (80), and HTTPS (443)
  • Launch the instance and download your .pem key

Step 2: Connect to the EC2 Instance

Open your terminal and run:

chmod 400 your-key.pem
ssh -i "your-key.pem" ec2-user@your-ec2-public-ip

You’re now connected to your server!

Step 3: Install Node.js, NPM, and Git

sudo yum update -y   # for Amazon Linux
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs git

For Ubuntu:

sudo apt update && sudo apt install nodejs npm git -y

Step 4: Install and Configure MongoDB

You have two options:

Option 1: Use MongoDB Atlas (Recommended)

  • Go to MongoDB Atlas
  • Create a free cluster
  • Whitelist your EC2 IP
  • Create a user and copy the connection URI

Option 2: Install MongoDB Locally on EC2

sudo yum install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod

Step 5: Clone Your MERN App

git clone https://github.com/your-username/your-mern-app.git
cd your-mern-app

Step 6: Setup Backend (Express + Node)

  • Navigate to the backend folder
  • Configure your .env file with MongoDB URI and PORT
  • Install dependencies:
cd backend
npm install
  • Start the server:
node server.js
Tip: Use PM2 to keep the backend running:
npm install -g pm2
pm2 start server.js
pm2 startup
pm2 save

Step 7: Setup Frontend (React)

  • Navigate to the frontend folder
  • Install dependencies:
cd ../frontend
npm install
npm run build
  • Install Nginx:
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
  • Configure Nginx:
sudo nano /etc/nginx/nginx.conf

Replace the default config with:

server {
    listen 80;
    server_name your-domain.com;

    root /home/ec2-user/your-mern-app/frontend/build;
    index index.html index.htm;

    location / {
        try_files $uri /index.html;
    }

    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;
    }
}

Then:

sudo nginx -t
sudo systemctl restart nginx

Step 8: (Optional) Add a Domain & Enable HTTPS

  • Point your domain to the EC2 public IP (via DNS settings)
  • Install Certbot:
sudo yum install -y certbot python3-certbot-nginx
sudo certbot --nginx
  • Follow prompts to install your free SSL certificate

Step 9: Security Best Practices

  • Enable UFW or configure your AWS security groups strictly
  • Avoid running apps as root
  • Use PM2 for process management
  • Backup your database regularly
  • Keep your dependencies updated

Conclusion

You’ve successfully deployed a full MERN Stack application on AWS EC2 — from setting up the server, configuring Node and MongoDB, to serving the frontend via Nginx.

💡 If you’re planning on scaling, consider moving to Docker, Elastic Beanstalk, or Kubernetes (EKS).

Final Thoughts

Deploying a MERN app on AWS is a valuable skill in today’s cloud-first development world. If you found this tutorial helpful, share it with your dev friends, or drop your questions in the comments!

💬 Have questions or need help? Drop a comment below or reach out — we’re here to support your dev journey!

Blog Visual

JWT vs Session: What's Best for Authentication in MERN:

How to Secure Your Express.js Application from Common Vulnerabilities:

Upload files in Express js:

Leave a Comment

Comments: