It’s relatively straightforward to have multiple domains and sub-domains use the same Certbot certificate when they all point to the same document root. Adding a sub-domain that points somewhere else is not as easy.
I wanted to add a beta sub-domain for testing a site rewrite. I could get the certificate to generate, but I couldn’t figure out how to modify the Apache config files for the beta. This is how I did it.
To figure out what should be done, I ran this code to expand the existing certificate.
sudo /opt/certbot/certbot-auto --installer apache --webroot -w /www/example -d example.com,www.example.com --webroot -w /www/example_beta -d beta.example.com
To verify that it did what I wanted, I ran:
/opt/certbot/certbot-auto certificates
and got this:
Certificate Name: example.com
Domains: example.com beta.example.com www.example.com
Expiry Date: 2018-01-14 19:35:43+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/www.example.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/www.example.com/privkey.pem
I was originally looking at the example.com file in the sites-available directory, but what I should have been looking at was in the Certbot generated files that end in -le-ssl.conf.
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
ServerAdmin root@example.com
DocumentRoot /www/example
CustomLog /var/log/apache2/example.com.access_log combined
ErrorLog /var/log/apache2/example.com.error_log
ErrorDocument 404 /missing.php
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/www.example.com/chain.pem
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName beta.example.com
ServerAdmin root@touringmachine.com
DocumentRoot /www/example_beta
CustomLog /var/log/apache2/example.com.access_log combined
ErrorLog /var/log/apache2/example.com.error_log
ErrorDocument 404 /missing.php
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/www.example.com/chain.pem
</VirtualHost>
</IfModule>
You need to run this code after changing the config files.
sudo service apache2 restart
Don’t forget to change your DNS record to add the sub-domain.