I’ve seen this being asked and answered a few times, but there are bits and pieces all over the place. I have taken the time to repeat and document the entire process starting from a vanilla Ubuntu install to building the binary from scratch to getting Caddy 2 doing its automatic HTTPS magic, serving out a website in this article:
Note that this module is only useful if you have enabled Cloudflare’s “Always Use HTTPS” option, where Cloudflare’s MITM would blindly redirect all HTTP requests, causing Caddy’s HTTP challenge to fail. If you do not enable this option in Cloudflare, you do not need this DNS module at all, as the stock version of Caddy would automatically activate your SSL certificate just fine.
Hopefully, someone would find it useful at some point.