Host Static Website on EC2 with Apache
Topics: EC2, Apache HTTP Server, httpd, User Data Automation, Static Website Hosting
Overview
Amazon EC2 instances start as bare virtual machines without pre-installed application software. To host a website, you must install and configure a web server. Apache HTTP Server (httpd) is the world's most popular open-source web server, powering millions of websites globally. It serves static content (HTML, CSS, JavaScript, images) and can be configured for dynamic content through modules.
This lab demonstrates two fundamental approaches to deploying web applications on EC2: manual installation (SSH-based configuration) and automated deployment using User Data scripts.
Key Concepts
| Concept | Description |
|---|---|
| Apache HTTP Server (httpd) | Open-source web server software that listens on port 80/443 and serves web content to browsers |
| httpd | HTTP Daemon - the service name for Apache on Red Hat-based Linux distributions (Amazon Linux, CentOS) |
| Document Root | Directory where web server looks for files to serve (default: /var/www/html on Amazon Linux) |
| systemd | Linux system and service manager for starting, stopping, enabling services (systemctl command) |
| yum Package Manager | Yellowdog Updater Modified - package manager for Red Hat-based systems to install/update software |
| User Data Script | Bash script executed automatically at instance first boot for configuration automation |
| Shebang (#!/bin/bash) | First line of script specifying the interpreter, required for User Data scripts to execute |
| Port 80 (HTTP) | Standard TCP port for unencrypted web traffic (Hypertext Transfer Protocol) |
| Security Group | Virtual firewall controlling inbound/outbound traffic; must allow port 80 for web access |
| cloud-init | AWS service that processes User Data scripts during instance initialization |
Prerequisites
- Active AWS account (Free Tier eligible)
- Completed Lab 6 (SSH connection to Linux EC2)
- Basic understanding of Linux command-line operations
- SSH client installed (PowerShell, Terminal, or PuTTY)
- Text editor knowledge (nano or vim) for manual method
- Basic HTML knowledge (optional, for customizing web content)
Architecture Overview
Click to expand Architecture Diagram
Method 1: Manual Installation of Apache Web Server
This method provides hands-on experience with Linux system administration by manually configuring each component.
Phase 1: Launch EC2 Instance
Sign in to AWS Management Console.
Navigate to EC2 service → Click Launch Instance.
Configure instance details:
- Name:
MyApacheServer(or descriptive name) - AMI: Amazon Linux 2 AMI (Free tier eligible)
- Instance type: t3.micro (Free tier eligible)
- Name:
Create or select key pair:
- Key pair: Create new or select existing
- Format: .pem (for SSH access)
- Download and save securely
Configure Network Settings:
- Click Edit under Network settings
- Firewall (security groups): Create security group or select existing
- Security group name:
apache-web-sg - Description: "Allow SSH and HTTP traffic"
Add inbound security group rules:
Rule 1:
- Type: SSH
- Protocol: TCP
- Port: 22
- Source: My IP (recommended for security)
Rule 2:
- Type: HTTP
- Protocol: TCP
- Port: 80
- Source: 0.0.0.0/0 (Anywhere - required for public website access)
Port 80 Requirement
Unlike SSH (which should be restricted), HTTP port 80 must be open to 0.0.0.0/0 (anywhere) to allow public access to your website. This is standard for web servers accessible from the internet.
Configure Storage:
- Size: 8 GiB (default)
- Volume type: gp3 (General Purpose SSD)
- Leave other settings at default
Click Launch instance.
Wait for instance to be ready:
- Instance state: Running
- Status check: 2/2 checks passed
- Note the Public IPv4 address
Phase 2: Connect via SSH
Open PowerShell (Windows) or Terminal (macOS/Linux).
Navigate to your key file location:
bashcd ~/Downloads # macOS/Linux cd "C:UsersYourNameDownloads" # WindowsSet proper key permissions:
bash# Linux/macOS chmod 400 your-key-file.pem # Windows PowerShell icacls .your-key-file.pem /inheritance:r icacls .your-key-file.pem /grant:r "$env:USERNAME:(R)"Connect to instance:
bashssh -i your-key-file.pem ec2-user@<Public-IP-Address>Type
yeswhen prompted to accept the host key.
Phase 3: Install Apache Web Server
- Update system packages (recommended before installing software):bash
sudo yum update -y
sudo command
sudo (superuser do) temporarily elevates privileges to root for administrative tasks. The ec2-user has passwordless sudo access on Amazon Linux instances.
Install Apache HTTP Server:
bashsudo yum install httpd -yhttpdis the package name for Apache on Red Hat-based systems-yflag automatically answers "yes" to installation prompts
Start the Apache service:
bashsudo systemctl start httpdEnable Apache to start automatically on system boot:
bashsudo systemctl enable httpd
systemctl vs service
systemctl is the modern systemd command. Older tutorials may show service httpd start (legacy command). Both work, but systemctl is preferred on modern Linux distributions.
Verify Apache is running:
bashsudo systemctl status httpd- Look for "active (running)" in green text
- Press
qto exit the status view
Test Apache from browser:
- Copy your instance's Public IPv4 address
- Open web browser
- Navigate to:
http://<Public-IP-Address> - You should see the Apache Test Page
Apache Test Page
The default page confirms Apache is running but no custom content exists yet. The page is generated from /usr/share/httpd/noindex/index.html when /var/www/html/index.html doesn't exist.
Phase 4: Create Custom Website Content
Navigate to the web document root:
bashcd /var/www/htmlCreate a new index.html file:
bashsudo nano index.htmlnanois a simple text editor (alternative:vim)sudorequired because /var/www/html is owned by root
Add HTML content (paste this code):
html<!DOCTYPE html> <html> <head> <title>Welcome to My Website</title> </head> <body style="text-align:center; background-color: #f0f0f0; font-family: Arial, sans-serif; padding: 50px;"> <h1 style="color: #333;">Hello from Apache on AWS EC2!</h1> <p style="font-size: 18px;">This is a static website hosted on Apache web server.</p> <p style="color: #666;">Deployed manually using SSH and systemctl commands.</p> </body> </html>Save and exit nano:
- Press
Ctrl + O(write out) - Press
Enter(confirm filename) - Press
Ctrl + X(exit)
- Press
Verify file creation:
bashls -la /var/www/html cat /var/www/html/index.htmlRestart Apache to ensure changes are loaded:
bashsudo systemctl restart httpdView your website:
- Refresh your browser:
http://<Public-IP-Address> - You should now see your custom HTML page instead of the Apache Test Page
- Refresh your browser:
File Permissions
Apache runs as user apache (group apache). If you encounter permission errors, set appropriate ownership: sudo chown apache:apache /var/www/html/index.html or sudo chmod 644 /var/www/html/index.html.
Method 2: User Data Script Automation
This method automates the entire Apache installation and website deployment using a bash script executed during instance first boot.
Understanding User Data Scripts
User Data scripts run automatically when an EC2 instance launches for the first time only. The cloud-init service executes the script as root user during the boot process, before the instance becomes available for SSH connection.
Key Requirements:
- First line must be
#!/bin/bash(shebang) - Runs only on first boot (not on subsequent reboots)
- Executes as root (no
sudoneeded in commands) - Must be provided during instance launch (cannot add later)
- Execution logs available in
/var/log/cloud-init-output.log
One-Time Execution
User Data scripts execute only during the first boot. If you stop/start the instance, the script does not re-run. To re-execute User Data, you must terminate and launch a new instance. For repeated execution, use cron jobs or AWS Systems Manager Run Command.
Phase 1: Prepare User Data Script
Copy this bash script (you'll paste it during instance launch):
#!/bin/bash
# Update all system packages
yum update -y
# Install Apache Web Server
yum install -y httpd
# Start Apache service
systemctl start httpd
# Enable Apache to start on boot
systemctl enable httpd
# Create custom website content
echo "<html>
<head><title>Welcome to My Auto Web Server</title></head>
<body style='text-align:center; background-color:#e9f5ff; font-family: Arial; padding: 50px;'>
<h1 style='color: #0066cc;'>Welcome to EC2 Instance</h1>
<h2 style='color: #333;'>Apache Installed Automatically via User Data Script</h2>
<p style='font-size: 18px;'>This is a static website hosted on EC2 using User Data automation.</p>
<p style='color: #666;'>No manual SSH configuration required!</p>
</body>
</html>" > /var/www/html/index.htmlRoot Privileges
User Data scripts run as root, so sudo is not required. Commands like yum install and systemctl execute with full administrative privileges automatically.
Phase 2: Launch Instance with User Data
Sign in to AWS Management Console.
- Perform all steps from Phase 1 till security group and storage.
Expand Advanced details section (at bottom):
- Scroll down to User data text box
- Paste the entire bash script from Phase 1 above
- Ensure the first line is
#!/bin/bash
Review configuration in Summary panel:
- Instance type: t3.micro
- Security group: HTTP (80) allowed
- User data: Present (shown as "Specified")
Click Launch instance and Wait for instance to be ready:
- Instance state: Running (1-2 minutes)
- Status checks: 2/2 checks passed (2-3 minutes)
- User Data execution: Additional 1-2 minutes
Phase 3: Verify Automated Deployment
Get the Public IPv4 address from EC2 console.
Open web browser and navigate to:
http://<Public-IP-Address>Verify your custom website appears immediately.
- No SSH required
- No manual configuration needed
- Website operational within minutes of launch
Public IP Changes
If you stop and start an instance, the Public IP address changes, breaking your website URL. For production websites, allocate an Elastic IP (static IP) or use a domain name with Route 53 DNS service.
Click to expand Troubleshooting Tips
If website doesn't appear, SSH to instance and check logs:
ssh -i your-key.pem ec2-user@<Public-IP>
# Check User Data execution log
sudo cat /var/log/cloud-init-output.log
# Check Apache status
sudo systemctl status httpd
# Check if index.html was created
ls -la /var/www/html/Log File Location
/var/log/cloud-init-output.log contains the complete output of User Data script execution, including any error messages. This is the first place to check when troubleshooting User Data issues.
Validation
Click to expand
Verify successful completion:
Instance Launch:
- Instance state shows "Running"
- Status checks: 2/2 checks passed
- Public IP address assigned
Security Group Configuration:
- Inbound rule for HTTP (port 80) from 0.0.0.0/0
- Inbound rule for SSH (port 22) from My IP (manual method)
Apache Installation:
sudo systemctl status httpdshows "active (running)"- Service enabled for boot:
sudo systemctl is-enabled httpdreturns "enabled"
Website Accessibility:
- Browser displays custom HTML page at
http://<Public-IP> - No connection timeout or Apache Test Page
- HTML content matches what you created/scripted
- Browser displays custom HTML page at
File System:
/var/www/html/index.htmlexists with correct content- File readable by Apache:
ls -la /var/www/html/index.html
Cost Considerations
Click to expand
EC2 Instance (t3.micro):
- Free Tier: 750 hours/month for first 12 months
- After Free Tier: ~$0.0104/hour = ~$7.50/month (us-east-1)
EBS Storage (8 GB gp3):
- Free Tier: 30 GB for first 12 months
- After Free Tier: $0.08/GB-month = $0.64/month
Data Transfer:
- Inbound: Free
- Outbound: First 100 GB/month free, then $0.09/GB
- Web traffic: Typical static website uses minimal bandwidth (<1 GB/month for low traffic)
Elastic IP (if allocated):
- Free while associated with running instance
- $0.005/hour if unattached or instance stopped
Cleanup
Click to expand
To avoid ongoing charges:
Exit SSH session (if connected):
bashexitTerminate the instance:
- Go to EC2 → Instances
- Select your instance
- Click Instance state → Terminate instance
- Confirm termination
- Result: All charges stop, data deleted permanently
Delete security group (optional):
- Go to EC2 → Security Groups
- Select your security group
- Click Actions → Delete security group
- Confirm deletion
Delete key pair (optional):
- Go to EC2 → Key Pairs
- Select your key pair
- Click Actions → Delete
- Delete local .pem file from your computer
Result
You have successfully deployed a static website on Amazon EC2 using Apache HTTP Server through two methods: manual SSH-based installation and automated User Data scripts. You now understand the fundamentals of web server deployment, Linux package management, systemd service control, and infrastructure automation.
Quick Start Guide
Quick Start Guide
- Launch EC2 instance with Amazon Linux 2 AMI and t3.micro type.
- Create security group allowing SSH (22) and HTTP (80) inbound traffic.
- Connect via SSH using your key pair.
- For Manual Method:
- Update packages:
sudo yum update -y - Install Apache:
sudo yum install httpd -y - Start and enable Apache:
sudo systemctl start httpdandsudo systemctl enable httpd - Create custom
index.htmlin/var/www/html/.
- Update packages:
- For User Data Method:
- Prepare bash script with Apache installation and HTML content.
- Paste script in User Data section during instance launch.
- Launch instance and wait for it to initialize.
- Access website via browser using Public IP address.
