{"id":885,"date":"2022-05-08T17:48:26","date_gmt":"2022-05-08T15:48:26","guid":{"rendered":"https:\/\/techtocraft.xyz\/?p=885"},"modified":"2023-08-09T11:18:04","modified_gmt":"2023-08-09T09:18:04","slug":"certificat-ssl-gratuit-par-lets-encrypt","status":"publish","type":"post","link":"https:\/\/hinakuu.xyz\/?p=885","title":{"rendered":"Debian Apache &#038; certificat SSL gratuit par Let&rsquo;s Encrypt"},"content":{"rendered":"\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"380\" src=\"https:\/\/techtocraft.xyz\/wp-content\/uploads\/2022\/05\/How-to-Install-Lets-Encrypt-SSL-Certificate.png\" alt=\"\" class=\"wp-image-916\" srcset=\"https:\/\/hinakuu.xyz\/wp-content\/uploads\/2022\/05\/How-to-Install-Lets-Encrypt-SSL-Certificate.png 728w, https:\/\/hinakuu.xyz\/wp-content\/uploads\/2022\/05\/How-to-Install-Lets-Encrypt-SSL-Certificate-300x157.png 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">Pr\u00e9sentation<\/mark><\/strong><\/h4>\n\n\n\n<p>Depuis quelques ann\u00e9es maintenant, il est possible de b\u00e9n\u00e9ficier d&rsquo;un <strong>certificat SSL gratuitement par l&rsquo;interm\u00e9diaire de Let&rsquo;s Encrypt<\/strong>. Cette autorit\u00e9 de certification publie des certificats reconnus par les navigateurs. Les certificats d\u00e9livr\u00e9s sont valables pour une dur\u00e9e de 90 jours mais il est facile de mettre en place un renouvellement automatique pour \u00e9viter tout d\u00e9sagr\u00e9ment.<\/p>\n\n\n\n<p><strong>Dans ce tutoriel, nous allons effectuer une demande de certificat Let&rsquo;s Encrypt et l&rsquo;int\u00e9grer sur un site sous Apache, le tout sous Debian.&nbsp;<\/strong>On s&rsquo;appuiera sur l&rsquo;outil certbot.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">Installation des d\u00e9pendances et de certbot<\/mark><\/strong><\/p>\n\n\n\n<p>Commencez par installer des d\u00e9pendances sur votre serveur :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">sudo apt-get update<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">sudo apt-get install software-properties-common<\/mark><\/pre>\n\n\n\n<p>Pour l&rsquo;installation du paquet \u00ab\u00a0<strong>python-certbot-apache<\/strong>\u00ab\u00a0, on va aller piocher dans les paquets backports de Debian Stretch. Ceci nous permettra d&rsquo;avoir une version encore plus r\u00e9cente du paquet. J&rsquo;ai utilis\u00e9 cette m\u00e9thode pour l&rsquo;installation car sinon j&rsquo;obtenais une erreur de Python lors de l&rsquo;utilisation de certbot.<\/p>\n\n\n\n<p>Cette m\u00e9thode implique d&rsquo;ajouter une source de paquets suppl\u00e9mentaire :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">sudo vim \/etc\/apt\/sources.list<\/mark><\/pre>\n\n\n\n<p>Int\u00e9grez cette ligne au fichier sources.list :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">deb http:\/\/ftp.debian.org\/debian stretch-backports main<\/mark><\/pre>\n\n\n\n<p>On va proc\u00e9der \u00e0 l&rsquo;actualisation des paquets et \u00e0 l&rsquo;installation du paquet python-certbot-apache \u00e0 partir du d\u00e9p\u00f4t que l&rsquo;on a renseign\u00e9 pr\u00e9c\u00e9demment. Voici la commande \u00e0 utiliser :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">sudo apt-get update<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">sudo apt-get install python-certbot-apache -t stretch-backports<\/mark><\/pre>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Demander un certificat avec certbot<\/mark><\/strong><\/p>\n\n\n\n<p>Lorsque l&rsquo;installation sera termin\u00e9e, nous allons utiliser Certbot pour effectuer une demande de certificat.<\/p>\n\n\n\n<p>Un challenge se d\u00e9clenche pendant la demande de certificat pour v\u00e9rifier que la demande est valide et le site web bien disponible sur Internet. Pour que certbot utilise son propre serveur web int\u00e9gr\u00e9, on va utiliser l&rsquo;option \u00ab\u00a0<strong>&#8211;standalone<\/strong>\u00ab\u00a0. On va ensuite lui indiquer, via l&rsquo;option \u00ab\u00a0<strong>&#8211;preferred-challenges<\/strong>\u00ab\u00a0, de faire son challenge sur le port 80 avec la valeur <em>http<\/em> ou 443 pour HTTPS avec la valeur <em>tls-sni<\/em>. Enfin, on va devoir pr\u00e9ciser le domaine pour lequel on effectue la demande de certificat, ceci s&rsquo;effectue via l&rsquo;option \u00ab\u00a0<strong>-d<\/strong>\u00ab\u00a0.<\/p>\n\n\n\n<p>Voici un exemple de commande pour le domaine \u00ab\u00a0www.mon-domaine.fr\u00a0\u00bb :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">sudo certbot certonly --standalone --preferred-challenges http -d www.mon-domaine.fr<\/mark>\n\n<strong>\/!\\ Attention si vous rencontrez l'erreur : <\/strong>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Problem binding to port 80: Could not bind to IPV4 or IPV6<\/mark>\n\nArr\u00eater le service Apache puis relancer la commande\n\nsystemctl stop apache2<\/pre>\n\n\n\n<p>Pendant le processus de demande de certificat, vous devrez indiquer une adresse e-mail. Celle-ci est notamment utilis\u00e9e pour vous avertir lorsque le certificat arrive bient\u00f4t \u00e0 expiration.<\/p>\n\n\n\n<p>Maintenant, on a en notre possession les fichiers n\u00e9cessaires \u00e0 la mise en place du certificat SSL. Ces fichiers se situent \u00e0 l&#8217;emplacement suivant : <strong>\/etc\/letsencrypt\/live\/&lt;votre-domaine&gt;\/<\/strong><\/p>\n\n\n\n<p><strong>Vous y trouverez trois fichiers : cert.pem, privkey.pem et chain.pem.<\/strong><\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Configurer le vhost d&rsquo;Apache<\/mark><\/strong><\/p>\n\n\n\n<p>Il faut que l&rsquo;on adapte notre VirtualHost Apache du site SSL pour int\u00e9grer le chargement des fichiers du certificat. Il est n\u00e9cessaire d&rsquo;utiliser cinq options diff\u00e9rentes, comme on le ferait avec tout certificat SSL.<\/p>\n\n\n\n<p>Voici les lignes \u00e0 int\u00e9grer au vHost (adaptez le chemin selon votre domaine) :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SSLEngine on\nSSLProtocol all -SSLv2\nSSLCertificateFile \/etc\/letsencrypt\/live\/www.mon-domaine.fr\/cert.pem\nSSLCertificateKeyFile \/etc\/letsencrypt\/live\/www.mon-domaine.fr\/privkey.pem\nSSLCertificateChainFile \/etc\/letsencrypt\/live\/www.mon-domaine.fr\/chain.pem<\/pre>\n\n\n\n<p>Enregistrez la configuration.<br>Activez le module SSL sur Apache avec la commande suivante :<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">sudo a2enmod ssl<\/mark><\/pre>\n\n\n\n<p>Enfin, si besoin, activez votre site Apache SSL. Par exemple, si le fichier de configuration du vHost se nomme \u00ab\u00a0www.mon-domaine.fr\u00a0\u00bb :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">sudo a2ensite www.mon-domaine.fr.conf<\/mark><\/pre>\n\n\n\n<p>Pour passer en production cette nouvelle configuration, nous allons recharger Apache :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">sudo systemctl reload apache2<\/mark><\/pre>\n\n\n\n<p>Pour terminer, nous allons automatiser le renouvellement du certificat avec un script en t\u00e2che planifi\u00e9e.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">Renouvellement via certbot<\/mark><\/strong><\/p>\n\n\n\n<p>On va cr\u00e9er un fichier sh contenant notre script :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">nano \/usr\/bin\/certbot-renew.sh<\/mark><\/pre>\n\n\n\n<p>Comme il s&rsquo;agit d&rsquo;un script, le fichier doit \u00eatre ex\u00e9cutable, on ajoute ce droit :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">chmod +x \/usr\/bin\/certbot-renew.sh<\/mark><\/pre>\n\n\n\n<p>Enfin, voici le contenu du script qui va simplement ex\u00e9cuter le binaire <em>certbot<\/em> avec l&rsquo;option <em>renew<\/em>. <\/p>\n\n\n\n<p>Un fichier de log sera g\u00e9n\u00e9r\u00e9 au chemin <em>\/var\/log\/certbot-renew.log<\/em>\u00ab\u00a0.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">#!\/bin\/bash<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">cd \/usr\/bin\n.\/certbot renew >> \/var\/log\/certbot-renew.log\nservice apache2 reload\nexit 0<\/mark><\/pre>\n\n\n\n<p>Pour lancer automatiquement le script, on va \u00e9diter la crontab (gestion des t\u00e2ches planifi\u00e9es) :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">crontab -e<\/mark><\/pre>\n\n\n\n<p>L&rsquo;exemple ci-dessous permet d&rsquo;ex\u00e9cuter l&rsquo;action de renouvellement tous les jours \u00e0 05 heures du matin. Le certificat sera renouvel\u00e9 si n\u00e9cessaire et non tous les jours.<\/p>\n\n\n\n<p>Ce qui donne :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0 5<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\"> <\/mark>* * * \/usr\/bin\/certbot-renew.sh<\/pre>\n\n\n\n<p>Il ne reste plus qu&rsquo;\u00e0 enregistrer la crontab.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Renouvellement d\u2019un certificat let\u2019s encrypt &amp; redirection de port<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">certbot-renews.sh \n\nou \n\nsudo certbot renew<\/pre>\n\n\n\n<p>Si vous avez plusieurs certificats pour diff\u00e9rents domaines et que vous souhaitez renouveler un certificat sp\u00e9cifique, utilisez\u00a0:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">certbot certonly --force-renew -d example.com<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Cas d&rsquo;erreur :<\/h3>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Failed to renew certificate mon.site.com with error: Problem binding to port 80: Could not bind to IPv4 or IPv6. All renewals failed. The following certificates could not be renewed: \/etc\/letsencrypt\/live\/mon.site.com\/fullchain.pem (failure) 1 renew failure(s), 0 parse failure(s)<\/mark><\/strong><\/p>\n\n\n\n<p>Pour renouveler le certificat SSL vous devez d\u00e9sactiver apache le temps de lancer la commande :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">systemctl stop apache2\ncertbot-renews.sh\nsystemctl start apache2<\/pre>\n\n\n\n<p>Autre possibilit\u00e9 :<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>v\u00e9rifier les r\u00e8gles de votre pare-feu et le port forward<\/li>\n\n\n\n<li>v\u00e9rifier si le port 80 est bien configurer dans le fichier \/etc\/apache2\/ports.conf<\/li>\n\n\n\n<li>v\u00e9rifier si le port 80 est bien configurer dans \/etc\/apache2\/sites-available\/votre.site.conf<\/li>\n<\/ul>\n\n\n\n<p>Pour v\u00e9rifier que le certificat a \u00e9t\u00e9 renouvel\u00e9, ex\u00e9cutez\u00a0:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo certbot renew --dry-run<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Pr\u00e9sentation Depuis quelques ann\u00e9es maintenant, il est possible de b\u00e9n\u00e9ficier d&rsquo;un certificat SSL gratuitement par l&rsquo;interm\u00e9diaire de Let&rsquo;s<\/p>\n","protected":false},"author":1,"featured_media":856,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-885","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=\/wp\/v2\/posts\/885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=885"}],"version-history":[{"count":28,"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=\/wp\/v2\/posts\/885\/revisions"}],"predecessor-version":[{"id":2103,"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=\/wp\/v2\/posts\/885\/revisions\/2103"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=\/wp\/v2\/media\/856"}],"wp:attachment":[{"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hinakuu.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}