LetsEncrypt PHP API with BIND DNS server for ACME DNS-01 challenge setup guide
Are you looking to setup your own DNS server for LetsEncrypt's ACME DNS-01 verification challenges with PHP API then this guide is for you. LetsEncrypt wild card certificates can also be requested using the same DNS records. I use Debian Linux so this guide is based on Debian 12 at the time of this writing. This guide assumes that you already have PHP installed on your system.
Domain registrar DNS records setup
First add a new DNS record for your dns server, for example dns.example.com AAAA 2001:0db8:a55b:42df:5d01:2359:a67e:737d or / and dns.example.com A 203.0.113.9 A/AAAA record with your server IP where you will serve your BIND9 DNS server.
Now for each hostname create a NS record in your domain registrar, for example. NS _acme-challenge.example.com dns.example.com NS _acme-challenge.www.example.com dns.example.com NS _acme-challenge.homeserver.example.com dns.example.com NS _acme-challenge.fileserver.example.com dns.example.com NS _acme-challenge.gameserver.example.com dns.example.com NS _acme-challenge.plexserver.example.com dns.example.com
Server Setup
Install BIND9 DNS server apt install bind9 dnsutils
Now we start configuring the BIND9 server. This example includes the main domain which covers all the subdomains using the DNS server for generating LetsEncrypt certificates
Now configure named.conf.options file, replace the example IP with your own, for IPv4 use listen-on { 127.0.0.1; 203.0.113.9; };, I only use IPv6 so mine is set to listen-on-v6 { ::1; 2001:0db8:a55b:42df:5d01:2359:a67e:737d; };
Now create a new zone file listed above (/var/lib/bind/example.com.zone), replace the values accordingly.
Last line in this file must be a blank line.
Now check the zone file by running named-checkzone example.com. /var/lib/bind/example.com.zone
Now restart BIND server with your new settings systemctl restart bind9
PHP Setup
Now create a new file acme.php with the content below where your BIND9 server and PHP is installed. Passwords for example.com and www.example.com needs to be the same.
Client Server Setup
Now install acme.sh curl https://get.acme.sh | sh -s email=my@example.com
We now need to create a new acme DNS plugin to interact with our PHP API. After installing acme.sh create a new file (dns_phpbind.sh) in the acme plugin directory named /root/.acme.sh/dnsapi/dns_phpbind.sh
If you are setting up a freshly installed server which never had a DNS server before and needs a LetsEncrypt certificate for itself then you might have to change https to http and once everything starts working you can switch back to https for secure PHP API. You can also manually add a TXT record by calling the API in the web browser address bar (See below).
Acme.sh command to use.
Manually adding and removing the TXT records
Let me know if you have any comments or if there is any error in this guide.