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:
- 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.
- 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:
sudo apt update && sudo apt upgrade -y
- 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
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
Install Git:
sudo apt install -y git
- Deploying the Application Clone Your Project Repository:
git clone https://github.com/your-repo-name.git
cd your-repo-name
Install Dependencies:
npm install
cd client
npm install
cd ..
Build the React Application:
In the React project folder:
npm run build
Serve the Build Folder:
Use serve or another static file server to host the React build.
npm install -g serve
serve -s build
- 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:
pm2 start server.js --name "mern-app"
pm2 save
Enable PM2 to Start on Reboot:
pm2 startup
- 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
Configure NGINX:
Open the configuration file:
sudo nano /etc/nginx/sites-available/default
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;
}
}
Restart NGINX:
sudo systemctl restart nginx
- 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
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!
Top comments (0)