DEV Community

Rajasekar Thangavel
Rajasekar Thangavel

Posted on

How to deploy react web app or static html in AWS instance

prerequisites

Feature Inbound Rules Outbound Rules
Direction Controls incoming traffic to the instance Controls outgoing traffic from the instance
Purpose Define who can access your server Define where your server can access
Example Allow HTTP (port 80) so browsers can load your site Allow access to external APIs or databases
Common Ports 22 (SSH), 80 (HTTP), 443 (HTTPS) 80, 443, custom DB/API ports
Default Behavior All inbound traffic denied unless explicitly allowed All outbound traffic allowed unless restricted
Use Case Allow only specific IPs or ports to access your app Restrict external communication for security

Inbound Rule:

Allow TCP port 22 from 0.0.0.0/0 — for SSH access to EC2
Allow TCP port 80 from 0.0.0.0/0 — for web server access
Enter fullscreen mode Exit fullscreen mode

Outbound Rule:

Allow all traffic to 0.0.0.0/0 — default for most servers
(Optional) Deny all except specific IPs/ports — for lockdown servers
Enter fullscreen mode Exit fullscreen mode

Step 1: Click on "Launch Instance"
Step 2: Select "Desire OS"

Desire OS

Step 3: Select Instance Types & Key Value pair
Instance Type - Based on required size
Key value pair - SSH login

Image Instance Types & Key Value pair

Step 4: Network Settings - Enable public access and private access

Network Settings

Step 5: Configure required storage
Configure required storage

Step 6: Click on "Launch Instance"

Note the instance's public IP and private IP

Step 7: Once the instance is launched successfully. SSH to connect to the instance

chmod 400 "jun17.pem"
ssh -i "jun17.pem" 
ubuntu@ec2-3-110-45-121.ap-south-1.compute.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

Install a web server to host the application

sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode
sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Host a static HTML pages

server {
    listen 80;
    server_name aws_ip;

    root /var/www/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}
Enter fullscreen mode Exit fullscreen mode

Restart service

sudo nginx -t
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Host React app

Install Nodejs in instance

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
Enter fullscreen mode Exit fullscreen mode
server {
    listen 80;
    server_name aws_ip;

    location / {
        proxy_pass http://localhost:3000; # or 5173, etc.
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
Enter fullscreen mode Exit fullscreen mode
sudo nginx -t
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Configure firewall in nginx (OPTIONAL)

sudo ufw status
sudo ufw allow 80
sudo ufw allow 22
sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

Note: Use PM2 for running multiple process at the same time in single instance.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.