DEV Community

kathir b
kathir b

Posted on

1

Deploying a MERN Stack App on AWS EC2

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

  1. Log in to AWS Console.
  2. Choose Ubuntu Server 20.04.
  3. 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
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Node.js & MongoDB

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs mongodb
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 5: Setup PM2 for Process Management

npm install -g pm2
pm2 start server.js --name mymernapp
pm2 save
pm2 startup
Enter fullscreen mode Exit fullscreen mode

Step 6: Configure Nginx as a Reverse Proxy

sudo apt install nginx
sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Restart Nginx:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Conclusion

Your MERN app is now running on AWS EC2 with Nginx handling traffic.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (1)

Collapse
 
subhashini_sathyanarayana profile image
subhashini sathyanarayanan

very effective

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay