# βοΈ AWS Setup: VPC, EC2 & Nginx Web Server
## ποΈ HIGH-LEVEL ARCHITECTURE
```text
VPC
βββ Public Subnet (EC2-1)
βββ Public Subnet (EC2-2)
βββ Internet Gateway
βββ Route Table β Internet
βοΈ PHASE 1: INFRASTRUCTURE (Algorithm Style)
πΉ STEP 1: Create VPC
Go to VPC Dashboard
Click Create VPC
Enter:
- Name:
MyVPC - CIDR:
10.0.0.0/16
Click Create
π This is your private network.
πΉ STEP 2: Create 2 Subnets
Go to Subnets β Create subnet
Subnet 1
- Name:
Public-Subnet-1 - CIDR:
10.0.1.0/24 - AZ: Any
Subnet 2
- Name:
Public-Subnet-2 - CIDR:
10.0.2.0/24 - AZ: Any (Different from Subnet 1 recommended)
π Both are PUBLIC (configured next).
πΉ STEP 3: Create Internet Gateway
Go to Internet Gateways
Create:
- Name:
MyIGW
Attach it to your VPC.
π Now your VPC can access the internet.
πΉ STEP 4: Create Route Table
Go to Route Tables β Create
- Name:
Public-RT
Attach to VPC.
Add Route:
- Destination:
0.0.0.0/0 - Target: Internet Gateway
π Sends all internet traffic outside.
πΉ STEP 5: Associate Subnets
Open Route Table β Subnet Associations
Attach:
- Public-Subnet-1
- Public-Subnet-2
π Both subnets are now PUBLIC.
πΉ STEP 6: Enable Auto Public IP
For each subnet:
Edit subnet settings β Enable:
- Auto-assign public IP
π Without this, EC2 won't get public IPs automatically.
πΉ STEP 7: Create Security Group
Go to:
EC2 β Security Groups
Create:
- Name:
Web-SG
Inbound Rules
| Type | Port | Source |
|---|---|---|
| SSH | 22 | My IP |
| HTTP | 80 | 0.0.0.0/0 |
π Allows SSH + web traffic.
πΉ STEP 8: Create Key Pair
Go to:
EC2 β Key Pairs
Create:
- Name:
my-key
Download:
my-key.pem
π Needed for SSH access.
πΉ STEP 9: Launch EC2 Instance 1
Go to:
EC2 β Launch Instance
Configure:
- Name:
EC2-1 - AMI: Amazon Linux
- Instance type:
t2.micro - Key pair:
my-key
Network Settings
- VPC: MyVPC
- Subnet: Public-Subnet-1
- Auto IP: Enabled
- Security Group: Web-SG
Click Launch
πΉ STEP 10: Launch EC2 Instance 2
Same settings, change:
- Name:
EC2-2 - Subnet:
Public-Subnet-2
πΉ STEP 11: SSH into EC2
chmod 400 my-key.pem
ssh -i my-key.pem ec2-user@<PUBLIC-IP>
π You are now inside your server.
π (OPTIONAL) STEP 12: Create Load Balancer
Go to:
EC2 β Load Balancers
Create Application Load Balancer
Choose:
- Internet-facing
- Select your VPC
- Select both subnets
Create Target Group
Add:
- EC2-1
- EC2-2
Attach Target Group to Load Balancer.
π Traffic will be distributed across both servers.
π§ FINAL UNDERSTANDING (Phase 1)
You built:
- VPC = Network
- Subnets = Zones
- IGW = Internet Access
- Route Table = Traffic Rules
- Security Group = Firewall
- EC2 = Servers
- Load Balancer = Traffic Distributor
βοΈ PHASE 2: NGINX CONFIGURATION
βοΈ STEP 1: Verification
Ensure both instances:
-
Are in a public subnet
-
Have a public IP
-
Security Group allows:
- HTTP (80)
- SSH (22)
π STEP 2: SSH into EC2-1
ssh -i my-key.pem ec2-user@<EC2-1-PUBLIC-IP>
π¦ STEP 3: Install Nginx on EC2-1
sudo yum update -y
sudo yum install nginx -y
Start and enable:
sudo systemctl start nginx
sudo systemctl enable nginx
Check status:
systemctl status nginx
π STEP 4: Test EC2-1
Open:
http://<EC2-1-PUBLIC-IP>
π You should see Nginx Welcome Page
π STEP 5: SSH into EC2-2
ssh -i my-key.pem ec2-user@<EC2-2-PUBLIC-IP>
π¦ STEP 6: Install Nginx on EC2-2
sudo yum update -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
π STEP 7: Test EC2-2
Open:
http://<EC2-2-PUBLIC-IP>
π You should see Nginx Welcome Page
π¨ (OPTIONAL) STEP 8: Customize Each Server
On EC2-1
echo "Hello from EC2-1" | sudo tee /usr/share/nginx/html/index.html
On EC2-2
echo "Hello from EC2-2" | sudo tee /usr/share/nginx/html/index.html
Test
- EC2-1 IP β Hello from EC2-1
- EC2-2 IP β Hello from EC2-2
π§ WHAT IS HAPPENING?
- Nginx listens on port 80
- EC2 public IP routes traffic to the instance
- Security Group allows HTTP traffic
- Nginx serves the HTML page
β οΈ COMMON MISTAKES
β No HTTP Rule in Security Group
- Page times out
β Nginx Not Started
- Connection refused
β Using Private IP in Browser
- Won't work from your local computer
β Wrong AMI Commands
Amazon Linux:
yum
Ubuntu:
apt
π NEXT LEVEL
Upgrade this setup:
- Add a Load Balancer
- Use one DNS instead of multiple IPs
- Enable failover if one EC2 goes down