Wiki CgX

Parce que j'ai un cerveau, mais pas trop.

Outils pour utilisateurs

Outils du site


it:linux-selfhosting:letsencrypt

Let's Encrypt / certbot

Créer un certificat

Mode standalone

Le mode standalone exige qu'aucun autre serveur web (http/https) ne tourne sur le serveur :

certonly --standalone --renew-by-default --rsa-key-size 4096 -t --agree-tos -m email@domaine.tld -d www.domaine.tld -d domaine.tld
  • -d : Autant de hosts que nécéssaire

Script bash de création

letsencrypt_create_nomlibre

letsencrypt_create_nomlibre.sh
#!/bin/bash
 
cd "$(dirname $0)"
. $PWD/config.ini
 
DOMAIN=$1
 
if [ ! -f "/etc/letsencrypt/live/$DOMAIN/cert.pem" ] ; then
 
         /etc/init.d/apache2 stop
 
        $LETSENCRYPT_BIN certonly --standalone --renew-by-default --rsa-key-size 4096 -t --agree-tos --preferred-challenges http -m $MAINTAINER -d $DOMAIN
 
        if [ "$?" -ne "0" ] ; then
                echo "OOPS !! Ca a foiré pour /etc/letsencrypt/live/$domain"
        else
                echo "COOL !! Ca s'est bien passé ! Je recrée ce qu'il faut si besoin et je recharge les services qui vont bien"
        fi
 
        /etc/init.d/apache2 start
fi

Acme Challenge sans arrêter Apache

Générer un certificat wildcard avec Bind & DNS-01

C'est par ici !

Renouveler un certificat

Scripts bash de renouvellement

letsencrypt_autorenew en webroot (apache allumé)

letsencrypt_autorenew_webroot.sh
#!/bin/bash
 
cd "$(dirname $0)"
. $PWD/config.ini
 
DATE_TODAY=$(date +'%s')
cd /etc/letsencrypt/live || (echo "Can't cd to /etc/letsencrypt/live !" && exit -1)
RENEW=0
for domain in *
do
        if [ -f "$domain/cert.pem" ] ; then
                CERT="$domain/cert.pem"
                CERT_END_DATE=$(openssl x509 -in "$CERT" -noout -enddate | sed -e "s/.*=//")
                CERT_END_DATE_FORMAT=$(date --date="$CERT_END_DATE" --iso-8601)
                DATE_CERT=$(date -ud "$CERT_END_DATE" +"%s")
                DATE_JOURS_DIFF=$(( ( $DATE_CERT - $DATE_TODAY ) / (60*60*24) ))
 
                if [ "$1" == "check" ] ; then
                        SANCHECK=$(openssl x509 -in "$CERT"  -text|grep DNS:|sed -e "s/DNS:/ /g" -e "s/, / /g")
                        echo $CERT_END_DATE_FORMAT" => "$SANCHECK
                fi
 
                if [[ $DATE_JOURS_DIFF -le $NBJOURS_RENEW ]]; then
                        echo "########### Le certificat $domain expire dans $DATE_JOURS_DIFF jours. Alors on renouvelle..."
                        # Read the SAN (Subject Alt Names) for this cert (Warn: this code may not be super reliable :/ )
                        SAN=$(openssl x509 -in "$CERT"  -text|grep DNS:|sed -e "s/DNS:/-d /g" -e "s/, / /g")
                        echo "Domaines recensés dans le certificat : $SAN"
 
                        echo "C'est parti :"
                        $LETSENCRYPT_BIN certonly --webroot -w /data/www/ --renew-by-default --rsa-key-size 4096 --preferred-challenges http -t --agree-tos -m $MAINTAINER $SAN
 
                        if [ "$?" -ne "0" ] ; then
                                echo "OOPS !! Ca a foiré pour /etc/letsencrypt/live/$domain"
                        else
                                echo "COOL !! Ca s'est bien passé ! Je recrée ce qu'il faut si besoin et je recharge les services qui vont bien"
                                RENEW=1
                                POST_TRAITEMENT_SCRIPT=$(dirname $0)/letsencrypt_posttraitement_$domain
                                if [ -f $POST_TRAITEMENT_SCRIPT ] ; then
                                        $POST_TRAITEMENT_SCRIPT
                                fi
                        fi
 
                        /etc/init.d/apache2 restart
 
                fi
        fi
done

letsencrypt_autorenew en standalone (apache éteint)

letsencrypt_autorenew_standalone.sh
#!/bin/bash
 
cd "$(dirname $0)"
. $PWD/config.ini
 
DATE_TODAY=$(date +'%s')
cd /etc/letsencrypt/live || (echo "Can't cd to /etc/letsencrypt/live !" && exit -1)
 
for domain in *
do
    if [ -f "$domain/cert.pem" ] ; then
        CERT="$domain/cert.pem"
        CERT_END_DATE=$(openssl x509 -in "$CERT" -noout -enddate | sed -e "s/.*=//")
        DATE_CERT=$(date -ud "$CERT_END_DATE" +"%s")
        DATE_JOURS_DIFF=$(( ( $DATE_CERT - $DATE_TODAY ) / (60*60*24) ))
 
        if [ "$1" == "check" ] ; then
            SANCHECK=$(openssl x509 -in "$CERT"  -text|grep DNS:|sed -e "s/DNS:/ /g" -e "s/, / /g")
            echo $SANCHECK" => "$CERT_END_DATE
        fi
 
        if [[ $DATE_JOURS_DIFF -le $NBJOURS_RENEW ]]; then
            echo "########### Le certificat $domain expire dans $DATE_JOURS_DIFF jours. Alors on renouvelle..."
            # Read the SAN (Subject Alt Names) for this cert (Warn: this code may not be super reliable :/ )
            SAN=$(openssl x509 -in "$CERT"  -text|grep DNS:|sed -e "s/DNS:/-d /g" -e "s/, / /g")
            echo "Domaines recensés dans le certificat : $SAN"
 
            echo "C'est parti :"
            /etc/init.d/apache2 stop
            $LETSENCRYPT_BIN certonly --standalone --renew-by-default --rsa-key-size 4096 -t --agree-tos -m $MAINTAINER $SAN
 
            if [ "$?" -ne "0" ] ; then
                echo "OOPS !! Ca a foiré pour /etc/letsencrypt/live/$domain"
            else
                echo "COOL !! Ca s'est bien passé ! Je recrée ce qu'il faut si besoin et je recharge les services qui vont bien"
                POST_TRAITEMENT_SCRIPT=$HERE/letsencrypt_posttraitement_$domain
                if [ -f $POST_TRAITEMENT_SCRIPT ] ; then
                    $POST_TRAITEMENT_SCRIPT
                fi
            fi
 
            /etc/init.d/apache2 start
 
        fi
    fi
done

Effacer un certificat

Pour effacer proprement un certificat (dans /etc/letsencrypt)

certbot delete

Non interactif :

certbot delete --cert-name www.domaine.tld

Désactiver le timer certbot de systemd

Par défaut, la tâche de mise à jour des certificats est lancée par un timer systemd plusieurs fois par jour. Si on a besoin de la désactiver :

systemctl stop certbot.timer
systemctl disable certbot.timer

Pourquoi ça marche pas ?

Lors de la création d'un certificat, plusieurs choses peuvent l'empécher, et c'est pas toujours évident de savoir quoi. Posez-vous les bonnes questions

  1. Le certbot est-il à jour ?
  2. Le service est-il disponible au niveau réseau ? (port 80 ouvert, firewall bien configuré…)
  3. Les certificats demandés pointent-ils bien TOUS vers le serveur qui fait la demande ? (L'IP si domaine.tld)
  4. N'y a-t'il pas plusieurs réponses pour un enregistrement de zone donné ? (plusieurs CNAME, plusieurs A)
  5. N'y a-t'il pas plusieurs réponses pour un @ de zone donné ? (un A et un AAAA)
  6. Le service est-il disponible en IPv6 avec un enregistrement en AAAA ?
it/linux-selfhosting/letsencrypt.txt · Dernière modification : 30 May 2022 :: 12:03 de CgX