AWS Builder Center
Deploying a MERN Stack Application on AWS EC2: A Step-by-Step Guide

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

Learn how to deploy a MERN stack application on an AWS EC2 Ubuntu server, connect it with GitHub, configure the Node.js backend, and make the application accessible online.

Student

Introduction

Deploying a full-stack web application is an important step in the software development process. In this article, I will explain my learning experience deploying a MERN stack application on Amazon Web Services (AWS) using an EC2 Ubuntu instance.
My project, GreenCycle, uses MongoDB, Express.js, React.js, and Node.js. The goal was to understand how a web application can be deployed on a cloud server and made accessible over the internet.

Technologies Used

  • MongoDB: Database for storing application data
  • Express.js: Backend web framework
  • React.js: Frontend user interface
  • Node.js: JavaScript runtime for the backend
  • AWS EC2: Cloud server for hosting the application
  • Ubuntu: Operating system running on the EC2 instance
  • Git and GitHub: Version control and source code management

Step 1: Prepare the Project

Before deployment, I checked the project structure and verified that the frontend and backend were organized correctly.
I also pushed the latest project changes to GitHub so that the code could be accessed from the EC2 server.

Step 2: Launch an AWS EC2 Instance

I used Amazon EC2 to create a virtual server running Ubuntu.
After launching the instance, I configured the required security group rules and connected to the server using SSH and my EC2 key pair.
Security is important when configuring cloud infrastructure. Only the necessary ports should be accessible, and private keys must be kept secure.

Step 3: Connect GitHub to EC2

After connecting to the Ubuntu server, I cloned my GitHub repository and navigated to the project directory.
Plain text
git clone YOUR_GITHUB_REPOSITORY_URL
cd GreenCycle
Replace the placeholder with your actual repository URL. Repository access may require additional authentication if it is private.

Step 4: Configure the Backend

I checked the backend configuration and environment variables, including the server port and MongoDB connection settings.
The backend must connect successfully to the database before the application can work correctly.
Sensitive values such as database credentials, API keys, and secrets should be stored in environment variables rather than committed to a public repository.

Step 5: Install Dependencies and Start the Application

After verifying the project configuration, I installed the required dependencies and tested the backend.
For a Node.js backend, the basic commands are:
Plain text
cd server
npm install
The correct startup command depends on the scripts configured in the project's  package.json .
For a production deployment, the application should run through a suitable process manager or service configuration instead of relying on a temporary terminal session.

Step 6: Configure Networking

To access the application from a browser, the server, frontend, and backend must be configured to communicate correctly.
This includes checking the EC2 security group, backend port, frontend hosting configuration, and any API URLs used by the frontend.
For a production environment, HTTPS and an appropriate reverse proxy configuration are recommended.

Challenges and Lessons Learned

During this process, I learned that deploying an application involves more than writing code. Server configuration, network access, environment variables, database connectivity, and dependency installation are all important parts of deployment.
Working with GitHub and AWS EC2 also helped me understand how developers can manage application code and run services on cloud infrastructure.

Conclusion

Deploying GreenCycle on AWS EC2 was a valuable learning experience that helped me explore cloud computing and full-stack application deployment.
I look forward to improving my knowledge of AWS, Linux, deployment automation, and production security as I continue developing my skills.
Disclaimer: This article describes my learning process and a general deployment workflow. Exact commands and configuration may vary depending on the project and AWS setup.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article