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.
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.
The MERN Stack is a popular JavaScript-based tech stack comprising:
Before starting, make sure you have:
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!
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
You have two options:
sudo yum install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
git clone https://github.com/your-username/your-mern-app.git
cd your-mern-app
cd backend
npm install
node server.js
Tip: Use PM2 to keep the backend running:
npm install -g pm2
pm2 start server.js
pm2 startup
pm2 save
cd ../frontend
npm install
npm run build
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable 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
sudo yum install -y certbot python3-certbot-nginx
sudo certbot --nginx
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).
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!
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.