Deploying a MERN Stack App on AWS EC2
Introduction
AWS EC2 is a scalable cloud server where we can host a MERN stack app. This guide walks through deployment.
Step 1: Launch an EC2 Instance
- Log in to AWS Console.
- Choose Ubuntu Server 20.04.
- Configure security group to allow HTTP, HTTPS, and SSH access.
Step 2: Connect to EC2
Use SSH:
ssh -i your-key.pem ubuntu@your-ec2-instance-ip
Step 3: Install Node.js & MongoDB
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs mongodb
Step 4: Deploy MERN App
Clone your repo, install dependencies, and start the app:
git clone https://github.com/your-repo.git
cd your-repo
npm install
node server.js
Step 5: Setup PM2 for Process Management
npm install -g pm2
pm2 start server.js --name mymernapp
pm2 save
pm2 startup
Step 6: Configure Nginx as a Reverse Proxy
sudo apt install nginx
sudo nano /etc/nginx/sites-available/default
Add this configuration:
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Restart Nginx:
sudo systemctl restart nginx
Conclusion
Your MERN app is now running on AWS EC2 with Nginx handling traffic.
Top comments (1)
very effective