diff --git a/deploy/server_setup.sh b/deploy/server_setup.sh index c0df161..2a119d9 100755 --- a/deploy/server_setup.sh +++ b/deploy/server_setup.sh @@ -172,6 +172,31 @@ else echo "Pas de certificat : relance avec --certbot adresse@mail, ou obtiens-le toi-même, puis relance ce script." fi +step "Renouvellement automatique du certificat (certbot)" +HOOK_DIR=/etc/letsencrypt/renewal-hooks/deploy +if [[ "${DRY_RUN}" == 1 ]]; then + echo "[dry-run] hook ${HOOK_DIR}/pyquest-reload-nginx (recharge nginx après chaque renouvellement)" + echo "[dry-run] systemctl enable --now certbot.timer (si l'unité existe)" +else + # Sans rechargement, nginx continuerait de servir l'ancien certificat jusqu'à son expiration. + install -d "${HOOK_DIR}" + printf '#!/bin/sh\nsystemctl reload nginx\n' > "${HOOK_DIR}/pyquest-reload-nginx" + chmod 755 "${HOOK_DIR}/pyquest-reload-nginx" + if systemctl list-unit-files certbot.timer >/dev/null 2>&1 && systemctl list-unit-files certbot.timer | grep -q certbot.timer; then + systemctl enable --now certbot.timer + echo "minuteur certbot actif : $(systemctl is-active certbot.timer)" + elif [[ -f /etc/cron.d/certbot ]]; then + echo "renouvellement par cron (/etc/cron.d/certbot)" + else + echo "ATTENTION : ni minuteur ni cron certbot trouvés : le certificat ne se renouvellera pas tout seul." >&2 + fi + if [[ -e "${CERT_DIR}/fullchain.pem" && ! -e "/etc/letsencrypt/renewal/${DOMAIN}.conf" ]]; then + echo "ATTENTION : le certificat de ${DOMAIN} n'a pas de configuration de renouvellement certbot (obtenu autrement ?)." >&2 + echo " Pour qu'il se renouvelle : certbot certonly --webroot -w ${WEB_ROOT}/letsencrypt -d ${DOMAIN} --keep-until-expiring --agree-tos -m adresse@mail" >&2 + fi + echo "Essai à blanc du renouvellement : certbot renew --dry-run" +fi + if [[ "${DRY_RUN}" == 0 ]] && systemctl is-active --quiet pyquest 2>/dev/null; then step "Redémarrage du service (le port ou l'unité a pu changer)" systemctl restart pyquest diff --git a/docs/site/en/host/operations.md b/docs/site/en/host/operations.md index 2e6d301..396d274 100644 --- a/docs/site/en/host/operations.md +++ b/docs/site/en/host/operations.md @@ -87,6 +87,28 @@ gunzip < /var/backups/pyquest/pyquest-YYYYMMDD-HHMMSS.sql.gz | mariadb pyquest Remember to move the backups off the VPS (copy to another machine). +### Let's Encrypt certificate: issuing and renewal + +`server_setup.sh --certbot you@mail` obtains the certificate (*webroot* method, without stopping nginx), installs a +**hook** that reloads nginx after each renewal and enables `certbot.timer` (automatic renewal, twice a day, effective in +the last 30 days of validity). Checks: + +```bash +certbot certificates # domains, expiry dates, certificate path +systemctl status certbot.timer # the timer is "active (waiting)" +certbot renew --dry-run # dry run of the renewal, changing nothing +``` + +If the certificate was obtained **another way** (by hand, with another tool, or copied), it has no renewal configuration +(`/etc/letsencrypt/renewal/.conf`) and will not renew: `vps_doctor.sh` flags it. To fix (port 80 must be +reachable, the domain must point to the server): + +```bash +certbot certonly --webroot -w /var/www/pyquest/letsencrypt -d pyquest.example.org --agree-tos -m you@mail +./deploy/server_setup.sh --domain pyquest.example.org # reinstalls the hook and the timer +certbot renew --dry-run +``` + ### Security updates `apt update && apt upgrade` regularly. After a Django upgrade, `./scripts/server_update.sh` reinstalls the dependencies and diff --git a/docs/site/fr/host/operations.md b/docs/site/fr/host/operations.md index 4d9d0b0..071dfe6 100644 --- a/docs/site/fr/host/operations.md +++ b/docs/site/fr/host/operations.md @@ -87,6 +87,28 @@ gunzip < /var/backups/pyquest/pyquest-AAAAMMJJ-HHMMSS.sql.gz | mariadb pyquest Pense à sortir les sauvegardes du VPS (copie vers une autre machine). +### Certificat Let's Encrypt : obtention et renouvellement + +`server_setup.sh --certbot adresse@mail` obtient le certificat (méthode *webroot*, sans arrêter nginx), installe un +**hook** qui recharge nginx après chaque renouvellement et active `certbot.timer` (renouvellement automatique, deux fois +par jour, effectif dans les 30 derniers jours de validité). Vérifications : + +```bash +certbot certificates # domaines, dates d'expiration, chemin du certificat +systemctl status certbot.timer # le minuteur est « active (waiting) » +certbot renew --dry-run # essai à blanc du renouvellement, sans rien changer +``` + +Si le certificat a été obtenu **autrement** (à la main, par un autre outil, ou copié), il n'a pas de configuration de +renouvellement (`/etc/letsencrypt/renewal/.conf`) et ne se renouvellera pas : `vps_doctor.sh` le signale. +Corriger (le port 80 doit être joignable, le domaine doit pointer vers le serveur) : + +```bash +certbot certonly --webroot -w /var/www/pyquest/letsencrypt -d pyquest.example.org --agree-tos -m toi@mail +./deploy/server_setup.sh --domain pyquest.example.org # réinstalle le hook et le minuteur +certbot renew --dry-run +``` + ### Mises à jour de sécurité `apt update && apt upgrade` régulièrement. Après une mise à jour de Django, `./scripts/server_update.sh` réinstalle les diff --git a/scripts/vps_doctor.sh b/scripts/vps_doctor.sh index ad976fd..a83f81d 100755 --- a/scripts/vps_doctor.sh +++ b/scripts/vps_doctor.sh @@ -87,7 +87,8 @@ else fi echo "Certificat" -domain="$(ls /etc/letsencrypt/live 2>/dev/null | grep -v README | head -n1 || true)" +domain="${SITE_HOST}" +[[ -d "/etc/letsencrypt/live/${domain}" ]] || domain="$(ls /etc/letsencrypt/live 2>/dev/null | grep -v README | head -n1 || true)" if [[ -n "${domain}" ]]; then end="$(as_root openssl x509 -enddate -noout -in "/etc/letsencrypt/live/${domain}/fullchain.pem" 2>/dev/null | cut -d= -f2)" if [[ -n "${end}" ]]; then @@ -96,11 +97,17 @@ if [[ -n "${domain}" ]]; then else info "certificat ${domain} non lisible sans root" fi + if [[ -f "/etc/letsencrypt/renewal/${domain}.conf" ]]; then + ok "configuration de renouvellement certbot présente pour ${domain}" + else + ko "pas de configuration de renouvellement pour ${domain} (certificat obtenu hors certbot ?) : voir la documentation, section certificat" + fi if systemctl is-active --quiet certbot.timer 2>/dev/null || [[ -f /etc/cron.d/certbot ]]; then ok "renouvellement automatique du certificat (certbot.timer ou cron)" else ko "renouvellement automatique absent : « systemctl enable --now certbot.timer » (puis « certbot renew --dry-run » pour tester)" fi + [[ -x /etc/letsencrypt/renewal-hooks/deploy/pyquest-reload-nginx ]] && ok "nginx rechargé après chaque renouvellement" || ko "hook de rechargement nginx absent : relancer ./deploy/server_setup.sh --domain ${domain}" else info "aucun certificat Let's Encrypt trouvé" fi diff --git a/tests/deploy/test_server_scripts.sh b/tests/deploy/test_server_scripts.sh index 5d17ba8..8e9b032 100755 --- a/tests/deploy/test_server_scripts.sh +++ b/tests/deploy/test_server_scripts.sh @@ -53,6 +53,8 @@ echo "${out}" | grep -q "\[dry-run\] .*apt-get install" || fail "le dry-run doit echo "${out}" | grep -q "db_setup.sh" || fail "les étapes restantes (base de données) doivent être listées" echo "${out}" | grep -q "server_update.sh" || fail "la publication du site doit être indiquée" echo "${out}" | grep -q "vhost complet" || fail "le dry-run doit traiter nginx" +echo "${out}" | grep -q "pyquest-reload-nginx" || fail "le dry-run doit annoncer le rechargement de nginx après renouvellement" +echo "${out}" | grep -q "certbot.timer" || fail "le dry-run doit annoncer le minuteur certbot" echo "${out}" | grep -q "${ROOT}/.venv-prod" || fail "l'environnement Python doit être dans le dépôt cloné (.venv-prod)" # Port local du backend : --port validé, et premier port libre choisi si 8001 est occupé. "${SETUP}" --domain pyquest.example.org --port 80 --dry-run >/dev/null 2>&1 && fail "port privilégié accepté"