Ravindra BagaleCourses & study guides

Guides

How to Install Apache (httpd) on Amazon Linux 2023 (EC2)

To install Apache on an Amazon Linux 2023 EC2 instance, run sudo yum install -y httpd, then start it with sudo service httpd start and enable it at boot with sudo systemctl enable httpd. Allow HTTP (TCP port 80) in the instance's security group and open http://<PUBLIC_IP> in a browser. On Amazon Linux the Apache package and service are called httpd, and the default web root is /var/www/html.

Hello friends! We saw Nginx earlier, now it's Apache. On the first day of my class one question always comes up: "Sir, the apache2 command doesn't work!" Because on Amazon Linux, Apache is called httpd. In this guide we install Apache and put your first page live. Let's type along together.

Quick answer

On an Amazon Linux 2023 EC2 instance, run these commands, allow HTTP (TCP port 80) in the security group, and open http://<PUBLIC_IP>:

sudo yum install -y httpd
sudo service httpd start
sudo systemctl enable httpd
sudo apachectl configtest        # expect "Syntax OK"
curl -I http://localhost         # expect HTTP/1.1 200 OK (or 403 before you add a page)

What do I need before installing Apache?

How does a request reach Apache on EC2?

How a web request reaches Apache (httpd) on EC2 A laptop and a mobile phone send an HTTP request over the internet. It passes the security group, which allows port 80, and reaches Apache (httpd) on the EC2 instance. Apache sends the response page back the same way. Request ← Response Laptop Mobile Internet Security group Port 80 open EC2 instance Apache httpd /var/www/html 200 OK Request Response Request Response

The browser sends a Request over the internet. The security group lets it in on port 80, Apache (httpd) on EC2 answers with a page from /var/www/html, and the Response travels back.

How do I install Apache on Amazon Linux 2023?

Step 1 — Free port 80 (only if Nginx is installed)

Only one program can listen on port 80. If Nginx runs on this instance, stop and disable it first:

sudo service nginx stop 2>/dev/null
sudo systemctl disable nginx 2>/dev/null
sudo ss -tlnp | grep :80            # no output = port 80 is free

Step 2 — Install the httpd package

sudo yum install -y httpd

On Amazon Linux 2023, CentOS Stream 9 and RHEL, Apache is the httpd package and the httpd service. On Ubuntu it is apache2.

Step 3 — Start Apache and enable it at boot

Apache does not start by itself after install on Amazon Linux 2023:

sudo service httpd start
sudo systemctl enable httpd
sudo service httpd status            # look for "active (running)"

Step 4 — Test the configuration and the service

sudo apachectl configtest            # expect "Syntax OK"
curl -I http://localhost             # a response line proves Apache answers

Before you add a page, Apache may answer with its test page (a 403 status is normal at this point, because /var/www/html is still empty).

Step 5 — Add your own page

The default web root is /var/www/html. Write a simple test page:

echo "<h1>Hello from Apache on EC2</h1>" | sudo tee /var/www/html/index.html
curl http://localhost

Now open http://<PUBLIC_IP> in your browser — http, not https. Your page is live! 🎉

Ravindra Bagale's Tip

Many students type sudo service apache2 start on Amazon Linux and get Unit not found. There, Apache is called httpd – the package, the service and the log folder /var/log/httpd/ are all httpd. Only on Ubuntu is it apache2. Check which OS you are on first with cat /etc/os-release.

Where are the Apache files on Amazon Linux 2023?

Item Path / command
Package / service httpd / httpd
Main config /​etc/​httpd/​conf/​httpd.​conf
Site configs /​etc/​httpd/​conf.​d/*.​conf
Default web root /var/www/html
Apache user apache
Logs /​var/​log/​httpd/​access_​log, /​var/​log/​httpd/​error_​log
Config test sudo apachectl configtest

For a site in its own folder, create a virtual host in /etc/httpd/conf.d/mysite.conf:

sudo mkdir -p /var/www/mysite && echo "<h1>My site</h1>" | sudo tee /var/www/mysite/index.html
sudo tee /etc/httpd/conf.d/mysite.conf > /dev/null <<'VHOST'
<VirtualHost *:80>
    ServerName mysite.local
    DocumentRoot /var/www/mysite
    <Directory /var/www/mysite>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog  /var/log/httpd/mysite_error.log
    CustomLog /var/log/httpd/mysite_access.log combined
</VirtualHost>
VHOST
sudo apachectl configtest && sudo service httpd reload

With name-based virtual hosts, the first <VirtualHost *:80> loaded answers any request whose name does not match another vhost, so this one also answers requests to the public IP. Options -Indexes stops Apache from listing a folder that has no index file.

Ravindra Bagale's Tip

💡 Make sudo apachectl configtest && sudo service httpd reload a habit. If the test fails, the reload doesn't happen, so one typo cannot take your site down. And when an error appears, don't guess – read sudo tail -n 20 /var/log/httpd/error_log; the file and line number are right there.

How do I fix common Apache errors on EC2?

Ghabru naka 😅 — find your symptom in the first column:

Symptom Likely cause Fix
Unit apache2.​service not found Wrong service name on Amazon Linux Use httpd: sudo service httpd start
Browser spins, then timeout Port 80 missing in the security group; no public IP; typed https:// Add inbound HTTP 80 from 0.0.0.0/0; use http://
Connection refused Apache is not running sudo service httpd start; sudo service httpd status
(98)Address already in use: AH00072: make_​sock: could not bind to address 0.​0.​0.​0:​80 Nginx or another server already uses port 80 sudo ss -​tlnp | grep :​80; sudo service nginx stop
403 Forbidden No index.html; wrong permissions Add index.html; folders 755, files 644
Test page instead of your page File not in /var/www/html or a vhost points elsewhere ls /var/www/html; check DocumentRoot
Config change has no effect Forgot to reload, or a syntax error sudo apachectl configtest && sudo service httpd reload

On the older Amazon Linux 2 the install command is the same (sudo yum install -y httpd). On Ubuntu use sudo apt install -y apache2 and the apache2 service name.

Got it? On Amazon Linux, Apache means httpd – install, start, enable, configtest and port 80 in the security group. That's all! Your page is live now. If you want WordPress next, this same Apache will be useful.

Frequently asked questions

How do I install Apache on Amazon Linux 2023?

Run sudo yum install -y httpd, then sudo service httpd start and sudo systemctl enable httpd. On Amazon Linux the package and the service are called httpd, not apache2.

Why does "sudo service apache2 start" fail on Amazon Linux?

Because the service is named httpd on Amazon Linux, CentOS and RHEL. apache2 is the name on Ubuntu and Debian, so Amazon Linux answers Unit not found.

Where is the Apache web root on Amazon Linux 2023?

The default web root is /var/www/html. The main config is /etc/httpd/conf/httpd.conf, site configs go in /etc/httpd/conf.d/*.conf and the logs are in /var/log/httpd/.

How do I check the Apache configuration for errors?

Run sudo apachectl configtest. It should print Syntax OK. Apply changes with sudo apachectl configtest && sudo service httpd reload.

Can Apache and Nginx run on the same EC2 instance?

Not both on port 80. Only one program can listen on a port, so stop one (sudo service nginx stop) before starting the other, or move one of them to a different port.

Does Apache start automatically after installation?

No, not on Amazon Linux 2023 or CentOS Stream 9. Start it with sudo service httpd start and enable it for every boot with sudo systemctl enable httpd. On Ubuntu, apache2 starts by itself.