Script de renouvellement automatique des certificats
Ce script gère la vérification et l'installation de certbot, puis lance le processsu de renouvellement des certificats SSL. Il assure également un enregistrement détaillé de chaque opération.
Créez ou éditez le fichier de script :
sudo vi /opt/bitnami/scripts/letsencrypt/renew.sh
Contenu du script :
#!/bin/bash
# Localisation du binaire certbot
CERTBOT_PATH=$(which certbot)
# Configuration du journalisation
LOG_DIR="/var/log/letsencrypt"
LOG_FILE="$LOG_DIR/renewal.log"
mkdir -p "$LOG_DIR"
# Enregistrement du début du processus
echo "==============================================" >> "$LOG_FILE"
echo "$(date) - Démarrage du processus de renouvellement de certificat" >> "$LOG_FILE"
# Vérification de la présence de certbot
if [ ! -x "$CERTBOT_PATH" ]; then
echo "$(date) - Erreur : certbot non trouvé ! Veuillez vérifier l'installation." >> "$LOG_FILE"
echo "$(date) - Tentative d'installation automatique de certbot..." >> "$LOG_FILE"
# Installation automatique (Debian/Ubuntu)
if command -v apt-get && [ -f /etc/debian_version ]; then
apt-get update && apt-get install -y certbot python3-certbot-apache >> "$LOG_FILE" 2>&1
# Installation automatique (CentOS/RHEL)
elif command -v yum && [ -f /etc/redhat-release ]; then
yum install -y epel-release && yum install -y certbot python3-certbot-apache >> "$LOG_FILE" 2>&1
fi
# Nouvelle vérification après tentative d'installation
CERTBOT_PATH=$(which certbot)
if [ ! -x "$CERTBOT_PATH" ]; then
echo "$(date) - Échec de l'installation automatique ! Veuillez installer certbot manuellement." >> "$LOG_FILE"
exit 1
else
echo "$(date) - certbot installé avec succès : $CERTBOT_PATH" >> "$LOG_FILE"
fi
fi
# Exécution du renouvellement du certificat
echo "$(date) - Lancement de la commande de renouvellement" >> "$LOG_FILE"
# Note: Utilisation du mode standalone, nécessitant l'arrêt d'Apache avant et son redémarrage après.
"$CERTBOT_PATH" renew --standalone --noninteractive --pre-hook "/opt/bitnami/scripts/apache2/stop.sh" --post-hook "/opt/bitnami/scripts/apache2/start.sh" >> "$LOG_FILE" 2>&1
# Vérification du statut de sortie
if [ $? -eq 0 ]; then
echo "$(date) - Succès : Certificat renouvelé." >> "$LOG_FILE"
else
echo "$(date) - Échec : Le renouvellement du certificat a échoué ! Veuillez consulter le journal." >> "$LOG_FILE"
fi
# Ajout d'une ligne vide pour séparer les entrées du journal
echo "" >> "$LOG_FILE"
Première obtention des certificats
Étant donné que l'environnement dispose d'Apache préinstallé et non géré par systemctl, le mode standalone de certbot est requis pour obtenir les certificats initiaux.
Exécutez la commande suivante en remplaçant sub.votredomaine.com et votre.email@votredomaine.com :
sudo certbot certonly --standalone -d sub.votredomaine.com --email votre.email@votredomaine.com --agree-tos --noninteractive
Les certificats seront stockés dans le répertoire /etc/letsencrypt/live.
Configuration d'Apache (VirtualHost)
Modifiez le fichier de configuration des hôtes virtuels :
sudo vim /opt/bitnami/apache2/conf/extra/httpd-vhosts.conf
Ajoutez ou adaptez la configuration suivante :
<VirtualHost *:80>
DocumentRoot "/var/www/html/votre_site"
ServerName sub.votredomaine.com
ErrorLog "logs/sub.votredomaine.com-error.log"
CustomLog "logs/sub.votredomaine.com-access.log" common
<Directory "/var/www/html/votre_site">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "/var/www/html/votre_site"
ServerName sub.votredomaine.com
ErrorLog "logs/sub.votredomaine.com-error.log"
CustomLog "logs/sub.votredomaine.com-access.log" common
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/sub.votredomaine.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/sub.votredomaine.com/privkey.pem
<Directory "/var/www/html/votre_site">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Planification de la tâche de renouvellement
Configurez une tâche cron pour exécuter le script de renouvellement périodiquement. Utilisez crontab -e pour éditer les tâches planifiées :
sudo crontab -e
Ajoutez la ligne suivante pour exécuter le script chaque dimanche à 3h00 du matin :
# Renouvellement hebdomadaire du certificat SSL
0 3 * * 0 /opt/bitnami/scripts/letsencrypt/renew.sh