Setting up a web application on an AWS EC2 instance involves several steps, from launching the server to configuring Nginx to serve your application. In this guide, we’ll walk through each step to deploy a ReactJS application on an AWS EC2 Ubuntu server.
- Launching an AWS EC2 Ubuntu Server
- Connecting to the AWS EC2 Instance
- Installing Node.js, NPM, and Nginx
- Cloning the ReactJS App to EC2
- Installing Required Dependencies
- Creating a Production Build
- Configuring Nginx
- Starting the Application
- Domain and SSL setup
- Conclusion
https://youtu.be/UK_OVKDRArs?si=G620QaGNjJOA0LGR
- Log in to the AWS Management Console.
- Navigate to the EC2 Dashboard.
- Click on “Launch Instance” and choose an Amazon Machine Image (AMI) with your preferred OS.
- Use your preferred SSH terminal to connect to the EC2 instance.
- For example, if you’re on a Mac, you can use the Terminal app.
sudo apt-get update -y
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
sudo apt install npm -y
sudo apt install nginx -y
- For Public Repository:
git clone <YOUR-GIT-Repo>
- For Private Repository:
git clone <YOUR-GIT-Repo>
it will ask you for your GitHub username and password. You can use a Personal Access Token instead of a password.
cd <project-folder>
npm install
npm run build
sudo mkdir /var/www/vhosts/frontend/
sudo cp -R build/ /var/www/vhosts/frontend/
with this command, you can check if already a default nginx file exists. You have to remove it.
cd /etc/nginx/sites-enabled/
sudo rm -rf default
-
Create a configuration file for Nginx using the following command:
sudo vim /etc/nginx/sites-available/<nginx-file-name>
-
Paste the provided server configuration inside the file created.
server { listen 80 default_server; server_name _; location / { autoindex on; root /var/www/vhosts/frontend/build; try_files $uri /index.html; } }
-
Activate the configuration using the following command:
sudo ln -s /etc/nginx/sites-available/<nginx-file-name> /etc/nginx/sites-enabled/
- Restart Nginx and allow the changes to take place.
sudo systemctl restart nginx sudo service nginx restart
- Additionally, in case of errors, you can check error logs and status.
Domain
First, you have to Public IP address or ec2 instance as An R3cord of your domain, it can be on any domain provider like GoDaddy. You can also watch the video.
SSL Setup
sudo apt-get install certbot python3-certbot-nginx
sudo certbot --nginx -d <domain-name>
sudo systemctl reload nginx
Deploying a ReactJS application on an AWS EC2 instance requires careful configuration and setup. By following these steps, you can successfully launch your application and serve it using Nginx, ensuring a seamless user experience.