Ravindra BagaleCourses & study guides

8. Hosting a Static Website and Changing the Configuration

8.3 Amazon Linux 2023 + Apache

sudo service nginx stop 2>/dev/null     # free port 80 if Nginx was installed
sudo systemctl disable nginx 2>/dev/null
sudo yum install -y httpd
sudo service httpd start
sudo systemctl enable httpd

sudo tee /etc/httpd/conf.d/mysite.conf > /dev/null <<'EOF'
<VirtualHost *:80>
    ServerName mysite.local
    DocumentRoot /var/www/mysite
    <Directory /var/www/mysite>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    ErrorDocument 404 /404.html
    ErrorLog  /var/log/httpd/mysite_error.log
    CustomLog /var/log/httpd/mysite_access.log combined
</VirtualHost>
EOF

sudo apachectl configtest && sudo service httpd reload
curl -s http://localhost | head -5

How Apache picks the site

With name-based virtual hosts, the first <VirtualHost *:80> loaded is used for any request whose Host does not match another vhost. Since ours is the only vhost, it answers requests to the public IP too. Add ServerAlias www.example.com when you have a domain.

Ravindra Bagale's Tip

If Nginx is already installed, Apache won't start, and students don't understand the error. Check who is using port 80 with sudo ss -tlnp | grep :80. Only one web server can use port 80 at a time.

Lab

Switch the same instance to Apache and host mysite. Confirm Options -Indexes works by creating an empty folder /var/www/mysite/test and opening /test/ in the browser – you should get 403, not a file list.