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

Nginx aadhi install asel tar Apache start hot nahi aani students la error samjat nahi. sudo ss -tlnp | grep :80 ne port 80 kon vaprtoy te bagha. Ekaveli ekach web server port 80 var.

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.