Besharamcode

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

about 1 month ago

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:

  • ngoDB – NoSQL database
  • press.js – Web framework for Node.js
  • act.js – Frontend library
  • de.js – Runtime environment for executing JavaScript on the server

Prerequisites

Before starting, make sure you have:

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

Step 1: Create an EC2 Instance

  • g in to your AWS Management Console
  • to EC2 > Instances > Launch Instance
  • oose an Amazon Linux 2 AMI (or Ubuntu)
  • lect an instance type (e.g., t2.micro for free tier)
  • nfigure storage and security group
  • low inbound rules for SSH (22), HTTP (80), and HTTPS (443)
  • unch 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)

  • to MongoDB Atlas
  • eate a free cluster
  • itelist your EC2 IP
  • eate 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)

  • vigate to the backend folder
  • nfigure your .env file with MongoDB URI and PORT
  • stall dependencies:
cd backend
npm install
  • art 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)

  • vigate to the frontend folder
  • stall dependencies:
cd ../frontend
npm install
npm run build
  • stall Nginx:
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
  • nfigure 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

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

Step 9: Security Best Practices

  • able UFW or configure your AWS security groups strictly
  • oid running apps as root
  • e PM2 for process management
  • ckup your database regularly
  • ep 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!

A 2D vector graphic showing the deployment of a MERN Stack application on AWS.

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:

NOTE: This blog post was created with the assistance of AI tools to help structure content, clarify concepts, and speed up writing. However, all topics, code snippets, insights, and testing have been personally reviewed and refined by me to ensure accuracy and developer relevance. The goal is to share practical knowledge with fellow developers—faster and smarter.

Leave a Comment

Comments: