Debian 11 Bullseye
Sponsored Link

Apache2 : Configure mod_md2021/09/07

 
Install and Configure [mod_md] to automate managing certificates from Let's Encrypt.
It's possible to configure each VirtualHost.
And it's not need to configure manual SSL/TLS setting like here for the Site with [mod_md].
Also it needs that it's possible to access from the Internet to the Site with [mod_md] because of verification from Let's Encrypt.
[1] Configure [mod_md].
root@www:~#
vi /etc/apache2/conf-available/acme.conf
# create new

MDBaseServer              on
MDCertificateProtocol     ACME
MDCAChallenges            http-01
MDDriveMode               auto
MDPrivateKeys             RSA 2048
MDRenewWindow             33%
MDStoreDir                md
MDCertificateAuthority    https://acme-v02.api.letsencrypt.org/directory
MDCertificateAgreement    https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf

<Location "/md-status">
    SetHandler md-status
    Require ip 127.0.0.1 10.0.0.0/24
</Location>

# [MDRenewWindow]
# default is [33%] if not specified
# if validity of certificates falls specified duration,
# [mod_md] will get new certificates
# 90 days * 33% ≒ 30 days
# if you'd like to set with day, specify [d]
# 30 days ⇒ [30d]

# [MDStoreDir]
# the directory certificates or other data are stored
# if not specified, default is [md]
# it is relative path from [ServerRoot] in [httpd.conf]

# [md-status]
# monitor MD status
[2] Configure each VirtualHost you'd like to set [mod_md].
It needs to specify valid email address for each [ServerAdmin] directive because Let's Encrypt will send various notification.
# for example, set on the site [rx-7.srv.world] site

root@www:~#
vi /etc/apache2/sites-available/rx-7.srv.world.conf
MDomain rx-7.srv.world
MDCertificateAgreement accepted
DirectoryIndex index.html
ServerAdmin root@www.srv.world

<VirtualHost *:80>
    DocumentRoot /var/www/rx-7.srv.world
    ServerName rx-7.srv.world
</VirtualHost>

<VirtualHost *:443>
    SSLEngine on
    DocumentRoot /var/www/rx-7.srv.world
    ServerName rx-7.srv.world
</VirtualHost>

root@www:~#
a2enmod md

Enabling module md.
To activate the new configuration, you need to run:
  systemctl restart apache2
root@www:~#
a2enconf acme

Enabling conf acme.
To activate the new configuration, you need to run:
  systemctl reload apache2
root@www:~#
a2ensite rx-7.srv.world

Enabling site rx-7.srv.world.
To activate the new configuration, you need to run:
  systemctl reload apache2

root@www:~#
systemctl restart apache2
# on initial start, some validation checks run and
# dumy certificate is created under the directory you set for [MDStoreDir]

root@www:~#
ll /etc/apache2/md/domains/rx-7.srv.world

total 20
drwx------ 2 root root 4096 Aug  3 07:22 ./
drwx------ 3 root root 4096 Aug  3 07:22 ../
-rw------- 1 root root 1119 Aug  3 10:49 fallback-cert.pem
-rw------- 1 root root 1704 Aug  3 10:49 fallback-privkey.pem
-rw------- 1 root root  473 Aug  3 10:49 md.json

# restart again

root@www:~#
systemctl restart apache2
# if all checks passed, valid certificate is gotten

root@www:~#
ll /etc/apache2/md/domains/rx-7.srv.world

total 28
drwx------ 2 root root 4096 Aug  3 07:23 ./
drwx------ 3 root root 4096 Aug  3 07:23 ../
-rw------- 1 root root 3913 Aug  3 10:50 job.json
-rw------- 1 root root  518 Aug  3 10:50 md.json
-rw------- 1 root root 1704 Aug  3 10:50 privkey.pem
-rw------- 1 root root 5591 Aug  3 10:50 pubcert.pem
[3] It's possible to confirm expiration date and others of certificate with [openssl] command like follows.
Or it's possible to see them to access to the URL of [md-status] you set on [2].
root@www:~#
openssl s_client -connect rx-7.srv.world:443 | openssl x509 -noout -startdate -enddate

depth=2 C = US, O = Internet Security Research Group, CN = ISRG Root X1
verify return:1
depth=1 C = US, O = Let's Encrypt, CN = R3
verify return:1
depth=0 CN = rx-7.srv.world
verify return:1
notBefore=Aug  3 00:49:39 2021 GMT
notAfter=Nov  1 00:49:37 2021 GMT
Matched Content