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.
timedatectl statussudo timedatectl set-ntp trueInstall Certbot from the official snap or your distribution package. On Debian and Ubuntu, the snap is the recommended channel because it ships current plugins.
sudo snap install --classic certbotsudo ln -s /snap/bin/certbot /usr/bin/certbotVerify the version and the available plugins.
certbot --versioncertbot pluginsChoosing a DNS Provider and Plugin
Pick the plugin that matches your DNS host. Each plugin is a separate package, installed alongside Certbot.
For Cloudflare:
sudo snap set certbot trust-plugin-with-root=oksudo snap install certbot-dns-cloudflareFor Amazon Route 53:
sudo snap install certbot-dns-route53For DigitalOcean:
sudo snap install certbot-dns-digitaloceanThe 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.
sudo mkdir -p /etc/letsencryptsudo chmod 755 /etc/letsencryptsudo install -m 600 /dev/null /etc/letsencrypt/cloudflare.inisudo nano /etc/letsencrypt/cloudflare.iniFor 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 = 0123456789abcdef0123456789abcdef01234567Lock the file down and verify ownership.
sudo chown root:root /etc/letsencrypt/cloudflare.inisudo chmod 600 /etc/letsencrypt/cloudflare.inils -l /etc/letsencrypt/cloudflare.iniThe 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.
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-emailThe --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.pemfullchain.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.
sudo certbot certificatesopenssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -subject -issuer -datesThe 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.
systemctl status certbot.timersystemctl list-timers | grep certbotCertbot 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.shsudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-web.sh >/dev/null <<'EOF'#!/bin/shset -eif systemctl is-active --quiet nginx; then nginx -t && systemctl reload nginxfiif systemctl is-active --quiet apache2; then apache2ctl configtest && systemctl reload apache2fiEOFsudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/reload-web.shTest the renewal path without actually reissuing.
sudo certbot renew --dry-runA 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.
sudo nginx -tsudo systemctl reload nginxNginx 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.
sudo a2enmod sslsudo apache2ctl configtestsudo systemctl reload apache2On 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.comOr:
Detail: No TXT record found at _acme-challenge.example.comCauses: 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.
dig TXT _acme-challenge.example.com @ns1.cloudflare.comIf 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 foundThe 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 domainsYou 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.
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 errorOr:
The server experienced an internal errorCheck 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.
date -ucurl -sI https://acme-v02.api.letsencrypt.org/directoryPermission 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.
sudo /etc/letsencrypt/renewal-hooks/deploy/reload-web.shAlso 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:
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.
| Tool | Language | DNS providers | Wildcard support | Auto-renewal | Reload handling |
|---|---|---|---|---|---|
| Certbot | Python | Plugins per provider | Yes, via DNS-01 | Timer or cron | Deploy hooks |
| acme.sh | Shell | Built-in, many | Yes, via DNS-01 | Own cron entry | Reload command |
| lego | Go | Built-in, many | Yes, via DNS-01 | External scheduler | External script |
| Caddy | Go | Plugin per provider | Yes, via DNS-01 | Built in | Built 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.iniwith mode 600 and root ownership. certbot certificateslists the wildcard and apex names with a 90-day expiry.certbot renew --dry-runsucceeds and invokes the deploy hook.- The web server config references
fullchain.pemandprivkey.pemunder/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.
echo | openssl s_client -connect example.com:443 -servername app.example.com 2>/dev/null | openssl x509 -noout -subject -issuer -datesThe 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.