Certbot’s usual HTTP-01 check cannot issue a wildcard certificate such as *.example.com: Let’s Encrypt only accepts the DNS-01 challenge for wildcards. This guide sets up Certbot DNS-01 for a wildcard certificate on Nginx. You end up with one certificate for example.com and every first-level subdomain, and it renews and reloads Nginx on its own.
Quick answer: Wildcard certificates need the DNS-01 challenge. Give Certbot API access to your DNS through a plugin or hook script, and request both example.com and *.example.com. Then point Nginx at the new files and add a reload hook for renewals.
- Install Certbot and your DNS provider’s plugin from snap on the server.
- Save a zone-scoped API token in a credentials file only root can read.
- Run
certbot certonlywith the DNS plugin and-d example.com -d "*.example.com". - Point
ssl_certificateandssl_certificate_keyat/etc/letsencrypt/live/example.com/and runsudo nginx -tbefore reloading. - Run
sudo certbot renew --dry-runand add a deploy hook that reloads Nginx.
In this guide
- Why Wildcards Need DNS-01 Instead of HTTP-01
- Before You Start
- Step 1: Install Certbot and the DNS Plugin
- Step 2: Store the DNS API Token Safely
- Step 3: Issue the Wildcard Certificate
- Alternative: Use Hook Scripts When There Is No DNS Plugin
- Step 4: Point Nginx at the Wildcard Certificate
- Step 5: Test Renewal and Reload Nginx Automatically
- If It Still Doesn’t Work: Common DNS-01 Errors
- Frequently Asked Questions
- Next Steps

Why Wildcards Need DNS-01 Instead of HTTP-01
With the HTTP-01 challenge, Certbot puts a temporary token in /.well-known/acme-challenge/ and Let’s Encrypt fetches it over port 80. That proves control of one hostname. It cannot prove control of every possible subdomain, many of which do not even point at a server yet.
DNS-01 proves control of the whole domain instead. Certbot creates a TXT record named _acme-challenge.example.com holding a one-time value, and Let’s Encrypt looks it up on your authoritative nameservers (the DNS servers that answer for your domain). If your problem is a single hostname failing HTTP-01, see how to fix Certbot ACME HTTP-01 challenge failed errors on Nginx instead.
Before You Start
- Server access: SSH to the Nginx server as a user with
sudo. The commands assume Ubuntu or Debian with snap available. - DNS API access: a token from whoever hosts your DNS, which is where your nameservers point and not always where you bought the domain. Cloudflare, DigitalOcean and Route 53 have official Certbot plugins. Namecheap DNS has none, so use the hook-script method below.
- Time: about 20–30 minutes, plus waits for DNS propagation.
- Rollback: a failed Certbot run leaves the live site untouched. Copy your Nginx site file before Step 4 (for example
sudo cp /etc/nginx/sites-available/yoursite /root/yoursite.bak) so you can restore it ifnginx -tfails.
Step 1: Install Certbot and the DNS Plugin
On Ubuntu and Debian, install Certbot from snap. Distribution packages often carry older DNS plugins that lag behind provider API changes.
Run these on the server over SSH. They remove any apt-installed Certbot first; certificates already in /etc/letsencrypt stay where they are, and the snap brings its own renewal timer:
sudo apt remove certbot -y
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/local/bin/certbot
sudo snap set certbot trust-plugin-with-root=okNext, install the DNS plugin for your provider. The two lines below show Cloudflare and RFC2136 (for DNS servers you run yourself, such as BIND); install only the one you need:
sudo snap install certbot-dns-cloudflare
sudo snap install certbot-dns-rfc2136Check it worked: certbot --version prints a version number, and sudo certbot plugins lists the plugin you installed (for example dns-cloudflare).
Step 2: Store the DNS API Token Safely
Certbot needs a token that can edit DNS records in your zone, and nothing more. Create a token limited to that one zone; never use a global account key, because anyone who reads the file could then change your whole account.
On the server, create a folder and a credentials file:
sudo mkdir -p /etc/letsencrypt/credentials
sudo nano /etc/letsencrypt/credentials/cloudflare.iniPaste your token into the file, save it and exit:
# Cloudflare API token with Zone:DNS:Edit permissions
dns_cloudflare_api_token = YOUR_CLOUDFLARE_API_TOKENThen make the file readable by root only, so other users on the server cannot see the token (Certbot warns about unsafe permissions if you skip this):
sudo chmod 600 /etc/letsencrypt/credentials/cloudflare.iniIf your domain is registered at Namecheap but its nameservers point to Cloudflare, use the Cloudflare plugin. What matters is who answers DNS for the domain, not where you bought it.
Step 3: Issue the Wildcard Certificate
A wildcard covers subdomains only, not the bare domain (the apex). To cover both example.com and *.example.com, pass each one with its own -d flag.
Run this on the server, replacing the domain and email address:
sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials /etc/letsencrypt/credentials/cloudflare.ini --dns-cloudflare-propagation-seconds 30 -d example.com -d "*.example.com" --agree-tos --email admin@example.com --no-eff-emailIf it worked, Certbot prints Successfully received certificate. followed by the paths it saved. Use the path it prints: if a certificate named example.com already existed, the new one may be saved as example.com-0001. Run sudo certbot certificates at any time to see each certificate’s name, domains and expiry date.
The files live in /etc/letsencrypt/live/example.com/:
fullchain.pem: The server certificate bundled with Let’s Encrypt’s intermediate certs.privkey.pem: Your private key (keep this locked down).cert.pemandchain.pem: Individual certificate components.
Alternative: Use Hook Scripts When There Is No DNS Plugin
If your DNS provider (Namecheap, for example) has no Certbot plugin, you still don’t have to add TXT records by hand at every renewal. Instead, give Certbot two small scripts: an auth hook that creates the TXT record through your provider’s API with curl, and a cleanup hook that deletes it afterwards.
Create the auth script on the server at /usr/local/bin/certbot-dns-auth.sh. The API URL and JSON body are placeholders; replace them with your provider’s real endpoint and format:
#!/usr/bin/env bash
set -euo pipefail
# Certbot passes $CERTBOT_DOMAIN (without any "*." prefix) and $CERTBOT_VALIDATION
API_KEY="your_secret_api_key"
ZONE="example.com"
RECORD_NAME="_acme-challenge.${CERTBOT_DOMAIN}"
RECORD_VALUE="${CERTBOT_VALIDATION}"
PAYLOAD=$(printf '{"type":"TXT","name":"%s","content":"%s","ttl":60}' "$RECORD_NAME" "$RECORD_VALUE")
# Call the DNS provider API to add a TXT record
curl -fsS -X POST "https://dns-api.example.com/v1/zones/${ZONE}/records" -H "Authorization: Bearer ${API_KEY}" -H "Content-Type: application/json" -d "$PAYLOAD"
# Allow DNS servers time to propagate
sleep 30Certbot runs this hook once per name, so example.com plus *.example.com puts two values on the same _acme-challenge.example.com record: the API call must add a TXT value, not replace one. A cleanup script passed with --manual-cleanup-hook should delete them afterwards.
The script holds your API key, so make it executable and readable by root only:
sudo chmod 700 /usr/local/bin/certbot-dns-auth.shThen request the certificate with the manual authenticator pointed at your script (add --manual-cleanup-hook /path/to/your-cleanup.sh once you have written the cleanup script):
sudo certbot certonly --manual --manual-auth-hook /usr/local/bin/certbot-dns-auth.sh --preferred-challenges dns-01 -d example.com -d "*.example.com" --agree-tos --email admin@example.comCertbot saves the hook paths in the certificate’s renewal settings, so later certbot renew runs call the same scripts without you. The Certbot Pre and Post Validation Hooks documentation lists every environment variable the hooks receive.
Step 4: Point Nginx at the Wildcard Certificate
Now tell Nginx to serve the new certificate for the apex and all subdomains. If the domain already has a server block, only add the ssl_certificate and ssl_certificate_key lines to it. A second block with the same server_name makes Nginx warn about a “conflicting server name” and ignore one of them.
For a new site, create a configuration file on the server:
sudo nano /etc/nginx/sites-available/wildcard.confPaste in this server block, replacing example.com with your domain:
server {
listen 80;
listen [::]:80;
server_name example.com *.example.com;
# Redirect all plain HTTP traffic to HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
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;
# Recommended TLS settings
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}Enable the site, test the configuration and reload. Run the lines one at a time and only run the reload once nginx -t passes:
sudo ln -s /etc/nginx/sites-available/wildcard.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxYou should see syntax is ok and test is successful. If nginx -t reports an error, fix it (or restore your backup) before reloading; the running site keeps working until a reload succeeds. On Nginx 1.25.1 and newer, a warning that listen ... http2 is deprecated is harmless; you can move HTTP/2 to a separate http2 on; line later.
To check that a subdomain gets the new certificate, run this from your own computer: echo | openssl s_client -connect app.example.com:443 -servername app.example.com 2>/dev/null | openssl x509 -noout -ext subjectAltName. The output should list both DNS:*.example.com and DNS:example.com.
Visitors only reach a subdomain if DNS sends it to this server. Either add a record per subdomain or one wildcard record; see how to set up wildcard subdomains in Namecheap DNS.
Step 5: Test Renewal and Reload Nginx Automatically
The snap’s systemd timer renews certificates on its own, but certonly never touches Nginx. Without a reload hook, Nginx keeps serving the old certificate from memory after a renewal.
First, simulate a renewal against the Let’s Encrypt staging server. This does not change your real certificates:
sudo certbot renew --dry-runA passing run reports that all simulated renewals succeeded. Once it does, create a deploy hook, a script Certbot runs after every successful renewal, at /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh:
sudo mkdir -p /etc/letsencrypt/renewal-hooks/deploy
sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh > /dev/null <<'EOF'
#!/usr/bin/env bash
systemctl reload nginx
EOF
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.shFinally, confirm the renewal timer is scheduled. The command should print a line with the next run time and a timer name containing certbot:
systemctl list-timers | grep certbotIf It Still Doesn’t Work: Common DNS-01 Errors
Most failed DNS-01 runs come down to one of these:
- DNS Propagation Delays: Some nameservers take longer than the Cloudflare plugin’s default 10 seconds to replicate TXT records globally. Add
--dns-cloudflare-propagation-seconds 60(or120) to give your nameservers time to sync. - Multi-Level Subdomains: A wildcard cert for
*.example.comonly covers single-level subdomains likeapp.example.com. It will not coverapi.v1.example.com. You need a separate SAN like*.v1.example.comfor nested levels. - Stale TXT Records: If a previous manual run failed, an old
_acme-challengeTXT record might be lingering. Check withdig +short TXT _acme-challenge.example.com, clear old values in your DNS control panel and try again. See the Let’s Encrypt DNS-01 Challenge Specifications for protocol details. - The error mentions CAA: your domain has a CAA record (a DNS record listing which certificate authorities may issue for it) that doesn’t include
letsencrypt.org. Add it, as shown in our guide to configuring CAA records in Namecheap DNS. - Nginx still serves the old certificate: Nginx was not reloaded, or
ssl_certificatepoints at a different folder (such asexample.com-0001). Compare the paths insudo certbot certificateswith your server block, then runsudo nginx -tand reload.
Frequently Asked Questions
Can Certbot issue a wildcard certificate with HTTP-01 or the Nginx plugin?
No. Let’s Encrypt only issues wildcard certificates through the DNS-01 challenge, so certbot --nginx and --webroot cannot request *.example.com. You can still use the Nginx plugin for single hostnames and a DNS plugin for the wildcard on the same server.
How often does Certbot renew wildcard certificates?
Let’s Encrypt certs currently last 90 days (64 days from February 2027, 45 days from 2028). Certbot’s timer checks twice daily and renews once about a third of the lifetime is left, which is 30 days for a 90-day cert.
Do I need to keep port 80 open if I use DNS-01?
No. DNS-01 verification works strictly over DNS queries to your nameservers. You don’t need port 80 open for Certbot to succeed, though leaving port 80 up to redirect HTTP requests to HTTPS is still recommended.
Can I use Certbot DNS-01 with Namecheap DNS?
Yes, but not through an official Certbot plugin. Use the hook-script method above with a script that calls Namecheap’s API, or move DNS hosting to a provider that has a plugin. The domain can stay registered at Namecheap either way; only the nameservers decide which API Certbot has to talk to.
Next Steps
Run sudo certbot certificates once more to confirm the wildcard and the apex are on the same certificate, then point the subdomains you need at this server. If the site sits behind a CDN, set the CDN to connect over HTTPS. If you then hit a loop, follow our fix for ERR_TOO_MANY_REDIRECTS on Nginx behind a CDN.

