13. Multiple Websites on One Server and HTTPS with Certbot
13.3 Multiple Sites with Apache (Amazon Linux 2023 and Ubuntu)
# 00-default.conf (loads first, so it is the fallback)
<VirtualHost *:80>
ServerName default.invalid
DocumentRoot /var/www/html
</VirtualHost>
# site1.conf
<VirtualHost *:80>
ServerName site1.example.com
DocumentRoot /var/www/site1
ErrorLog /var/log/httpd/site1_error.log
</VirtualHost>
# site2.conf
<VirtualHost *:80>
ServerName site2.example.com
DocumentRoot /var/www/site2
ErrorLog /var/log/httpd/site2_error.log
</VirtualHost>
# Ubuntu Apache: enable sites (log path ${APACHE_LOG_DIR})
sudo a2ensite site1.conf site2.conf
sudo a2dissite 000-default.conf # optional
sudo apache2ctl configtest && sudo service apache2 reload
Test without buying domains:
curl -H "Host: site1.example.com" http://localhost # Welcome to site1
curl -H "Host: site2.example.com" http://localhost # Welcome to site2
curl -I http://localhost # default block answers
From your laptop, add both names to your hosts file pointing to the server's Elastic IP, then open them in the browser. In Chapter 12 we'll replace this trick with real DNS records.
| Step | Amazon Linux 2023 | Ubuntu |
|---|---|---|
| Config folder | /etc/httpd/conf.d/site1.conf | /etc/apache2/sites-available/site1.conf |
| Enable | Automatic (any .conf in conf.d) | sudo a2ensite site1.conf |
| Log path | /var/log/httpd/site1_error.log | ${APACHE_LOG_DIR}/site1_error.log |
| Test | sudo apachectl configtest | sudo apache2ctl configtest |
| Reload | sudo service httpd reload | sudo service apache2 reload |
| Show vhosts | sudo apachectl -S | sudo apache2ctl -S |
Ravindra Bagale's Tip
Amazon Linux var conf.d madhe juni copy site1-old.conf naavane thevli tar Apache ti pan load karto – mag ekach naavache don vhosts! Khup students la he kalat nahi. Backup nehmi .bak ne sampva, .conf ne nahi.
Lab
Repeat the two-site setup with Apache on Ubuntu. Run sudo apache2ctl -S and identify the default virtual host.