How to Fix Apache 404 Not Found on AWS EC2
The Root Cause
On AWS EC2, an Apache 404 “Not Found” error commonly occurs when the web server cannot locate the requested resource within its configured DocumentRoot, typically /var/www/html. This often happens because no index file (e.g., index.html) exists in that directory, or the Apache service is not running or properly enabled to serve content.
Quick Fix (CLI)
Execute the following commands to ensure Apache is installed, running, and serving a basic index page from the default DocumentRoot. Choose commands appropriate for your EC2 instance’s operating system.
# --- For Amazon Linux 2 / RHEL-based AMIs ---
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
echo "<html><body><h1>WebToolsWiz: Apache is Working on EC2!</h1></body></html>" | sudo tee /var/www/html/index.html
sudo chown -R apache:apache /var/www/html
sudo chmod -R 755 /var/www/html
sudo systemctl restart httpd # Restart to ensure permissions and new file are recognized
# --- For Ubuntu / Debian-based AMIs (if applicable, uncomment to use) ---
# sudo apt update -y
# sudo apt install -y apache2
# sudo systemctl start apache2
# sudo systemctl enable apache2
# echo "<html><body><h1>WebToolsWiz: Apache is Working on EC2!</h1></body></html>" | sudo tee /var/www/html/index.html
# sudo chown -R www-data:www-data /var/www/html
# sudo chmod -R 755 /var/www/html
# sudo systemctl restart apache2 # Restart to ensure permissions and new file are recognized
Configuration Check
Review your Apache configuration file to confirm the correct DocumentRoot and directory permissions.
- File to edit (Amazon Linux 2/RHEL):
/etc/httpd/conf/httpd.conf - File to edit (Ubuntu/Debian):
/etc/apache2/apache2.confand/etc/apache2/sites-available/000-default.conf
Example lines to check/modify (for /etc/httpd/conf/httpd.conf):
# Ensure this directive points to your web content directory
DocumentRoot "/var/www/html"
# Verify the corresponding directory block, ensuring 'Require all granted'
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
After any configuration changes, always restart Apache: sudo systemctl restart httpd (Amazon Linux 2/RHEL) or sudo systemctl restart apache2 (Ubuntu/Debian).
Verification
After implementing the fixes, verify Apache is serving content correctly.
# Check the Apache service status
sudo systemctl status httpd # For Amazon Linux 2 / RHEL
# sudo systemctl status apache2 # For Ubuntu / Debian
# Locally verify content is being served
curl http://localhost/
A successful verification will display the HTML content (e.g., <h1>WebToolsWiz: Apache is Working on EC2!</h1>) instead of a 404 error.