Inteliny
DevOpsLevel: Intermediate120m•Verified Production Blueprint

Deploy a MERN Stack Application on AWS EC2

Learn to deploy a MERN Stack application on AWS EC2 using Ubuntu, Nginx, PM2, MongoDB Atlas, SSL, and a custom domain.

Inteliny Engineering

Principal Architect

Overview & Architecture Scope

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:

Node.js v18.0+ runtime environment
MongoDB v6.0+ database cluster
Active AWS or Cloudflare account with DNS access
Linux Ubuntu 22.04 LTS server instance
Basic knowledge of CLI bash & Git workflow

Target System Architecture Diagram

Browser
NGINX
Node / Express
MongoDB / Redis

Interactive Execution Checklist

0/10 Completed

Step-by-Step Implementation Guide

1

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:

bash
aws ec2 run-instances --image-id ami-0c94855ba95c71c99 --instance-type t2.micro --key-name my-key
Make sure to replace 'ami-0c94855ba95c71c99' with the ID of the Ubuntu image in your region and 'my-key' with the name of your SSH key pair.
2

Step 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:

bash
ssh -i my-key.pem ubuntu@your-ec2-public-ip
Replace 'my-key.pem' with the name of your .pem file and 'your-ec2-public-ip' with the public IP address of your EC2 instance.
3

Step 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:

bash
sudo apt update && sudo apt install nodejs
Make sure your Node.js version is compatible with your application's requirements.
4

Step 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:

bash
sudo apt install git && git clone your-repo-url
Make sure you have Git configured on your EC2 instance and that you have access to your repository.
5

Step 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:

bash
MONGODB_URI=mongodb+srv://username:password@cluster-name.mongodb.net/
Replace the placeholders with your actual MongoDB Atlas connection details.
6

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:

bash
npm install && pm2 start server.js
Make sure your server.js file is set up to start your backend server.
7

Step 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:

bash
npm run build
Ensure you have the correct build script in your package.json file.
8

Step 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:

bash
sudo apt install nginx && sudo nano /etc/nginx/sites-available/default
Your Nginx configuration should include the 'proxy_pass' directive to your backend server.
9

Step 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:

bash
sudo apt install certbot && sudo certbot --nginx
Make sure your custom domain is properly configured and resolving to your EC2 instance's public IP address.
10

Step 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:

None
None
Regularly review your security settings to ensure they are up to date and aligned with best practices.

Pro Tips & Optimizations

Regularly update your EC2 instance and installed packages to ensure you have the latest security patches.
Use a process manager like PM2 to manage and monitor your Node.js application.
Implement logging and monitoring tools to track your application's performance and issues.
Test your application thoroughly after each deployment to catch any bugs or issues early.

Common Pitfalls to Avoid

Forgetting to update the package list before installing new packages, which can lead to compatibility issues.
Not configuring the security group correctly, resulting in blocked traffic to your instance.
Incorrectly setting up the Nginx reverse proxy, leading to routing issues between the frontend and backend.
Failing to renew the SSL certificate, causing it to expire and resulting in security warnings for your users.
Conclusion & Next Steps

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.

Deploy a MERN Stack Application on AWS EC2 | Inteliny Knowledge Base