Apache2.4 + Let's Encrypt

Apache 2.4 + Letsencrypt

On passe par la suite à la création d’un fichier de configuration /etc/apache2/conf-available/letsencrypt-include.conf :

<IfModule mod_ssl.c>
SSLEngine on
SSLProtocol             all -SSLv2 -SSLv3
SSLCipherSuite          ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA
SSLHonorCipherOrder     on
SSLCompression          off
SSLOptions +StrictRequire
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined
LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common
BrowserMatch "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4
</IfModule>

qui ne sera pas activé par défaut mais inclus dans les VHosts SSL, et un autre fichier /etc/apache2/conf-available/letsencrypt-well-known.conf :

<IfModule mod_proxy.c>
ProxyPass /.well-known !
</IfModule>
Alias /.well-known/acme-challenge /srv/www/.well-known/acme-challenge/
<Directory "/srv/www/.well-known/acme-challenge/">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>

que l’on va activer via la commande :

a2enconf letsencrypt-well-known
service apache2 restart

on modifie la configuration par défaut (changer les paramètres avec vos données) du fichier de configuration de certbot /etc/letsencrypt/cli.ini :

# Because we are using logrotate for greater flexibility, disable the
# internal certbot logrotation.
max-log-backups = 0
# Adjust interactive output regarding automated renewal
preconfigured-renewal = True
# Use a 4096 bit RSA key instead of 2048
rsa-key-size = 4096
# Uncomment and update to register with the specified e-mail address
email = toto@mymail.com
# Uncomment to use a text interface instead of ncurses
text = True
# Uncomment to use the webroot authenticator. Replace webroot-path with the
# path to the public_html / webroot folder being served by your web server.
authenticator = webroot
webroot-path = /srv/www

On créé le fichier de configuration /etc/apache2/conf-available/options-force-ssl.conf pour forcer https quand le mod_ssl est actif sur apache2 serveur f :

<IfModule mod_ssl.c>
RewriteEngine On
RewriteCond %{HTTPS} !=On
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</IfModule>

puis on modifie le fichier du Virtualhost par défaut qui devrait correspondre au hostname par défaut du serveur /etc/apache2/site-available/000-default.conf :

<VirtualHost *:80>
ServerName monhost.toto.com
ServerAdmin webmaster@localhost
DocumentRoot /srv/www/root
<Directory /srv/www/root/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
<IfModule mod_ssl.c>
Include /etc/apache2/conf-available/options-force-ssl.conf
</IfModule>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/000-error.log
CustomLog ${APACHE_LOG_DIR}/000-access.log combined
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName monhost.toto.com
ServerAdmin webmaster@localhost
DocumentRoot /srv/www/root
<Directory /srv/www/root/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
Include /etc/apache2/conf-available/letsencrypt-include.conf
SSLCertificateKeyFile    /etc/letsencrypt/live/monhost.toto.com/privkey.pem
SSLCertificateFile       /etc/letsencrypt/live/monhost.toto.com/cert.pem
SSLCertificateChainFile  /etc/letsencrypt/live/monhost.toto.com/chain.pem
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/000-error.log
CustomLog ${APACHE_LOG_DIR}/000-access.log combined
</VirtualHost>
</IfModule>

Comme la configuration l’indique, les échanges entre le/les serveurs letsencrypt et notre serveur vont se faire via des fichier générés dans le répertoire /srv/www/.well-known/acme-challenge/ qui doit appartenir à l’utilisateur du serveur apache2. Ce répertoire devra donc être créé avec les bon droits :

mkdir -p /srv/www/.well-known/acme-challenge/
mkdir /srv/www/root
chown -R www-data:www-data /srv/www

Nous pouvons maintenant générer le certificat :

certbot certonly --text --renew-by-default --expand --agree-tos -c /etc/letsencrypt/cli.ini --cert-name monhost.toto.com -d monhost.toto.com

qui devrai retourner le résulat suivant:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Account registered.
Requesting a certificate for monhost.toto.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/monhost.toto.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/monhost.toto.com/privkey.pem
This certificate expires on 2024-01-02.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

On devra activer par la suite le virtualhost par défaut (si ce n’est pas déjà fait) ainsi que le module ssl et redémarrer apache pour la prise en compte des nouvelles configurations:

a2ensite 000-default
a2enmod ssl
service apache2

Normalement tout devait bien se passer, mais dans le cas d’une méchante petite erreur qui empêcherai apache de démarrer, il faudrait soit consulter les fichiers logs dans le repertoire /var/log/apache2/, soit les journaux apache via la commande :

journalctl -u apache2.service

qui devraient fournir toutes les informations relatives à l’erreur.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.