This comprehensive guide is designed for experienced developers and DevOps engineers looking to deploy a MERN Stack application on AWS EC2. The MERN Stack, consisting of MongoDB, Express.js, React, and Node.js, is a popular choice for web development. By the end of this guide, you will have successfully deployed your MERN Stack application on AWS EC2 using Ubuntu, Nginx, PM2, MongoDB Atlas, SSL, and a custom domain. This guide assumes you have basic knowledge of Linux commands, Node.js, and React. You will need an AWS account, a custom domain, and a MongoDB Atlas account to follow along.
Prerequisites & System Requirements
Ensure your development workstation or staging server fulfills the following prerequisites before initiating commands:
Target System Architecture Diagram
Interactive Execution Checklist
Step-by-Step Implementation Guide
Step 1: Set Up AWS EC2 Instance
First, you need to set up an AWS EC2 instance. Log in to your AWS account, navigate to the EC2 dashboard, and click on 'Launch Instance'. Choose Ubuntu as your operating system and select the appropriate instance type based on your application's requirements. Configure your instance details, add storage if necessary, and set up your security group to allow inbound traffic on ports 22 (SSH), 80 (HTTP), and 443 (HTTPS). Launch your instance and wait for it to become available.
Execute Command Terminal:
aws ec2 run-instances --image-id ami-0c94855ba95c71c99 --instance-type t2.micro --key-name my-keyStep 2: Configure SSH
To connect to your EC2 instance securely, you need to configure SSH. Download the .pem file associated with your SSH key pair from the AWS console. Open a terminal on your local machine, navigate to the directory where you downloaded the .pem file, and run the command 'chmod 400 my-key.pem' to change the file's permissions. Then, connect to your EC2 instance using 'ssh -i my-key.pem ubuntu@your-ec2-public-ip'.
Execute Command Terminal:
ssh -i my-key.pem ubuntu@your-ec2-public-ipStep 3: Install Node.js and MongoDB
Once connected to your EC2 instance, you need to install Node.js and MongoDB. Update your package list with 'sudo apt update', then install Node.js using 'sudo apt install nodejs'. For MongoDB, you will use MongoDB Atlas, so you don't need to install MongoDB on your EC2 instance. Instead, create a MongoDB Atlas account, set up a cluster, and get your connection string.
Execute Command Terminal:
sudo apt update && sudo apt install nodejsStep 4: Clone Your Git Repository
Clone your MERN Stack application's Git repository to your EC2 instance. First, install Git using 'sudo apt install git', then navigate to the directory where you want to clone your repository and run 'git clone your-repo-url'. Replace 'your-repo-url' with the URL of your Git repository.
Execute Command Terminal:
sudo apt install git && git clone your-repo-urlStep 5: Configure Environment Variables
Create a .env file in the root of your project and add your environment variables, including your MongoDB Atlas connection string. Use 'nano .env' or your preferred text editor to create and edit the file.
Execute Command Terminal:
MONGODB_URI=mongodb+srv://username:password@cluster-name.mongodb.net/Step 6: Deploy Backend
Install your backend dependencies using 'npm install' or 'yarn install', depending on your package manager. Then, start your backend server. If you're using a process manager like PM2, install it globally with 'npm install pm2 -g' or 'yarn global add pm2', then start your server with 'pm2 start server.js'.
Execute Command Terminal:
npm install && pm2 start server.jsStep 7: Build Frontend
Navigate to your frontend directory and build your React application using 'npm run build' or 'yarn build'. This will create a production-ready build of your frontend in the 'build' folder.
Execute Command Terminal:
npm run buildStep 8: Configure Nginx Reverse Proxy
Install Nginx using 'sudo apt install nginx', then configure it as a reverse proxy to serve your frontend and proxy requests to your backend. Create a new Nginx configuration file using 'sudo nano /etc/nginx/sites-available/default' and add your configuration. Restart Nginx with 'sudo service nginx restart' to apply the changes.
Execute Command Terminal:
sudo apt install nginx && sudo nano /etc/nginx/sites-available/defaultStep 9: Set Up SSL with Let's Encrypt
Install Certbot using 'sudo apt install certbot' and run 'sudo certbot --nginx' to obtain an SSL certificate for your custom domain. Follow the prompts to configure your certificate.
Execute Command Terminal:
sudo apt install certbot && sudo certbot --nginxStep 10: Configure Domain and Security
Update your domain's DNS settings to point to your EC2 instance's public IP address. Ensure your security group allows inbound traffic on the necessary ports. Consider implementing additional security measures such as a firewall and monitoring.
Execute Command Terminal:
NonePro Tips & Optimizations
Common Pitfalls to Avoid
By following these steps, you have successfully deployed your MERN Stack application on AWS EC2 using Ubuntu, Nginx, PM2, MongoDB Atlas, SSL, and a custom domain. This setup provides a robust and scalable foundation for your web application, with considerations for security, performance, and maintainability. Remember to continuously monitor and improve your application to ensure it meets the evolving needs of your users.
Production Best Practices & Hardening
Security Hardening
Disable root SSH access, enforce key-based auth, and enable UFW firewall on ports 80/443.
Memory Management
Set Node.js max-old-space-size to 80% of total RAM to avoid Linux OOM-killer crashes.
Frequently Asked Questions
Yes, all NGINX, Docker, and PM2 deployment steps can be packaged into Infrastructure as Code (IaC) playbooks.
Need Help Implementing This?
Partner with Inteliny's principal architects to audit your stack, automate CI/CD, and accelerate deployment.