Iot

Blog

banner border

Introduction:

The MERN stack (MongoDB, Express, React, Node.js) is a popular choice for building modern, dynamic web applications. With AWS (Amazon Web Services), you can deploy your MERN stack app in a secure, scalable, and cost-effective way. This guide walks you through the step-by-step process of setting up a MERN stack application on AWS, including creating an EC2 instance, configuring MongoDB, and deploying your React frontend and Node.js backend.

AWS Account: If you don’t have an AWS account, sign up at AWS Free Tier.

MongoDB Atlas Account (Optional): MongoDB Atlas offers a fully managed MongoDB solution.

SSH Key Pair: Needed to access the EC2 instance securely.

Steps to Set Up Your MERN Stack Application in AWS :

Step 1: Launch an EC2 Instance

 Create an Instance

  1. In your AWS Console, go to EC2 and click Launch Instance.
  2. Choose Amazon Linux 2 or Ubuntu as the AMI (Amazon Machine Image).
  3. Select t3.micro as the instance type.
  4. Configure security groups to allow access to the necessary ports (22 for SSH, 3000 for frontend, 5000 for backend).
  5. Generate a new SSH key pair if you don’t have one and download it.
  1. Access the Instance.

ssh -i “key file” user name@ip

Step 2: Install Node.js, NPM, and Git

After connecting to your EC2 instance, install Node.js, NPM, and Git, which are essential for running the MERN stack application.

Step 3: Clone Your MERN App Repository

Clone your MERN stack app from GitHub or another repository.

Step 4: Set Up MongoDB

For the database, you can either use MongoDB Atlas (a fully managed service) or install MongoDB on the EC2 instance itself.

Install MongoDB Locally on EC2

Step 5: Install and Build Application Dependencies

Navigate to both backend and frontend folders to install necessary dependencies.

npm install

npm run build

Step 6: Configure Environment Variables

1.In the backend folder, create a .env file 

Example-

MONGO_URI=your-mongo-uri

PORT=5000

2.Add the MongoDB URI, API keys, or any other necessary environment variables.

Step 7: Run the Application

1.Start the backend server

2.Your Node.js server should now be running on port 5000 (or the port you configured).

Step 8: Set Up Nginx as a Reverse Proxy

1.Install Nginx

2.Configure Nginx to serve the React app and proxy requests to your backend server

3.Restart Nginx

Step 9: Use PM2 to Keep Your Node App Running

PM2 is a process manager that ensures your application stays online and restarts if it crashes.

  1. Install PM2 globally
  2. Start the backend server with PM2
  3. Save the PM2 process to restart

Step 10: Configure Domain and SSL (Optional)

For production environments, using a custom domain and SSL certificate is essential for security.

  1. In AWS Route 53, create a hosted zone and add an A record pointing to your EC2 instance’s public IP.
  2. Use Certbot with Let’s Encrypt to get a free SSL certificate.

Final Step: Testing Your Application

Visit your EC2 instance’s public IP or domain to view your deployed MERN stack application. Test various routes to ensure the frontend, backend, and database connectivity work as expected.

Conclusion

Deploying a MERN stack app on AWS involves setting up an EC2 instance, connecting to a MongoDB database, configuring Nginx as a reverse proxy, and keeping the server running with PM2. This setup offers a strong foundation for scaling and maintaining your application in a production environment.

Let us know in the comments if you have any questions or tips to share about deploying the MERN stack on AWS!

Contact Us

    location
    x
    location
    Schedule a
    Free Consultatiion

    If we can help in any way, please don't hesitate to set a time to meet or talk, or leave your details and we'll get back to you.

    Startups, We Shape your Ideas
    let's build location

    start here

    • Pick a date & time of your choice.
    • No obligation. Cancel anytime.