Skip to content
masdzub.com
~/articles/let's encrypt — one from the notebook

Let's Encrypt Wildcard Certificates with DNS-01: A Practical Guide for Web Hosting

Issue and renew wildcard TLS certificates from Let's Encrypt using DNS-01 challenges. Certbot setup, automation, Nginx and Apache deployment, troubleshooting.

masdzub@website:~$ cat ~/articles/web-hosting-lets-encrypt-with-dns-01-and-wildcard-certs.aspx.md
# 1 file · 15 min read · topic: let's encrypt · status: published

Introduction

A wildcard certificate covers every subdomain under a domain, and Let’s Encrypt issues them only through DNS-01 challenges. This guide walks through requesting, automating, and deploying one for a web hosting stack, with commands that run on a current Debian or Ubuntu server and the failure modes that appear in production.

The DNS-01 challenge proves control of a domain by publishing a TXT record at _acme-challenge.<domain>. Because the proof lives in DNS rather than on a web server, it works for wildcard names, for servers behind a firewall, and for hosts that do not answer on port 80. The trade-off is that you hand an API token to your certificate client, so the token scope and storage matter as much as the certificate itself.

Why DNS-01 and Wildcards

HTTP-01 is the default challenge for most Let’s Encrypt clients. The client places a file under /.well-known/acme-challenge/ on the target host, and the ACME server fetches it over HTTP. That works for a single name or a fixed list of names, but it cannot issue *.example.com, because the wildcard itself never resolves to a single host that can answer the challenge. DNS-01 solves that by moving the proof into the DNS zone, where a single TXT record authorizes the whole subtree.

A wildcard saves operational work when you run many subdomains: app.example.com, api.example.com, staging.example.com, and customer-specific names such as acme.example.com. One certificate covers all of them, and renewal is one event instead of one per host. The cost is that every subdomain shares one private key and one expiry date, so a key compromise or a missed renewal affects everything at once. If you need per-host isolation or different expiry schedules, issue separate certificates per name instead.

DNS-01 also helps when the web server is not publicly reachable. A staging box behind a VPN, a build farm on a private subnet, or a mail server with no HTTP listener can still obtain a certificate, because the ACME server only talks to the DNS provider. The same property makes DNS-01 the only practical route for wildcard issuance.

How the Challenge Flows

The client, typically Certbot, generates an authorization key and asks the ACME server for an order covering *.example.com and example.com. The server responds with a challenge token. The client computes a key authorization from the token and its account key, then asks the DNS plugin to create a TXT record at _acme-challenge.example.com with that value. The ACME server queries DNS, confirms the record matches, and marks the authorization valid. The client then submits a certificate signing request and receives the certificate chain.

Two details cause most failures. First, the TXT record must be visible to the ACME server’s resolvers before the validation request, which means respecting DNS propagation time. Second, the record name is always _acme-challenge.<domain>, even for a wildcard request. The wildcard asterisk never appears in the DNS name.

Prerequisites

You need a domain whose DNS is hosted by a provider with an API that a Certbot DNS plugin supports. Cloudflare, Amazon Route 53, DigitalOcean, Google Cloud DNS, Hetzner, and OVH are common choices. You also need root or sudo access on the server that will hold the certificate, and outbound HTTPS access to acme-v02.api.letsencrypt.org.

Check the server clock before starting. ACME uses signed requests with short-lived nonces, and a clock skew of more than a few minutes produces Error creating new order :: JWS verification error or nonce reuse errors. Run timedatectl status and enable NTP if the time is off.

Terminal window
timedatectl status
sudo timedatectl set-ntp true

Install Certbot from the official snap or your distribution package. On Debian and Ubuntu, the snap is the recommended channel because it ships current plugins.

Terminal window
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

Verify the version and the available plugins.

Terminal window
certbot --version
certbot plugins

Choosing a DNS Provider and Plugin

Pick the plugin that matches your DNS host. Each plugin is a separate package, installed alongside Certbot.

For Cloudflare:

Terminal window
sudo snap set certbot trust-plugin-with-root=ok
sudo snap install certbot-dns-cloudflare

For Amazon Route 53:

Terminal window
sudo snap install certbot-dns-route53

For DigitalOcean:

Terminal window
sudo snap install certbot-dns-digitalocean

The plugin name in the Certbot command is the DNS provider name, for example --dns-cloudflare. Credentials are supplied through an INI file whose path you control.

Cloudflare Credentials File

Create a directory for credentials and restrict its permissions. Certbot refuses to read a credentials file that is world-readable.

Terminal window
sudo mkdir -p /etc/letsencrypt
sudo chmod 755 /etc/letsencrypt
sudo install -m 600 /dev/null /etc/letsencrypt/cloudflare.ini
sudo nano /etc/letsencrypt/cloudflare.ini

For Cloudflare, use an API token with Zone:DNS:Edit and Zone:Zone:Read scoped to the zone you need. Do not use the global API key. The file contents look like this:

dns_cloudflare_api_token = 0123456789abcdef0123456789abcdef01234567

Lock the file down and verify ownership.

Terminal window
sudo chown root:root /etc/letsencrypt/cloudflare.ini
sudo chmod 600 /etc/letsencrypt/cloudflare.ini
ls -l /etc/letsencrypt/cloudflare.ini

The expected output is a mode of -rw------- owned by root. If the mode is looser, Certbot exits with an error before it ever contacts the ACME server.

First Issuance

Request the certificate with both the apex and the wildcard name. Including example.com alongside *.example.com is common, because the wildcard does not match the apex itself.

Terminal window
sudo certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
--dns-cloudflare-propagation-seconds 60 \
-d '*.example.com' \
-d 'example.com' \
--email admin@example.com \
--agree-tos \
--no-eff-email

The --dns-cloudflare-propagation-seconds 60 flag tells Certbot to wait 60 seconds after creating the TXT record before asking the ACME server to validate. Increase it to 120 or 300 if your provider is slow to publish. Let’s Encrypt’s own resolvers usually see the record within seconds, but conservative values prevent intermittent failures during renewal.

On success, Certbot writes the certificate and key under /etc/letsencrypt/live/example.com/:

/etc/letsencrypt/live/example.com/fullchain.pem
/etc/letsencrypt/live/example.com/privkey.pem
/etc/letsencrypt/live/example.com/cert.pem
/etc/letsencrypt/live/example.com/chain.pem

fullchain.pem is cert.pem plus the intermediate. Web servers should serve fullchain.pem as the certificate file, not cert.pem, because clients need the intermediate to build a trust chain.

Inspect the result.

Terminal window
sudo certbot certificates
openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -subject -issuer -dates

The subject should show CN = example.com or a wildcard SAN depending on the Let’s Encrypt profile, and the issuer should be R10 or R11 for current Let’s Encrypt. The not-after date is roughly 90 days out.

Automating Renewal

Let’s Encrypt certificates expire after 90 days. Certbot installs a renewal timer automatically. On systems using systemd, the timer is certbot.timer; on others, a cron entry under /etc/cron.d/certbot runs certbot -q renew twice daily.

Check the timer status.

Terminal window
systemctl status certbot.timer
systemctl list-timers | grep certbot

Certbot renews when a certificate is within 30 days of expiry, so a twice-daily run is plenty. You do not need to schedule anything extra. What you do need is a deploy hook that reloads the web server after a successful renewal, because the server process keeps the old certificate in memory until it re-reads the files.

Create the hook script.

sudo install -m 755 /dev/null /etc/letsencrypt/renewal-hooks/deploy/reload-web.sh
sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-web.sh >/dev/null <<'EOF'
#!/bin/sh
set -e
if systemctl is-active --quiet nginx; then
nginx -t && systemctl reload nginx
fi
if systemctl is-active --quiet apache2; then
apache2ctl configtest && systemctl reload apache2
fi
EOF
sudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/reload-web.sh

Test the renewal path without actually reissuing.

Terminal window
sudo certbot renew --dry-run

A dry run exercises the same DNS-01 flow and the deploy hook. If it passes, production renewal will pass. If it fails, the error message points at the failing step.

Deploying to Nginx

Point the server block at the live files. Nginx reads them at startup and on reload, so the deploy hook above is enough.

server {
listen 443 ssl http2;
server_name example.com *.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
root /var/www/example.com;
index index.html;
}

Validate and reload.

Terminal window
sudo nginx -t
sudo systemctl reload nginx

Nginx runs as a non-root user but reads the certificate files as root during startup, so the default permissions under /etc/letsencrypt/live/ and /etc/letsencrypt/archive/ work without changes. If you run Nginx in a container or under a restricted user, ensure that user can read privkey.pem.

Deploying to Apache

Apache uses separate directives for the certificate, key, and chain. With Apache 2.4.8 and later, you can point SSLCertificateFile at fullchain.pem and omit the chain directive.

<VirtualHost *:443>
ServerName example.com
ServerAlias *.example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
DocumentRoot /var/www/example.com
</VirtualHost>

Enable the SSL module if it is not already active, then test and reload.

Terminal window
sudo a2enmod ssl
sudo apache2ctl configtest
sudo systemctl reload apache2

On older Apache versions below 2.4.8, use SSLCertificateFile for cert.pem, SSLCertificateKeyFile for privkey.pem, and SSLCertificateChainFile for chain.pem. Current distributions ship 2.4.8 or newer, but check with apache2 -v if you are on an older release.

Security Considerations

The DNS API token is the most sensitive artifact in this setup. Treat it like a password.

Scope the token to the single zone and the minimum permissions. A Cloudflare token needs Zone:DNS:Edit and Zone:Zone:Read for one zone. It does not need account-level permissions, and it should not be reused for other services.

Keep the credentials file outside version control and outside web roots. /etc/letsencrypt/ is the right place because it is not served by the web server and is not part of any application repository.

Rotate the token on a schedule and immediately if it leaks. Because the file mode is 600 and owned by root, only root processes can read it, but backups, configuration management tools, and CI pipelines sometimes copy it elsewhere without the mode preserved.

Consider a separate DNS account or subdomain for certificate automation. If the token is compromised, the attacker can create and delete DNS records in that zone. Restricting the token to a single zone limits the blast radius. Some teams delegate _acme-challenge via CNAME to a dedicated validation domain controlled by a separate account, which keeps the production zone token out of the certificate host entirely.

Let’s Encrypt rate limits matter for automation. The duplicate certificate limit is 5 per week for an identical set of names, and the new orders limit is 300 per 3 hours per account. A misconfigured renewal loop that retries every minute can exhaust the new orders limit and block legitimate issuance for hours. The deploy hook and the Certbot timer prevent this by running on a fixed schedule, but a manual script that calls certbot certonly in a loop will hit the limit.

Troubleshooting

No TXT Record Found

Error message:

Detail: DNS problem: NXDOMAIN looking up TXT for _acme-challenge.example.com

Or:

Detail: No TXT record found at _acme-challenge.example.com

Causes: the record has not propagated yet, the plugin wrote to the wrong zone, or the provider API returned success without actually publishing. Increase --dns-cloudflare-propagation-seconds to 120 or 300. Check the zone with a direct query against the provider’s authoritative servers rather than your local resolver.

Terminal window
dig TXT _acme-challenge.example.com @ns1.cloudflare.com

If the record never appears, verify the API token scope and that the domain in the Certbot command matches the zone name exactly, including the absence of a trailing dot.

Zone Determination Errors

Error message:

Error determining zone for example.com: ... zone could not be found

The token lacks Zone:Zone:Read, or the zone name in the token scope does not match the requested domain. Reissue the token with read access to the correct zone and re-run.

Rate Limit Exhausted

Error message:

Error creating new order :: too many certificates already issued for exact set of domains

You have hit the 5 per week duplicate limit. Wait for the window to reset, or add a name to the set so it is not an exact duplicate. For testing, use the staging server to avoid consuming production quota.

Terminal window
sudo certbot certonly --staging \
--dns-cloudflare \
--dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
-d '*.example.com' -d 'example.com'

Staging certificates are not trusted by browsers, but they exercise the full flow and count against separate, higher limits.

Clock Skew and Nonce Errors

Error message:

Error creating new order :: JWS verification error

Or:

The server experienced an internal error

Check the system time and enable NTP. Also check that the server can reach acme-v02.api.letsencrypt.org on port 443 without a proxy interfering.

Terminal window
date -u
curl -sI https://acme-v02.api.letsencrypt.org/directory

Permission Denied on Renewal

Error message:

PermissionError: [Errno 13] Permission denied: '/etc/letsencrypt/.certbot.lock'

The renewal ran as a non-root user. The timer and cron entries created by Certbot run as root by default. If you invoked certbot renew manually as a regular user, re-run with sudo. If a custom scheduler runs as another user, either run it as root or grant that user write access to /etc/letsencrypt/, which is not recommended.

Web Server Still Serving the Old Certificate

Symptom: browsers show the new expiry only after a manual reload, even though renewal succeeded.

The deploy hook did not run, or it ran but the reload failed. Check the hook is executable and that nginx -t or apache2ctl configtest passes. Run the hook manually to see the error.

Terminal window
sudo /etc/letsencrypt/renewal-hooks/deploy/reload-web.sh

Also confirm the server block points at /etc/letsencrypt/live/... and not at a copied file in another directory. Certbot updates the live symlink on renewal, but a copy goes stale.

Alternative Clients and Approaches

Certbot is not the only option. Each tool has a different shape, and the right choice depends on your hosting model.

acme.sh is a shell script with no Python dependency. It supports dozens of DNS providers and runs on minimal systems such as routers and embedded devices. A Cloudflare issuance looks like this:

Terminal window
export CF_Token="0123456789abcdef0123456789abcdef01234567"
acme.sh --issue --dns dns_cf -d '*.example.com' -d 'example.com'
acme.sh --install-cert -d example.com \
--fullchain-file /etc/ssl/certs/example.com.pem \
--key-file /etc/ssl/private/example.com.key \
--reloadcmd "systemctl reload nginx"

acme.sh installs its own cron entry for renewal and runs the reload command after each successful renewal. It stores state under ~/.acme.sh/ by default, which is convenient on single-user systems but awkward for centralized hosting.

lego is a Go binary with a single static executable. It suits CI pipelines and container images where installing Python or shell tooling is undesirable. It supports many DNS providers through environment variables.

Caddy handles HTTPS automatically and can obtain wildcard certificates when built with a DNS challenge plugin. The catch is that the official Caddy binary does not include DNS providers, so you must build a custom binary with xcaddy or use a community build. Once built, the Caddyfile needs only the domain and the provider credentials:

*.example.com, example.com {
tls {
dns cloudflare {env.CF_API_TOKEN}
}
root * /var/www/example.com
file_server
}

Caddy renews and reloads itself, which removes the deploy hook entirely. Traefik and other reverse proxies follow a similar model, with DNS challenge configuration in their static or dynamic config.

The comparison below summarizes the differences.

ToolLanguageDNS providersWildcard supportAuto-renewalReload handling
CertbotPythonPlugins per providerYes, via DNS-01Timer or cronDeploy hooks
acme.shShellBuilt-in, manyYes, via DNS-01Own cron entryReload command
legoGoBuilt-in, manyYes, via DNS-01External schedulerExternal script
CaddyGoPlugin per providerYes, via DNS-01Built inBuilt in

Choose Certbot when you want a well-documented, widely supported client with clear separation between issuance and deployment. Choose acme.sh for minimal systems. Choose Caddy when the reverse proxy itself can own the certificate lifecycle.

Operational Checklist

Before declaring the setup complete, verify each item.

  • The credentials file exists at /etc/letsencrypt/cloudflare.ini with mode 600 and root ownership.
  • certbot certificates lists the wildcard and apex names with a 90-day expiry.
  • certbot renew --dry-run succeeds and invokes the deploy hook.
  • The web server config references fullchain.pem and privkey.pem under /etc/letsencrypt/live/.
  • The server block answers on the wildcard name with a valid chain, verified by openssl s_client.
  • The API token is scoped to one zone with DNS edit and zone read only.
  • The deploy hook reloads the server without errors.

Run an end-to-end verification with OpenSSL.

Terminal window
echo | openssl s_client -connect example.com:443 -servername app.example.com 2>/dev/null | openssl x509 -noout -subject -issuer -dates

The subject should include the wildcard, and the issuer should be a Let’s Encrypt intermediate. If the chain is incomplete, browsers will fail to validate even though the leaf certificate is correct, so always serve fullchain.pem.

Closing Notes

DNS-01 wildcard issuance is a small amount of configuration with a large operational payoff, but it concentrates risk in two places: the DNS API token and the single renewal path. Keep the token scoped and rotated, test renewal with --dry-run after every change, and make the deploy hook fail loudly rather than silently. The certificate itself is the easy part; the automation around it is what keeps the site online 90 days later.

found this useful? share it →
Written by

Dzubayyan Ahmad

System Administrator, SRE & Technical Support Engineer specialising in web hosting infrastructure — Linux servers, automation, and monitoring. Writing these notes since 2015 so future-me can find the fix again.