DEV Community

Avradeep Nayak
Avradeep Nayak

Posted on • Edited on

2

Hosting a MERN Application on AWS EC2: A Step-by-Step Guide

Hosting a web application is one of the most exciting steps in the development lifecycle. It transforms your local project into something accessible to the world. In this post, I’ll share my experience of hosting a MERN (MongoDB, Express, React, Node.js) application on an AWS EC2 instance, focusing on the steps, challenges, and benefits of using AWS for deployment.

Why AWS EC2?

AWS EC2 (Elastic Compute Cloud) is a powerful cloud computing service that offers scalable virtual servers. It’s an excellent choice for hosting MERN applications due to:

Scalability: EC2 allows you to scale resources up or down based on traffic.
Flexibility: You have complete control over the server environment.
Global Availability: With multiple regions and zones, your application can be hosted closer to your users for better performance.
Cost-Effectiveness: You only pay for what you use, making it ideal for both small and large applications.

Step-by-Step Hosting Guide

Here’s how I hosted my MERN application on AWS EC2:

  1. Setting Up the EC2 Instance Launch an EC2 Instance:

Go to the AWS Management Console and launch an EC2 instance.
Choose an Amazon Machine Image (AMI), such as Ubuntu 24.04 LTS.
Select an appropriate instance type (e.g., t2.micro for small-scale applications under the free tier).
Configure Security Groups:

Open ports for HTTP (80), HTTPS (443), and SSH (22) in the security group settings.
Restrict SSH access to your IP for security.
Key Pair:

Create or select an existing key pair for SSH access.

  1. Connecting to the Instance Use your terminal or SSH client to connect to the instance:
ssh -i "your-key.pem" ubuntu@your-ec2-public-ip
Update and upgrade the server:
Enter fullscreen mode Exit fullscreen mode
sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode
  1. Setting Up the Environment Install Node.js and npm:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
Enter fullscreen mode Exit fullscreen mode

Install MongoDB (Optional for Local DB):

If you're hosting MongoDB locally, install and configure it.

sudo apt install -y mongodb
sudo systemctl start mongodb
sudo systemctl enable mongodb
Enter fullscreen mode Exit fullscreen mode

Install Git:

sudo apt install -y git
Enter fullscreen mode Exit fullscreen mode
  1. Deploying the Application Clone Your Project Repository:
git clone https://github.com/your-repo-name.git
cd your-repo-name
Enter fullscreen mode Exit fullscreen mode

Install Dependencies:

npm install
cd client
npm install
cd ..
Enter fullscreen mode Exit fullscreen mode

Build the React Application:

In the React project folder:

npm run build
Serve the Build Folder:
Enter fullscreen mode Exit fullscreen mode

Use serve or another static file server to host the React build.

npm install -g serve
serve -s build
Enter fullscreen mode Exit fullscreen mode
  1. Configuring a Process Manager To ensure the Node.js server runs continuously, use PM2:

Install PM2:

npm install -g pm2
Start the Server with PM2:
Enter fullscreen mode Exit fullscreen mode
pm2 start server.js --name "mern-app"
pm2 save
Enter fullscreen mode Exit fullscreen mode

Enable PM2 to Start on Reboot:

pm2 startup
Enter fullscreen mode Exit fullscreen mode
  1. Setting Up a Reverse Proxy To expose your application on port 80 or 443, set up NGINX as a reverse proxy.

Install NGINX:

sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

Configure NGINX:

Open the configuration file:

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

Add the following:

server {
    listen 80;
    server_name your-domain.com;

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

Restart NGINX:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode
  1. Domain Name and SSL (Optional) Point a Domain to Your EC2 Instance:

Update your domain’s DNS settings to point to the EC2 instance’s public IP.
Install Let’s Encrypt for SSL:

Use Certbot to install and configure SSL certificates:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx
Enter fullscreen mode Exit fullscreen mode

Challenges and Solutions

Server Downtime:
Using PM2 minimized downtime by automatically restarting the application after crashes.

Security Concerns:
Regular updates and strict SSH rules ensured server security.

Database Connection:
I used a managed MongoDB Atlas database to simplify scaling and backups.

Final Thoughts
Hosting a MERN application on an AWS EC2 instance gives you full control over your application’s environment and scalability. While the process requires some setup and maintenance, the flexibility and performance benefits are well worth the effort.

Have questions or tips on hosting web applications? Share them in the comments below—I’d love to hear your thoughts!

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

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

Okay