A demonstration of hosting two web servers (Nginx and Apache) on AWS EC2 instances, with DNS routing using Cloudflare.
This project showcases a real-world simulation of setting up multiple web servers on Amazon Web Services (AWS) EC2 instances, while managing domain routing via Cloudflare.
In this task, I purchased a domain through Cloudflare and created two EC2 instances: one running Nginx and another running Apache. Both servers were made publicly accessible on port 80 (HTTP) via subdomains.
- Cloudflare Domain with Nginx and Apache on AWS EC2
- Amazon Web Services (AWS EC2): Cloud-hosted virtual servers.
- Nginx: High-performance web server.
- Apache: A widely-used web server for hosting websites.
- Cloudflare: DNS management and domain registration.
- Bash Scripting: Automating the setup process on EC2 instances.
DNS routing is a mechanism that maps a human-readable domain name (like example.com
) to the corresponding IP address of a server. When a user types a URL into their browser, DNS translates that URL into the server's IP address, allowing the browser to connect to the correct web server.
In this project, I used:
- Cloudflare to manage the domain and DNS routing.
- Cloudflare was responsible for routing traffic from the domain I purchased to two AWS EC2 instances, each running a different web server (Nginx and Apache).
- It simplifies access to servers by using domains instead of IP addresses.
- It allows multiple web services (like Nginx and Apache) to be accessed via different subdomains.
- Provides a production-ready way to manage traffic across multiple servers.
The first step in this project was to purchase a domain via Cloudflare. Cloudflare offers easy-to-use domain registration and DNS management services. I used their service to register the domain zeynabyusuf.com
.
-
Launch an EC2 instance with the following configurations:
- AMI: Amazon Linux 2.
- Instance Type: t2.micro (free tier eligible).
- Security Group: Allow port 80 (HTTP) for public access.
-
Add the following User Data script during instance creation to automate the installation and setup of Nginx:
#!/bin/bash yum update -y yum install -y nginx systemctl start nginx systemctl enable nginx
3. Verify Nginx is running by visiting the instance's public IP in a browser. You should see the default Nginx welcome page.
- Launch a second EC2 instance with similar configurations:
- AMI: Amazon Linux 2.
- Instance Type: t2.micro (free tier eligible).
- Security Group: Allow port 80 (HTTP) for public access.
- Add the following User Data script during instance creation to automate the installation and setup of Apache:
#!/bin/bash
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
- Verify Apache is running by visiting the instance's public IP in a browser. You should see the default Nginx welcome page.
With both servers running, I used Cloudflare to route traffic to each instance based on subdomains.
- Login to Cloudflare and navigate to the DNS settings for your domain.
- Add A Records for both servers:
- A Record for Nginx:
- Name:
nginx
- IPv4 Address: Public IP of the EC2 instance running Nginx.
- Name:
- A Record for Apache:
- Name:
apache
- IPv4 Address: Public IP of the EC2 instance running Apache.
- Name:
- A Record for Nginx:
- Wait for DNS propagation (usually a few minutes), then verify by accessing:
nginx.zeynabyusuf.com
for Nginx.
apache.zeynabyusuf.com
for Apache.
- SSL (HTTPS) Integration: Add SSL certificates to enable HTTPS for secure communication.
- Automation with Terraform: Automate the entire infrastructure setup using Infrastructure-as-Code (Terraform).