Passer au contenu principal

Mise en place d’un certificat Let’s Encrypt avec Apache sur Debian 10

Introduction

Let’s Encrypt est une autorité de certification permettant d’obtenir et d’installer facilement des certificats SSL gratuits, permettant ainsi le chiffrement HTTPS sur votre serveur web.
Dans cet article, vous allez utiliser certbot pour obtenir un certificat SSL gratuit pour Apache sur Debian 10.

Pré-requis

Pour suivre cet article, vous aurez besoin de :

- Un serveur avec le système d’exploitation Debian 10 installé.
- Un nom de domaine enregistré, dans cet article nous utiliserons « your_domain » en tant qu’exemple.
- Le service Apache installé sur votre serveur avec un virtual host configuré pour votre domaine.

Installation de Certbot

La première étape pour l'utilisation de Let's Encrypt est l'installation de Certbot sur votre serveur.
Afin d'installer ce dernier, vous allez devoir ajouter le repos backport.
Ajouter donc cette ligne au niveau du fichier /etc/apt/sources.list :

deb http://ftp.debian.org/debian buster-backports main

Sauvegarder le fichier et mettre a jour la liste la des paquets :

apt-get update

Enfin procéder à l'installation de certbot:

apt-get install python-certbot-apache -t buster-backports

Certbot étant maintenant installé, nous allons voir pour la mise en place d'un vhost Apache.

Mise en place d'un vhost Apache

Si ce n'est pas déjà fait, créer un vhost Apache pour le domaine que vous souhaitez protéger par un certificat Let's Encrypt.
Ces fichiers de configurations se trouvent dans /etc/apache2/sites-available.

Créer un fichier de configuration pour votre domaine:

vi /etc/apache2/sites-available/your_domain.conf

Voici un exemple de fichier de configuration Apache:

<VirtualHost *:80>
        ServerName your_domain
        ServerAlias www.yur_domain
        DocumentRoot /var/www/your_domain
        <Directory /var/www/your_domain>
                Options -Indexes +FollowSymLinks -MultiViews
                AllowOverride all
                Order allow,deny
                allow from all
        </Directory>
        ErrorLog /var/log/apache2/your_domain.error.log
        CustomLog /var/log/apache2/your_domain.access.log combined
</VirtualHost>

Activer ce vhost au niveau Apache:

a2ensite your_domain.conf

Tester votre configuration et redémarrer Apache:

apache2ctl -t
systemctl reload apache2

Obtenir le certificat Let's Encrypt

Maintenant que votre vhost Apache et configuré et que certbot est correctement installé sur votre serveur, nous allons voir comment obtenir et installé le certificat Let's Encrypt.

Effectuer la commande suivante sur votre serveur afin d'obtenir le certificat SSL via le plugin Apache de Certbot:

certbot --apache -d your_domain -d www.your_domain

Si vous lancez certbot pour la première fois, vous aller être inviter à rentrer une adresse mail et accepter les conditions d’utilisation de certbot. Il vous demandera également si vous souhaitez partager vos informations avec l’organisation gérant certbot.

Après cela, certbot lancera la vérification/création de votre certificat SSL Lets Encrypt.
Si cette vérification est réussie, il vous demandera alors comment vous souhaitez configurer vos paramètres HTTPS:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

Faites votre choix entre les deux solutions proposées par certbot et validé ce choix.
Certbot terminera ensuite ces actions avec un message vous indiquant que le processus de génération de votre certificat SSL est terminé et vous indiquera ou est stocké votre certificat :

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/your_domain/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/your_domain/privkey.pem
   Your cert will expire on 2019-10-20. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - 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

Votre certificat Let's Encrypt a été généré correctement.

Vérifier la mise en place de votre certificat Lets Encrypt au niveau du vhost Apache

Voici un exemple de configuration d’un vhost Apache http/https avec un certificat SSL lets encrypt et une redirection http -> https :

<VirtualHost *:80>
	...
        RewriteEngine on
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
	...
</VirtualHost>

<VirtualHost *:443>
	...
        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/your_domain/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/your_domain/privkey.pem
	...
</VirtualHost>

Une fois cette configuration vérifier au niveau d’Apache pour votre domaine.
Tester votre configuration et redémarrer Apache:

apache2ctl -t
systemctl reload apache2

Garder a l'esprit qu'un certificat Let's Encrypt n'est valable que 90 jours, vous pouvez donc bien évidemment mettre en place une tache planifiée pour le renouvellement de votre certificat sur votre serveur. Cette installation de certbot avec le plugin Apache, installe normalement par elle même dans /etc/cron.d une tache de renouvellement automatique pour votre certificat SSL.

Vous pouvez tester le renouvellement de votre certificat via la commande suivante:

certbot renew --dry-run

Par la suite, vous pouvez tester un accès a votre site via le protocole https, vous pourrez également vérifier la mise en place de votre certificat SSL via le site de test SSL Labs (https://www.ssllabs.com/ssltest/)