From a5b52a0da9bc555eb6ce0820707db5fb3f2c1e39 Mon Sep 17 00:00:00 2001 From: Bob Perper Date: Tue, 6 Oct 2026 11:30:26 -0400 Subject: [PATCH] acme: use OpenLiteSpeed's built-in ACME with optional www What changed: - bin/acme.sh now has OpenLiteSpeed request and renew certificates instead of running acme.sh in the container's root account. - A domain is moved from the docker template to a new dockerAcme template whose vhssl block enables ACME; --revoke, --remove and --uninstall move it back. - docker-compose.yml mounts ./lsws/acme at /usr/local/lsws/acme. - bin/container/certhookctl.sh is removed and the README is updated. Why: - OpenLiteSpeed ACME adds www. to a certificate only when it is reachable, so a domain without a working www record still gets a certificate, and www is added at a later renewal. - The old renewals and the restart hook relied on cron, which the container never starts, so certificates were never renewed. How: - install_acme.sh and uninstall_acme.sh from the image run in throwaway containers that share the conf and acme volumes: the installer skips an existing acme directory (always a mount point here), and acme.sh --uninstall would also remove root's acme.sh in the litespeed container. - After the restart, the script follows the [ACME] lines in error.log until the certificate is replaced or the request fails, then checks that port 443 serves the new certificate. - Manual renew and revoke run the installed acme.sh as lsadm with the same environment OpenLiteSpeed uses. Co-Authored-By: Claude Opus 5 --- README.md | 35 ++- bin/acme.sh | 560 ++++++++++++++++++++++++++++------- bin/container/certhookctl.sh | 18 -- docker-compose.yml | 1 + 4 files changed, 488 insertions(+), 126 deletions(-) delete mode 100755 bin/container/certhookctl.sh diff --git a/README.md b/README.md index 4a9b39c3..5a42bc2d 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ The docker image installs the following packages on your system: |MariaDB|[Latest Stable version: 13.0 LTS](https://hub.docker.com/_/mariadb)| |PHP|[Latest version](http://rpms.litespeedtech.com/debian/)| |LiteSpeed Cache|[Latest from WordPress.org](https://wordpress.org/plugins/litespeed-cache/)| -|ACME|[Latest from ACME official](https://github.com/acmesh-official/get.acme.sh)| +|ACME|[OpenLiteSpeed built-in ACME](https://docs.openlitespeed.org/config/advanced/acme/), using [acme.sh](https://github.com/acmesh-official/acme.sh)| |WordPress|[Latest from WordPress](https://wordpress.org/download/)| |phpMyAdmin|[Latest from dockerhub](https://hub.docker.com/r/phpmyadmin/phpmyadmin/)| |Redis|[Latest from dockerhub](https://hub.docker.com/_/redis/)| @@ -67,6 +67,7 @@ Cloned project │   ├── lsrestart.log │   └── stderr.log ├── lsws +│   ├── acme │   ├── admin-conf │   └── conf ├── sites @@ -76,7 +77,9 @@ Cloned project └── docker-compose.yml ``` -* `acme` contains all applied certificates from Lets Encrypt +* `acme` contains certificates applied by older versions of `bin/acme.sh` + +* `lsws/acme` contains the acme.sh copy used by OpenLiteSpeed ACME, and `lsws/conf/cert/acme` its account and certificates * `bin` contains multiple CLI scripts to allow you add or delete virtual hosts, install applications, upgrade, etc @@ -178,6 +181,8 @@ Go to [WordPress > LSCache Plugin > Cache > Object](https://docs.litespeedtech.c ### Install ACME +Certificates are requested and renewed by [OpenLiteSpeed ACME](https://docs.openlitespeed.org/config/advanced/acme/). It needs the `./lsws/acme:/usr/local/lsws/acme` volume from `docker-compose.yml`, so run `docker compose up -d` after updating this project. + We need to run the ACME installation command the **first time only**. With email notification: @@ -187,23 +192,39 @@ bash bin/acme.sh [-I, --install] [-E, --email] EMAIL_ADDR ### Applying a Let's Encrypt Certificate -Use the root domain in this command, and it will check for a certificate and automatically apply one with and without `www`: +Use the root domain in this command. It must already be added with `bin/domain.sh`, and its DNS must point to this server with port 80 open: ```bash bash bin/acme.sh [-D, --domain] example.com ``` +The domain is moved to the `dockerAcme` template, OpenLiteSpeed is restarted and requests the certificate, and the script waits for the result. Certificates are stored in `lsws/conf/cert/acme/certs`. + +`www.example.com` is added to the certificate when OpenLiteSpeed can reach it, otherwise the certificate is issued for `example.com` only and `www` is added at a later renewal once it is reachable. This needs an OpenLiteSpeed version with optional `www` support; with older versions `www.example.com` must resolve and reach this server, or no certificate is issued. + +Notes: + +* OpenLiteSpeed checks `www` from inside the container through the address it resolves to, so the host or router must allow connecting back to its own public address. + +* If a domain has an AAAA record, it must reach this server over IPv6 too. + +* OpenLiteSpeed checks the certificates daily and renews them when they are due, but serves a renewed certificate after its next restart. Restart it regularly from the host's crontab, for example weekly: `0 3 * * 0 cd /path/to/ols-docker-env && bash bin/webadmin.sh -R` + +* Certificates from older versions of this script were stored in `acme`, and were not renewed because the container does not run cron. Run `bash bin/acme.sh -D example.com` again to switch a domain to OpenLiteSpeed ACME. + Other parameters: -* [`-r`, `--renew`]: Renew a specific domain with -D or --domain parameter if posibile. To force renew, use -f parameter. +* [`-r`, `--renew`]: Renew a specific domain with -D or --domain parameter if it is due. To force renew, use -f parameter. -* [`-R`, `--renew-all`]: Renew all domains if possible. To force renew, use -f parameter. +* [`-R`, `--renew-all`]: Renew all domains if they are due. To force renew, use -f parameter. * [`-f`, `-F`, `--force`]: Force renew for a specific domain or all domains. -* [`-v`, `--revoke`]: Revoke a domain. +* [`-v`, `--revoke`]: Revoke the certificate of a domain and move it back to the `docker` template. + +* [`-V`, `--remove`]: Move a domain back to the `docker` template. OpenLiteSpeed deletes its unused certificate at the next start. -* [`-V`, `--remove`]: Remove a domain. +* [`-U`, `--uninstall`]: Move all domains back to the `docker` template and uninstall OpenLiteSpeed ACME. ### Using mkcert for Local Development SSL diff --git a/bin/acme.sh b/bin/acme.sh index 80e4bc4f..af195861 100755 --- a/bin/acme.sh +++ b/bin/acme.sh @@ -1,19 +1,23 @@ #!/usr/bin/env bash EMAIL='' -NO_EMAIL='' DOMAIN='' INSTALL='' UNINSTALL='' -TYPE=0 CONT_NAME='litespeed' -ACME_VERSION='3.1.2' -ACME_SRC="https://raw.githubusercontent.com/acmesh-official/acme.sh/${ACME_VERSION}/acme.sh" EPACE=' ' RENEW='' RENEW_ALL='' FORCE='' REVOKE='' REMOVE='' +LSDIR='/usr/local/lsws' +HTTPD_CONF="${LSDIR}/conf/httpd_config.conf" +ACME_HOME="${LSDIR}/acme" +ACME_CONF="${LSDIR}/conf/cert/acme" +ERROR_LOG="${LSDIR}/logs/error.log" +TEMPLATE_NAME='dockerAcme' +TEMPLATE_FILE='conf/templates/docker-acme.conf' +WAIT_SECS=180 echow(){ FLAG=${1} @@ -24,29 +28,31 @@ echow(){ help_message(){ case ${1} in "1") - echo 'You will need to install acme script at the first time.' + echo 'You will need to install ACME the first time.' echo 'Please run acme.sh --install --email example@example.com' ;; "2") echo -e "\033[1mOPTIONS\033[0m" echow '-D, --domain [DOMAIN_NAME]' echo "${EPACE}${EPACE}Example: acme.sh --domain example.com" - echo "${EPACE}${EPACE}will auto detect and apply for both example.com and www.example.com domains." + echo "${EPACE}${EPACE}OpenLiteSpeed will apply for example.com, and include www.example.com when it is reachable." echow '-H, --help' echo "${EPACE}${EPACE}Display help and exit." echo -e "\033[1m Only for the First time\033[0m" echow '--install --email [EMAIL_ADDR]' - echo "${EPACE}${EPACE}Will install ACME with the Email provided" + echo "${EPACE}${EPACE}Will install OpenLiteSpeed ACME support with the Email provided" echow '-r, --renew' - echo "${EPACE}${EPACE}Renew a specific domain with -D or --domain parameter if posibile. To force renew, use -f parameter." + echo "${EPACE}${EPACE}Renew a specific domain with -D or --domain parameter if it is due. To force renew, use -f parameter." echow '-R, --renew-all' - echo "${EPACE}${EPACE}Renew all domains if possible. To force renew, use -f parameter." + echo "${EPACE}${EPACE}Renew all domains if they are due. To force renew, use -f parameter." echow '-f, -F, --force' echo "${EPACE}${EPACE}Force renew for a specific domain or all domains." echow '-v, --revoke' - echo "${EPACE}${EPACE}Revoke a domain." + echo "${EPACE}${EPACE}Revoke the certificate of a domain and stop using ACME for it." echow '-V, --remove' - echo "${EPACE}${EPACE}Remove a domain." + echo "${EPACE}${EPACE}Stop using ACME for a domain. OpenLiteSpeed deletes its certificate." + echow '-U, --uninstall' + echo "${EPACE}${EPACE}Stop using ACME for all domains and uninstall OpenLiteSpeed ACME support." exit 0 ;; "3") @@ -104,12 +110,6 @@ email_filter(){ fi } -cert_hook(){ - echo '[Start] Adding ACME hook' - docker compose exec ${CONT_NAME} su -s /bin/bash -c "certhookctl.sh" - echo '[End] Adding ACME hook' -} - www_domain(){ CHECK_WWW=$(echo ${1} | cut -c1-4) if [[ ${CHECK_WWW} == www. ]] ; then @@ -124,13 +124,11 @@ domain_verify(){ curl -Is http://${DOMAIN}/ | grep -i LiteSpeed > /dev/null 2>&1 if [ ${?} = 0 ]; then echo -e "[O] The domain name \033[32m${DOMAIN}\033[0m is accessible." - TYPE=1 curl -Is http://${WWW_DOMAIN}/ | grep -i LiteSpeed > /dev/null 2>&1 if [ ${?} = 0 ]; then echo -e "[O] The domain name \033[32m${WWW_DOMAIN}\033[0m is accessible." - TYPE=2 else - echo -e "[!] The domain name ${WWW_DOMAIN} is inaccessible." + echo -e "[!] The domain name ${WWW_DOMAIN} is inaccessible, OpenLiteSpeed will leave it out if its own check fails too." fi else echo -e "[X] The domain name \e[31m${DOMAIN}\e[39m is inaccessible, please verify." @@ -138,120 +136,452 @@ domain_verify(){ fi } +container_id(){ + CONT_ID=$(docker compose ps -q ${CONT_NAME} 2>/dev/null) + if [ -z "${CONT_ID}" ]; then + echo "[X] The ${CONT_NAME} container is not running, please run docker compose up -d first." + exit 1 + fi +} + +mount_source(){ + docker inspect -f "{{range .Mounts}}{{if eq .Destination \"${1}\"}}{{.Source}}{{end}}{{end}}" ${CONT_ID} +} + +# Prints the stat(1) format ${1} of the container file ${2}, or ${3} if it is missing. +file_stat(){ + docker compose exec -T ${CONT_NAME} sh -c 'stat -c "${1}" "${2}" 2>/dev/null || echo "${3}"' sh "${1}" "${2}" "${3}" +} + +lsws_restart(){ + docker compose exec -T ${CONT_NAME} su -c "${LSDIR}/bin/lswsctrl restart >/dev/null" + if [ ${?} = 0 ]; then + echo '[O] OpenLiteSpeed restarted.' + else + echo '[X] OpenLiteSpeed restart failed.' + fi +} + +acme_installed(){ + docker compose exec -T ${CONT_NAME} test -f ${ACME_HOME}/acme.sh -a -f ${ACME_CONF}/server.conf +} + +require_acme(){ + if ! acme_installed; then + help_message 1 + exit 1 + fi +} + install_acme(){ echo '[Start] Install ACME' - if [ "${1}" = 'true' ]; then - docker compose exec litespeed su -c " - cd && - wget ${ACME_SRC} && - chmod 755 acme.sh && - ./acme.sh --install --cert-home ~/.acme.sh/certs && - /root/.acme.sh/acme.sh --set-default-ca --server letsencrypt && - rm ~/acme.sh - " - elif [ "${2}" != '' ]; then - email_filter "${2}" - docker compose exec \ - -e ACME_SRC="${ACME_SRC}" \ - -e ACME_EMAIL="${2}" \ - litespeed su -c ' - cd && - wget "$ACME_SRC" && - chmod 755 acme.sh && - ./acme.sh --install --cert-home ~/.acme.sh/certs --accountemail "$ACME_EMAIL" && - /root/.acme.sh/acme.sh --set-default-ca --server letsencrypt && - rm ~/acme.sh - ' - else + if [ -z "${1}" ]; then help_message 1 exit 1 fi + email_filter "${1}" + if ! docker compose exec -T ${CONT_NAME} test -f ${LSDIR}/admin/misc/install_acme.sh; then + echo '[X] This OpenLiteSpeed image has no ACME support, please update OLS_VERSION in .env.' + exit 1 + fi + CONF_SRC=$(mount_source ${LSDIR}/conf) + ACME_SRC=$(mount_source ${ACME_HOME}) + if [ -z "${CONF_SRC}" ] || [ -z "${ACME_SRC}" ]; then + echo "[X] ${ACME_HOME} is not mounted in the ${CONT_NAME} container." + echo "[!] Add ./lsws/acme:${ACME_HOME} to its volumes in docker-compose.yml and run docker compose up -d" + exit 1 + fi + IMAGE_ID=$(docker inspect -f '{{.Image}}' ${CONT_ID}) + # install_acme.sh does nothing when ${ACME_HOME} exists, and in the litespeed + # container it is always a mount point. Run it in a throwaway container of + # the same image that shares the conf volume, then copy the result into + # the acme volume. + docker run --rm -i -e ACME_EMAIL="${1}" \ + -v "${CONF_SRC}:${LSDIR}/conf" -v "${ACME_SRC}:/acme-volume" \ + --entrypoint /bin/bash ${IMAGE_ID} -s <<'EOF' +set -e +if ! command -v git >/dev/null 2>&1; then + apt-get update -qq + DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends git >/dev/null +fi +mkdir -p /usr/local/lsws/conf/cert/acme +bash /usr/local/lsws/admin/misc/install_acme.sh -e "${ACME_EMAIL}" +test -f /usr/local/lsws/acme/acme.sh +test -f /usr/local/lsws/conf/cert/acme/server.conf +grep -q acmeThumbPrint /usr/local/lsws/conf/httpd_config.conf +cp -a /usr/local/lsws/acme/. /acme-volume/ +chown lsadm:lsadm /acme-volume +EOF + if [ ${?} != 0 ]; then + echo '[X] ACME installation failed, please check the messages above.' + exit 1 + fi + echo '[O] ACME installed.' + lsws_restart echo '[End] Install ACME' } uninstall_acme(){ echo '[Start] Uninstall ACME' - docker compose exec ${CONT_NAME} su -c "~/.acme.sh/acme.sh --uninstall" + for MEMBER in $(template_members ${TEMPLATE_NAME}); do + if move_member "${MEMBER}" ${TEMPLATE_NAME} docker; then + echo -e "[O] Domain '\033[32m${MEMBER}\033[0m' moved back to 'docker' template" + else + echo "[X] Failed to move ${MEMBER} back to docker template" + exit 1 + fi + done + docker compose exec -T ${CONT_NAME} bash -s -- "${HTTPD_CONF}" "${TEMPLATE_NAME}" "${LSDIR}/${TEMPLATE_FILE}" <<'EOF' +if grep -q "^vhTemplate ${2} {" "${1}"; then + cp "${1}" "${1}.backup.$(date +%Y%m%d_%H%M%S)" + awk -v tpl="${2}" ' + $1 == "vhTemplate" && $2 == tpl && $3 == "{" {skip = 1} + !skip {print} + skip && /^}/ {skip = 0} + ' "${1}" > "${1}.tmp" && cat "${1}.tmp" > "${1}" + rm -f "${1}.tmp" + echo "[O] Template ${2} removed." +fi +rm -f "${3}" +EOF + if acme_installed; then + CONF_SRC=$(mount_source ${LSDIR}/conf) + ACME_SRC=$(mount_source ${ACME_HOME}) + IMAGE_ID=$(docker inspect -f '{{.Image}}' ${CONT_ID}) + # uninstall_acme.sh runs acme.sh --uninstall, which also removes root's + # acme.sh cron job and ~/.acme.sh/acme.sh, so keep it away from the + # litespeed container's /root/.acme.sh. + docker run --rm -i -v "${CONF_SRC}:${LSDIR}/conf" -v "${ACME_SRC}:${ACME_HOME}" \ + --entrypoint /bin/bash ${IMAGE_ID} -s <<'EOF' 2>&1 | grep -v 'Device or resource busy' +bash /usr/local/lsws/admin/misc/uninstall_acme.sh +EOF + else + echo '[i] ACME is not installed.' + fi + lsws_restart echo '[End] Uninstall ACME' - exit 0 -} +} check_acme(){ echo '[Start] Checking ACME' - docker compose exec ${CONT_NAME} su -c "test -f /root/.acme.sh/acme.sh" - if [ ${?} != 0 ]; then - install_acme "${NO_EMAIL}" "${EMAIL}" - cert_hook + if ! acme_installed; then + install_acme "${EMAIL}" + if [ -z "${DOMAIN}" ]; then + help_message 3 + fi + elif [ "${INSTALL}" = 'true' ] && [ -z "${DOMAIN}" ]; then + echo '[i] ACME is already installed.' help_message 3 fi echo '[End] Checking ACME' } -lsws_restart(){ - docker compose exec ${CONT_NAME} su -c '/usr/local/lsws/bin/lswsctrl restart >/dev/null' +check_server_acme(){ + if docker compose exec -T ${CONT_NAME} grep -Eq '^[[:space:]]*acme[[:space:]]+0[[:space:]]*$' ${HTTPD_CONF}; then + echo "[X] ACME is disabled for the whole server (acme 0 in ${HTTPD_CONF})." + exit 1 + fi } -doc_root_verify(){ - if [ "${DOC_ROOT}" = '' ]; then - DOC_PATH="/var/www/vhosts/${1}/html" - ALT_DOC_PATH="/var/www/vhosts/www.${1}/html" - else - DOC_PATH="${DOC_ROOT}" +# Sets MEMBER_TPL to the template holding the member named ${1} and CERT_DOMAIN +# to its first vhDomain entry, which OpenLiteSpeed names the certificate after. +find_member(){ + read -r MEMBER_TPL CERT_DOMAIN <<< "$(docker compose exec -T ${CONT_NAME} awk -v name="${1}" ' + $1 == "vhTemplate" && $3 == "{" {tpl = $2; next} + /^}/ {tpl = ""; next} + tpl != "" && $1 == "member" && $2 == name && $3 == "{" {found = tpl; next} + found != "" && $1 == "vhDomain" {sub(/,.*/, "", $2); print found, $2; exit} + found != "" && $1 == "}" {print found, name; exit} + ' ${HTTPD_CONF})" + if [ -n "${CERT_DOMAIN}" ] && [ "${CERT_DOMAIN}" != "${1}" ]; then + validate_domain "${CERT_DOMAIN}" + echo "[i] The certificate of ${1} is named after its first vhDomain entry: ${CERT_DOMAIN}" fi + CERT_FILE="${ACME_CONF}/certs/${CERT_DOMAIN}_ecc/fullchain.cer" +} - docker compose exec ${CONT_NAME} su -c "[ -e ${DOC_PATH} ]" - if [ ${?} -eq 0 ]; then - echo -e "[O] The document root folder \033[32m${DOC_PATH}\033[0m does exist." - return 0 +template_members(){ + docker compose exec -T ${CONT_NAME} awk -v tpl="${1}" ' + $1 == "vhTemplate" && $3 == "{" {cur = $2; next} + /^}/ {cur = ""; next} + cur == tpl && $1 == "member" && $3 == "{" {print $2} + ' ${HTTPD_CONF} +} + +# Moves the member block ${1} verbatim from template ${2} to template ${3}. +move_member(){ + docker compose exec -T ${CONT_NAME} bash -s -- "${1}" "${2}" "${3}" "${HTTPD_CONF}" <<'EOF' +cp "${4}" "${4}.backup.$(date +%Y%m%d_%H%M%S)" +awk -v name="${1}" -v from="${2}" -v to="${3}" ' + {line[NR] = $0} + $1 == "vhTemplate" && $3 == "{" {tpl = $2; next} + /^}/ {if (tpl == to) last = NR; tpl = ""; next} + tpl == from && $1 == "member" && $2 == name && $3 == "{" {first = NR; inside = 1; next} + inside && $1 == "}" {stop = NR; inside = 0} + END { + if (!first || !stop || !last) + exit 1 + for (i = 1; i <= NR; i++) { + if (i == last) + for (j = first; j <= stop; j++) + print line[j] + if (i < first || i > stop) + print line[i] + } + } +' "${4}" > "${4}.tmp" && cat "${4}.tmp" > "${4}" +RC=${?} +rm -f "${4}.tmp" +exit ${RC} +EOF +} + +create_acme_template(){ + echo '[Start] Creating docker-acme.conf template' + docker compose exec -T ${CONT_NAME} bash -s -- "${LSDIR}/conf/templates/docker.conf" "${LSDIR}/${TEMPLATE_FILE}" <<'EOF' +if [ -f "${2}" ]; then + echo "[i] Template file already exists: ${2}" + exit 0 +fi +# Copy docker.conf without its vhssl block and closing brace, then let +# OpenLiteSpeed ACME provide the certificate. +awk ' + /^ vhssl {/ {skip = 1} + skip {if (/^ }/) skip = 0; next} + {line[++n] = $0} + END { + while (n && line[n] !~ /^}/) + n-- + if (!n) + exit 1 + for (i = 1; i < n; i++) + print line[i] + } +' "${1}" > "${2}.tmp" +cat >> "${2}.tmp" <<'VHSSL_EOF' + vhssl { + acme { + enabled 2 + } + } +} +VHSSL_EOF +if [ ! -s "${2}.tmp" ] || grep -Eq '^[[:space:]]*(keyFile|certFile)[[:space:]]' "${2}.tmp"; then + echo "[X] Unable to create ${2} from ${1}" + rm -f "${2}.tmp" + exit 1 +fi +mv "${2}.tmp" "${2}" +chown lsadm:lsadm "${2}" +chmod 644 "${2}" +echo "[O] Template ${2} created." +EOF + if [ ${?} != 0 ]; then + exit 1 fi + echo '[End] Creating docker-acme.conf template' +} - if [ "${DOC_ROOT}" = '' ]; then - docker compose exec ${CONT_NAME} su -c "[ -e ${ALT_DOC_PATH} ]" - if [ ${?} -eq 0 ]; then - DOC_PATH="${ALT_DOC_PATH}" - echo -e "[!] The default document root was not found. Using fallback \033[33m${DOC_PATH}\033[0m." - echo -e "[O] The document root folder \033[32m${DOC_PATH}\033[0m does exist." - return 0 +register_acme_template(){ + echo "[Start] Registering vhTemplate: ${TEMPLATE_NAME}" + docker compose exec -T ${CONT_NAME} bash -s -- "${HTTPD_CONF}" "${TEMPLATE_NAME}" "${TEMPLATE_FILE}" <<'EOF' +if grep -q "^vhTemplate ${2} {" "${1}"; then + echo "[i] Template ${2} already exists, skipped." + exit 0 +fi +cp "${1}" "${1}.backup.$(date +%Y%m%d_%H%M%S)" +cat >> "${1}" </dev/null || echo 0) -lt ${SIZE} ]; then + SIZE=0 + fi + NEW=$(tail -c +$((SIZE + 1)) "${4}" 2>/dev/null | grep '\[ACME\]') + if echo "${NEW}" | grep -q 'certs succeeded'; then + if [ "$(stat -c %Y "${2}" 2>/dev/null || echo none)" != "${3}" ]; then + exit 0 fi + RC=1 + break fi + if echo "${NEW}" | grep -Eq "Could not validate domain: ${DOMAIN_RE}[ .]"; then + RC=1 + break + fi +done +echo "${NEW}" | grep -E "${DOMAIN_RE}|certs succeeded" | tail -n 20 +exit ${RC} +EOF +} - echo -e "[X] The document root folder \e[31m${DOC_PATH}\e[39m does not exist!" - if [ "${DOC_ROOT}" = '' ]; then - echo -e "[X] The fallback document root \e[31m${ALT_DOC_PATH}\e[39m does not exist!" +# Waits for OpenLiteSpeed to serve the certificate in CERT_FILE for ${1}. +cert_served(){ + docker compose exec -T ${CONT_NAME} bash -s -- "${1}" "${CERT_FILE}" <<'EOF' +WANT=$(openssl x509 -noout -fingerprint -sha256 -in "${2}" 2>/dev/null) +for i in $(seq 1 12); do + GOT=$(echo | timeout 10 openssl s_client -connect 127.0.0.1:443 -servername "${1}" 2>/dev/null | openssl x509 -noout -fingerprint -sha256 2>/dev/null) + if [ -n "${WANT}" ] && [ "${WANT}" = "${GOT}" ]; then + exit 0 fi - exit 1 + sleep 5 +done +exit 1 +EOF } -install_cert(){ - echo '[Start] Apply Lets Encrypt Certificate' - if [ ${TYPE} = 1 ]; then - docker compose exec ${CONT_NAME} su -c "/root/.acme.sh/acme.sh --issue -d ${1} -w ${DOC_PATH}" - elif [ ${TYPE} = 2 ]; then - docker compose exec ${CONT_NAME} su -c "/root/.acme.sh/acme.sh --issue -d ${1} -d www.${1} -w ${DOC_PATH}" +check_served(){ + if cert_served "${1}"; then + echo -e "[O] OpenLiteSpeed is serving the certificate for \033[32m${1}\033[0m." + return 0 + fi + echo '[!] OpenLiteSpeed is not serving the new certificate yet, restarting it again.' + lsws_restart + if cert_served "${1}"; then + echo -e "[O] OpenLiteSpeed is serving the certificate for \033[32m${1}\033[0m." else - echo 'unknown Type!' - exit 2 + echo "[X] OpenLiteSpeed is still not serving the new certificate for ${1}, please check ${ERROR_LOG}." + fi +} + +acme_cmd(){ + SERVER=$(docker compose exec -T ${CONT_NAME} head -n 1 ${ACME_CONF}/server.conf) + # The same environment OpenLiteSpeed gives acme.sh, as the same user. + docker compose exec -T -u lsadm ${CONT_NAME} env -i PATH=/usr/sbin:/usr/bin:/sbin:/bin \ + LE_CONFIG_HOME=${ACME_CONF}/data LE_WORKING_DIR=${ACME_HOME} ACME_DIRECTORY="${SERVER}" \ + ${ACME_HOME}/acme.sh "${@}" --server "${SERVER}" +} + +require_acme_member(){ + find_member "${1}" + if [ "${MEMBER_TPL}" != "${TEMPLATE_NAME}" ]; then + echo -e "[X] The domain \e[31m${1}\e[39m does not use ACME, please run acme.sh --domain ${1} first." + exit 1 + fi +} + +apply_cert(){ + echo '[Start] Apply Lets Encrypt Certificate' + if [ "${1}" = 'localhost' ]; then + echo "[X] Let's Encrypt does not issue certificates for localhost." + exit 1 fi + find_member "${1}" + case "${MEMBER_TPL}" in + "${TEMPLATE_NAME}") + if docker compose exec -T ${CONT_NAME} test -f "${CERT_FILE}"; then + echo -e "[O] The domain \033[32m${1}\033[0m already uses ACME:" + show_cert + echo "[!] To renew it, run acme.sh --renew --domain ${1} [--force]" + echo '[End] Apply Lets Encrypt Certificate' + return 0 + fi + echo "[!] The domain ${1} is in the ${TEMPLATE_NAME} template but has no certificate yet, retrying." + ;; + docker) + create_acme_template + register_acme_template + ;; + '') + echo -e "[X] No virtual host member named \e[31m${1}\e[39m was found in ${HTTPD_CONF}." + echo "[!] Please add the domain first: bash bin/domain.sh --add ${1}" + exit 1 + ;; + *) + echo -e "[X] The domain \e[31m${1}\e[39m is in the ${MEMBER_TPL} template, which sets its own certificate." + if [ "${MEMBER_TPL}" = 'dockerLocal' ]; then + echo "[!] Please run bash bin/mkcert.sh --remove --domain ${1} first." + fi + exit 1 + ;; + esac + + CERT_MTIME=$(file_stat %Y "${CERT_FILE}" none) + LOG_SIZE=$(file_stat %s "${ERROR_LOG}" 0) + if [ "${MEMBER_TPL}" = 'docker' ]; then + if move_member "${1}" docker ${TEMPLATE_NAME}; then + echo -e "[O] Domain '\033[32m${1}\033[0m' moved to '${TEMPLATE_NAME}' template" + else + echo "[X] Failed to move domain to ${TEMPLATE_NAME} template" + exit 1 + fi + fi + echo "[!] Restarting OpenLiteSpeed, it will request the certificate. This can take a few minutes." + lsws_restart + wait_cert + case ${?} in + 0) + echo -e "[O] The certificate for \033[32m${CERT_DOMAIN}\033[0m was issued:" + show_cert + check_served "${CERT_DOMAIN}" + ;; + 1) + echo -e "[X] The certificate for \e[31m${CERT_DOMAIN}\e[39m was not issued, see the log lines above." + echo "[!] Please check the DNS and that port 80 reaches this server, then run this command again." + echo "[!] To go back to the docker template, run acme.sh --remove --domain ${1}" + ;; + *) + echo "[!] OpenLiteSpeed has not finished after ${WAIT_SECS} seconds, please check ${ERROR_LOG}." + ;; + esac echo '[End] Apply Lets Encrypt Certificate' } renew_acme(){ echo '[Start] Renew ACME' + require_acme_member "${1}" if [ "${FORCE}" = 'true' ]; then - docker compose exec ${CONT_NAME} su -c "~/.acme.sh/acme.sh --renew --domain ${1} --force" + acme_cmd --renew -d "${CERT_DOMAIN}" --stateless --force else - docker compose exec ${CONT_NAME} su -c "~/.acme.sh/acme.sh --renew --domain ${1}" + acme_cmd --renew -d "${CERT_DOMAIN}" --stateless fi + case ${?} in + 0) + echo '[O] The certificate was renewed.' + lsws_restart + show_cert + check_served "${CERT_DOMAIN}" + ;; + 2) + echo '[i] The certificate is not due for renewal yet. To force renew, use -f parameter.' + ;; + *) + echo '[X] The renewal failed, please check the messages above.' + ;; + esac echo '[End] Renew ACME' - lsws_restart } renew_all_acme(){ echo '[Start] Renew all ACME' if [ "${FORCE}" = 'true' ]; then - docker compose exec ${CONT_NAME} su -c "~/.acme.sh/acme.sh --renew-all --force" + acme_cmd --renew-all --force else - docker compose exec ${CONT_NAME} su -c "~/.acme.sh/acme.sh --renew-all" + acme_cmd --renew-all fi echo '[End] Renew all ACME' lsws_restart @@ -259,43 +589,72 @@ renew_all_acme(){ revoke(){ echo '[Start] Revoke a domain' - docker compose exec ${CONT_NAME} su -c "~/.acme.sh/acme.sh --revoke --domain ${1}" - echo '[End] Revoke a domain' + require_acme_member "${1}" + if ! acme_cmd --revoke -d "${CERT_DOMAIN}"; then + echo '[X] The revoke failed, please check the messages above.' + exit 1 + fi + if move_member "${1}" ${TEMPLATE_NAME} docker; then + echo -e "[O] Domain '\033[32m${1}\033[0m' moved back to 'docker' template" + else + echo "[X] Failed to move domain back to docker template" + exit 1 + fi + # OpenLiteSpeed deletes certificates that are no longer used when it starts. lsws_restart + echo '[End] Revoke a domain' } remove(){ echo '[Start] Remove a domain' - docker compose exec ${CONT_NAME} su -c "~/.acme.sh/acme.sh --remove --domain ${1}" - echo '[End] Remove a domain' + require_acme_member "${1}" + if move_member "${1}" ${TEMPLATE_NAME} docker; then + echo -e "[O] Domain '\033[32m${1}\033[0m' moved back to 'docker' template" + else + echo "[X] Failed to move domain back to docker template" + exit 1 + fi + # OpenLiteSpeed revokes and deletes certificates that are no longer used + # when it starts. lsws_restart + echo '[End] Remove a domain' } main(){ - if [ "${RENEW_ALL}" = 'true' ]; then + container_id + if [ "${UNINSTALL}" = 'true' ]; then + uninstall_acme + exit 0 + elif [ "${RENEW_ALL}" = 'true' ]; then + require_acme renew_all_acme exit 0 elif [ "${RENEW}" = 'true' ]; then - validate_domain "${DOMAIN}" + require_acme + domain_filter "${DOMAIN}" + www_domain "${DOMAIN}" renew_acme "${DOMAIN}" exit 0 elif [ "${REVOKE}" = 'true' ]; then - validate_domain "${DOMAIN}" + require_acme + domain_filter "${DOMAIN}" + www_domain "${DOMAIN}" revoke "${DOMAIN}" exit 0 elif [ "${REMOVE}" = 'true' ]; then - validate_domain "${DOMAIN}" + require_acme + domain_filter "${DOMAIN}" + www_domain "${DOMAIN}" remove "${DOMAIN}" exit 0 fi check_acme - domain_filter ${DOMAIN} - www_domain ${DOMAIN} + domain_filter "${DOMAIN}" + www_domain "${DOMAIN}" + check_server_acme domain_verify - doc_root_verify ${DOMAIN} - install_cert ${DOMAIN} - lsws_restart + apply_cert "${DOMAIN}" } check_input ${1} @@ -313,7 +672,6 @@ while [ ! -z "${1}" ]; do ;; -[uU] | --uninstall ) UNINSTALL=true - uninstall_acme ;; -[fF] | --force ) FORCE=true @@ -341,4 +699,4 @@ while [ ! -z "${1}" ]; do shift done -main \ No newline at end of file +main diff --git a/bin/container/certhookctl.sh b/bin/container/certhookctl.sh deleted file mode 100755 index 18be0965..00000000 --- a/bin/container/certhookctl.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash -BOTCRON='/var/spool/cron/crontabs/root' - -cert_hook(){ - grep 'acme' ${BOTCRON} >/dev/null - if [ ${?} = 0 ]; then - grep 'lswsctrl' ${BOTCRON} >/dev/null - if [ ${?} = 0 ]; then - echo 'Hook already exist, skip!' - else - sed -i 's/--cron/--cron --renew-hook "\/usr\/local\/lsws\/bin\/lswsctrl restart"/g' ${BOTCRON} - fi - else - echo "[X] ${BOTCRON} does not exist, please check it later!" - fi -} - -cert_hook \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index cd4f7827..fe70c10a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,6 +22,7 @@ services: volumes: - ./lsws/conf:/usr/local/lsws/conf - ./lsws/admin-conf:/usr/local/lsws/admin/conf + - ./lsws/acme:/usr/local/lsws/acme - ./bin/container:/usr/local/bin - ./sites:/var/www/vhosts/ - ./acme:/root/.acme.sh/