Using Let's Encrypt on OpenBSD 8.0 with acme-client and httpd
Configure acme-client and httpd on OpenBSD 8.0 to obtain a Let's Encrypt certificate, serve the site over HTTPS and renew it from cron.
OpenBSD includes everything needed to obtain and renew Let's Encrypt certificates in the base system: acme-client(1) talks to the certificate authority and httpd(8) serves the validation files. This guide sets up both on OpenBSD 8.0, the current release, and ends with a site served over HTTPS and a cron job that renews the certificate. OpenBSD 6.1, where acme-client first appeared, is long out of support, and some of its syntax (the -A and -D flags, root { "/acme", strip 2 }) no longer exists.
Requirements
- A server running OpenBSD 8.0 with a public IP address, and a user allowed to run commands through
doas(or a root shell). - DNS A (and AAAA, if you use IPv6) records for
example.comand every additional name pointing to the server. Replaceexample.comwith your domain throughout. - TCP ports 80 and 443 reachable from the internet. The default
pf.confallows them. If you run a restrictive ruleset, add a rule like the one below to/etc/pf.conf, then test and load it.
pass in on egress proto tcp to port { 80 443 }
doas pfctl -nf /etc/pf.conf
doas pfctl -f /etc/pf.conf
acme-client implements only the HTTP challenge (http-01). Let's Encrypt issues wildcard certificates only after DNS validation, so list each host name you need instead of a wildcard.
Step 1: Configure acme-client
OpenBSD ships a sample configuration. Copy it into place:
doas cp /etc/examples/acme-client.conf /etc/acme-client.conf
Edit /etc/acme-client.conf so that it looks like this:
authority letsencrypt {
api url "https://acme-v02.api.letsencrypt.org/directory"
account key "/etc/acme/letsencrypt-privkey.pem"
}
authority letsencrypt-staging {
api url "https://acme-staging-v02.api.letsencrypt.org/directory"
account key "/etc/acme/letsencrypt-staging-privkey.pem"
}
domain example.com {
alternative names { www.example.com }
domain key "/etc/ssl/private/example.com.key"
domain full chain certificate "/etc/ssl/example.com.fullchain.pem"
sign with letsencrypt
}
What the entries mean:
- Each
authorityblock names a certificate authority, its ACME URL and the file holding your account key. The account key is created on first use. domain example.comstarts a section whose name, the handle, is what you pass toacme-clienton the command line. It is also the first name on the certificate.alternative nameslists further names, separated by spaces or commas. Remove the line if you have none.domain keyis the private key for the certificate. If the file does not exist,acme-clientgenerates it. The default type is RSA; appendecdsaafter the file name for an ECDSA key.domain full chain certificatestores the certificate together with the authority's intermediate chain, which is the file a web server needs.sign withselects the authority. While you experiment, useletsencrypt-staging: it issues untrusted test certificates but has far looser rate limits.
Check the file for syntax errors:
doas acme-client -n example.com
Step 2: Serve the challenge directory with httpd
The certificate authority verifies each name by requesting a file under http://example.com/.well-known/acme-challenge/. acme-client writes those files to /var/www/acme, which exists on a default installation. Because httpd runs chrooted in /var/www, it sees that directory as /acme.
Create /etc/httpd.conf with a plain HTTP server for now. The TLS part comes after the certificate exists, since httpd refuses to start when a certificate file is missing.
server "example.com" {
alias "www.example.com"
listen on * port 80
location "/.well-known/acme-challenge/*" {
root "/acme"
request strip 2
}
location * {
block return 302 "https://$HTTP_HOST$REQUEST_URI"
}
}
request strip 2 removes the two leading path components (.well-known and acme-challenge) so that the remaining file name is looked up directly in /acme. Every other request is redirected to HTTPS.
Validate the configuration, then enable and start the daemon:
doas httpd -n
doas rcctl enable httpd
doas rcctl start httpd
Step 3: Request the certificate
Run the client with the handle from the configuration file:
doas acme-client -v example.com
With -v the client reports each step: creating the account and domain keys, the challenge for every name, and finally the files it wrote. Give -v twice to trace the full exchange with the authority. On success you have:
/etc/ssl/private/example.com.key: the private key/etc/ssl/example.com.fullchain.pem: the certificate and chain
If you tested against the staging authority, change sign with to letsencrypt and run doas acme-client -vF example.com; -F forces a new certificate even though the existing one has not expired.
Step 4: Enable HTTPS in httpd
Append a second server block to /etc/httpd.conf:
server "example.com" {
alias "www.example.com"
listen on * tls port 443
tls {
certificate "/etc/ssl/example.com.fullchain.pem"
key "/etc/ssl/private/example.com.key"
}
root "/htdocs/example.com"
}
Create the document root (inside the chroot, so the real path starts with /var/www), add a test page and reload:
doas mkdir -p /var/www/htdocs/example.com
echo "It works" | doas tee /var/www/htdocs/example.com/index.html
doas httpd -n
doas rcctl reload httpd
Step 5: Verify
Check the redirect and the certificate from the server or any other machine:
curl -I http://example.com/
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -issuer -subject -dates
The first command should return a 302 response with an https:// location. The second prints the issuer, the names and the validity period.
Step 6: Renew automatically
acme-client decides for itself whether a renewal is due: for certificates valid for more than 10 days it renews once less than a third of the lifetime remains. Otherwise it does nothing and exits with status 2. That makes it safe to run often. Edit root's crontab:
doas crontab -e
Add this line from the manual page:
~ * * * * acme-client example.com && rcctl reload httpd
The ~ in the minute field picks a random minute, so the job runs once an hour. httpd is reloaded only when the client exits with status 0, meaning the certificate was replaced. The previous certificate is kept beside the new one with a .1 suffix.
Troubleshooting
- Challenge fails with a connection error or 404. Fetch a test file through the same path: create
/var/www/acme/testand requesthttp://example.com/.well-known/acme-challenge/test. A failure means DNS points elsewhere, port 80 is blocked, or thelocationblock is wrong. - One of several names fails. Every name in
alternative namesmust resolve to this server and be answered by the port 80 server block. Add missing names asaliasentries. - Rate limit errors. Repeated failed attempts against the production authority are throttled. Switch to
letsencrypt-staginguntil the setup works. - httpd does not start after adding TLS. Run
doas httpd -n; the usual cause is a path in thetlsblock that does not matchacme-client.conf.
The manual pages acme-client(1), acme-client.conf(5) and httpd.conf(5) are the reference for every option used here.
Frequently asked questions
How do I revoke a certificate?
Run doas acme-client -r example.com. Remove the TLS server block from httpd.conf or request a new certificate afterwards, because the revoked one is still on disk.
Can other daemons use the same certificate?
Yes. Point relayd, smtpd or another service at the same key and full chain files, and add its reload command to the cron line, for example && rcctl reload httpd && rcctl restart smtpd.
Does acme-client work with certificate authorities other than Let's Encrypt?
Any authority that speaks ACME and offers the HTTP challenge can be added as another authority block with its own api url and account key. For authorities that require external account binding, OpenBSD 8.0 added the -e option to pass the key identifier and key.
Do I need to add a second domain to the same section?
Unrelated sites are better kept in separate domain sections with their own key and certificate files. Add one cron line per handle, or chain the commands in a small script.