Problem Statement:
When deploying a web application, developers often require a reliable and scalable web server. Hosting a website or web application on AWS EC2 is a common solution, but configuring the web server properly can be challenging for beginners. The goal of this project is to set up an EC2 instance, install Apache or Nginx, and ensure it serves web content efficiently.
Debugging Challenges
During the deployment process, several issues may arise:
- SSH Connection Issues: Misconfigured security groups or incorrect key pairs may prevent SSH access.
- Package Installation Failures: Package repositories may be outdated, requiring updates before installation.
- Service Not Starting: Apache or Nginx might fail to start due to incorrect permissions, missing dependencies, or conflicts.
- Firewall Restrictions: If the firewall or security group settings are misconfigured, the web server might not be accessible.
- File Path Issues: The default web directory may vary depending on the web server, leading to content not being served correctly.
Solution Approach
Step 1: Launch an EC2 Instance
- Navigate to AWS Management Console → EC2.
Click Launch Instance and configure:
o Name: MyWebServer
o AMI: Amazon Linux 2 or Ubuntu
o Instance Type: t2.micro (Free Tier eligible)
o Key Pair: Create or use an existing key pair.
o Security Group: Allow SSH (22) and HTTP (80) traffic.Click Launch.
Step 2: Connect to the EC2 Instance
ssh -i your-key.pem ec2-user@your-instance-public-ip
- (For Ubuntu, use ubuntu instead of ec2-user)
Step 3: Install Apache or Nginx
Apache Installation
sudo yum update -y # Amazon Linux
sudo apt update -y # Ubuntu
sudo yum install httpd -y # Amazon Linux
sudo apt install apache2 -y # Ubuntu
sudo systemctl start httpd # Amazon Linux
sudo systemctl enable httpd # Amazon Linux
sudo systemctl start apache2 # Ubuntu
sudo systemctl enable apache2 # Ubuntu
Verify installation:
sudo systemctl status httpd # Amazon Linux
sudo systemctl status apache2 # Ubuntu
Nginx Installation
sudo yum install nginx -y # Amazon Linux
sudo apt install nginx -y # Ubuntu
sudo systemctl start nginx
sudo systemctl enable nginx
Verify installation:
sudo systemctl status nginx
Step 4: Configure Firewall
Allow HTTP traffic:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
(For Ubuntu, security groups handle firewall settings in AWS.)
Step 5: Deploy a Web Page
sudo echo "<h1>Welcome to My Web Server</h1>" > /var/www/html/index.html # Apache
sudo echo "<h1>Welcome to My Web Server</h1>" > /usr/share/nginx/html/index.html # Nginx
Step 6: Access the Web Server
Open a browser and navigate to:
http://your-instance-public-ip
You should see "Welcome to My Web Server" displayed.
Step 7: Configure Auto Start (Optional)
Ensure the web server starts automatically on reboot:
sudo systemctl enable httpd # Apache
sudo systemctl enable nginx # Nginx
Conclusion
By following these steps, we successfully set up an EC2-based web server running Apache or Nginx. This approach is widely used in hosting websites, web applications, and reverse proxy configurations. Debugging common issues such as SSH connectivity, package installation, and firewall settings ensures a smooth deployment experience.
By learning in public, sharing our debugging challenges and solutions, we not only solidify our understanding but also help others facing similar issues. Happy deploying!
Top comments (1)
👍