Skip to content

TLS for Apache with Let's Encrypt and certbot

Provision and auto-renew Let's Encrypt certificates for Apache vhosts — the apache plugin, the cron/timer renewal, and the rollback when the hook breaks.

Let’s Encrypt and certbot turned TLS provisioning from a quarterly procurement task into a daemon. For Apache, the certbot Apache plugin is the most ergonomic path — it discovers your vhosts, splits them into HTTP and HTTPS pairs, drops the right SSLCertificateFile lines, and registers a systemd timer for renewal. This article is the full workflow plus the parts that quietly break: hook ordering, vhost edits that confuse the plugin, and rate-limit awareness when you re-issue too often.

How to verify

Before installing, confirm the box meets Let’s Encrypt’s basic requirements: port 80 reachable from the public internet (for the HTTP-01 challenge), DNS resolving to this box, and Apache running. Then check what is already in place:

sudo certbot certificates 2>/dev/null || echo "certbot not installed"
ls /etc/letsencrypt/live/ 2>/dev/null
systemctl list-timers | grep certbot
sudo openssl s_client -connect example.com:443 -servername example.com </dev/null | grep -A1 'subject='

certbot certificates lists every certificate certbot manages, including issue date, expiry, and the domains covered. A timer named snap.certbot.renew.timer (snap install) or certbot.timer (apt install) is what triggers renewal twice a day.

What’s happening

The Apache plugin (certbot --apache) does three things in one run:

  1. Solves the ACME challenge — by default HTTP-01, which means certbot temporarily binds to port 80 (or asks Apache to serve a token file) and Let’s Encrypt fetches http://yourdomain/.well-known/acme-challenge/<token>.
  2. Writes the issued certificate, chain, and private key to /etc/letsencrypt/live/<primary-domain>/ (symlinks pointing into /etc/letsencrypt/archive/<primary-domain>/).
  3. Modifies the matching vhost: either adds SSLCertificateFile / SSLCertificateKeyFile directives to an existing <VirtualHost *:443> block, or creates a new <VirtualHost *:443> copy of the port-80 vhost.

Renewal runs via the systemd timer (modern installs) or a cron job (older installs). When the certificate is within 30 days of expiry, certbot renew re-runs the challenge and writes the new cert into the same path. Apache picks up the new cert on the next graceful reload, which the deploy hook handles.

The vhosts after a certbot run look like this — one port-80 vhost (often reduced to a redirect) and one port-443 vhost with the TLS directives:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public

    SSLEngine on
    SSLCertificateFile      /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

The procedure

  1. Install certbot and the Apache plugin. On Ubuntu 22.04+, the snap install is the upstream-recommended path:

    sudo snap install --classic certbot
    sudo ln -sf /snap/bin/certbot /usr/bin/certbot
    sudo snap set certbot trust-plugin-with-root=ok
    sudo snap install certbot-dns-cloudflare   # only if using DNS-01

    On hosts where snap is not appropriate, sudo apt install certbot python3-certbot-apache gives you a slightly older but working version.

  2. Confirm the vhost is set up cleanly. Certbot reads /etc/apache2/sites-enabled/ to discover sites. The vhost must have a working ServerName (and optional ServerAlias) and respond on port 80. Run apache2ctl -S to confirm certbot will see what you expect.

  3. Run certbot for the vhost. The non-interactive flags fit cleanly into a script or playbook:

    sudo certbot --apache \
      -d example.com -d www.example.com \
      --email ops@example.com \
      --agree-tos --no-eff-email \
      --redirect

    --redirect tells the plugin to write the port-80 → port-443 redirect for you. --non-interactive plus --agree-tos makes the run scriptable.

  4. Verify the certificate is live. Browser the site, then on the box:

    sudo certbot certificates
    sudo apachectl configtest
    curl -sI https://example.com/ | head -5

    The expiry on the certificate should be roughly 90 days out.

  5. Test renewal in dry-run mode. Do this once after the first issuance to catch hook problems before they fire during a real renewal:

    sudo certbot renew --dry-run

    A passing dry run means the timer-triggered renewal in 60 days will work.

  6. Wire up a deploy hook that reloads Apache. The Apache plugin’s --reinstall hook handles this on most installs, but make it explicit:

    sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh <<'EOF'
    #!/bin/bash
    /usr/sbin/apachectl configtest && /bin/systemctl reload apache2
    EOF
    sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh

    Configtest before reload prevents a bad cert (rare, but possible during a Let’s Encrypt incident) from taking the site down.

  7. Move to DNS-01 for wildcards. HTTP-01 cannot issue wildcards. If you need *.example.com, switch to DNS-01 with a provider plugin:

    sudo certbot certonly --dns-cloudflare \
      --dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
      -d example.com -d '*.example.com' \
      --preferred-challenges dns-01

    The credentials file should be chmod 600 and owned by root. See Let’s Encrypt DNS-01 for wildcards for the full provider matrix.

Common pitfalls

  • A vhost without ServerName set is invisible to certbot — the plugin will not offer it in the interactive list. Add ServerName and reload before running certbot.
  • Manual edits to /etc/letsencrypt/options-ssl-apache.conf survive certbot upgrades; manual edits to /etc/letsencrypt/live/*/ symlinks do not — those are regenerated on renewal.
  • Let’s Encrypt rate limits issuance to 50 certs per registered domain per week and 5 duplicate certs per week. During a debugging spree, you can lock yourself out; use the staging environment (--staging or --server https://acme-staging-v02.api.letsencrypt.org/directory) for iteration.
  • If Apache is behind a CDN or load balancer that does not pass port 80 through, HTTP-01 cannot succeed. Either configure the CDN to forward /.well-known/acme-challenge/, or move to DNS-01.
  • A renewal failure produces nothing visible unless you check — set up mailto: in certbot’s email or pipe certbot renew --quiet || alert into your monitoring.

Stack Harbor runs Let’s Encrypt at fleet scale for clients — every Apache host with vhosts under management has a tested renewal hook, monitored expiry dates, and a documented rollback path. If you have an Apache fleet where certificate renewal is somebody’s tribal knowledge, that is the kind of work that lives under Managed Operations.