Deploying a MERN stack app on AWS EC2

Here's the simplified guide formatted as HTML: ```html Deploy MERN Stack App on AWS EC2

Deploy MERN Stack App on AWS EC2

1. Set Up Your EC2 Instance

  1. Go to AWS and create an EC2 instance (a virtual server).
  2. Choose an Amazon Linux 2 template.
  3. Select a small server size (like t2.micro).
  4. Configure the instance to allow HTTP, HTTPS, and SSH traffic.
  5. Download the key file provided by AWS.

2. Connect to Your EC2 Instance

  1. Open your terminal (or use PuTTY on Windows).
  2. Connect to your EC2 instance using the command:
    ssh -i /path/to/your-key-file.pem ec2-user@your-ec2-public-dns

3. Install Required Software

  1. Update the server:
    sudo yum update -y
  2. Install Node.js (for running JavaScript):
    curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -
    sudo yum install -y nodejs
  3. Install MongoDB (your database):
    echo "[mongodb-org-4.2]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.2/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc" | sudo tee /etc/yum.repos.d/mongodb-org-4.2.repo
    sudo yum install -y mongodb-org
    sudo systemctl start mongod
    sudo systemctl enable mongod

4. Deploy Your Application

  1. Copy your app from GitHub:
    git clone https://github.com/your-repo/your-mern-app.git
    cd your-mern-app
  2. Install app dependencies:
    npm install
    cd client
    npm install
    cd ..
  3. Build the frontend:
    cd client
    npm run build
    cd ..
  4. Start your app:
    npm start

5. Make Your App Accessible Online

  1. Install Nginx (a web server):
    sudo amazon-linux-extras install nginx1 -y
  2. Configure Nginx to pass traffic to your app:
    sudo vim /etc/nginx/nginx.conf
    Add the following lines inside the server block:
    server {
        listen 80;
        server_name your-ec2-public-dns;
    
        location / {
            proxy_pass http://localhost:3000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
  3. Restart Nginx:
    sudo systemctl restart nginx

6. Access Your Application

Open your web browser and go to your EC2 instance's public DNS (the address provided by AWS). Your MERN app should now be live!

```

Post a Comment

0 Comments