Learn how to deploy a MERN stack application to an AWS EC2 instance and set up a production-ready web server for your MERN application on Ubuntu 24.04.
This guide explains how to create a fully functional MERN stack web application on an Amazon EC2 instance (Elastic Compute Cloud) and deploy a customized MERN stack application that supports user registration and authentication.
- Sign in to your AWS account.
- Navigate to the EC2 dashboard.
- Click on "Launch Instance" and select Ubuntu 24.04 as your Amazon Machine Image (AMI).
- Configure instance details, security groups, and storage settings as needed.
- Launch the instance and wait for it to initialize.
- Open a terminal (or Git Bash) on your local machine.
- Set the permissions for your private key file:
chmod 400 <path-to-key-file>
- Connect to your EC2 instance using the following command:
ssh -i <path-to-key-file> ubuntu@<public-dns>
- Install Git if it’s not already installed:
sudo apt update sudo apt install git
- Clone your MERN stack project repository:
git clone <repository-url> cd <project-directory>
- Install Node.js:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - sudo apt install -y nodejs
- Install MongoDB:
sudo apt install -y mongodb
- Start MongoDB:
sudo systemctl start mongodb
- Navigate to your project directory and install dependencies:
npm install
- Update the proxy settings in your
server.js
file to match your environment.
- Install PM2:
sudo npm install -g pm2
- Start your application using PM2:
pm2 start server.js
- To run the client, navigate to the client directory and use:
pm2 start client.js
- Open a web browser and navigate to your EC2 instance's public IP or DNS to access the application.
You have successfully deployed a MERN stack application on an AWS EC2 instance running Ubuntu 24.04. Your application should now be accessible over the web.
- AWS Documentation
- MERN Stack Documentation
- Node.js and MongoDB Documentation
Feel free to modify and expand this guide based on your specific application needs!