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
On Amazon Linux, if you keep an old copy in conf.d named site1-old.conf, Apache loads that too – and you get two vhosts with the same name! Many students don't realise this. Always end backups with .bak, not .conf.
Ravindra Bagale's Tip – मराठी
Amazon Linux वर conf.d मध्ये जुनी copy site1-old.conf नावाने ठेवली तर Apache ती पण load करतो – मग एकाच नावाचे दोन vhosts! बऱ्याच students ना हे कळत नाही. Backup नेहमी .bak ने संपवा, .conf ने नाही.
Ravindra Bagale's Tip – हिंदी
Amazon Linux पर conf.d में पुरानी copy site1-old.conf नाम से रखी तो Apache उसे भी load करता है – फिर एक ही नाम के दो vhosts! बहुत से students को यह पता नहीं चलता. Backup हमेशा .bak पर ख़त्म करो, .conf पर नहीं.
Lab
Repeat the two-site setup with Apache on Ubuntu. Run sudo apache2ctl -S and identify the default virtual host.