Build Caddy from Source in 2019 (23/6/19)
Since my previous tutorial in 2018, there are many changes and improvement in Caddy Server. One of the improvement is the building process now far easier than before.
In the past, to build Caddy with several plugins installed, we need to modify Caddy's source code. This is not exactly easy, especially for beginner. Today, thanks to Go module, we can easily do it without modifying the source code.
For example, here we will build Caddy with dns.cloudflare plugin which allows you to obtain certificates using DNS records for domains managed with Cloudflare. Before you start, make sure Go 1.12 or newer already installed on your system.
First, create a new folder anywhere and create a Go file with following content:
package main
import (
// The main Caddy server
"github.com/caddyserver/caddy/caddy/caddymain"
// Caddy plugins, use underscore as import name
_ "github.com/caddyserver/dnsproviders/cloudflare"
)
func main() {
caddymain.Run()
}
Next, initiate the folder as Go module named caddy
:
go mod init caddy
After that, add and update Caddy as module dependency:
go get -u github.com/caddyserver/caddy
Now we just need to install it by running go install
which will put caddy
in $GOPATH/bin
.
As web server, Caddy will need the access to port 80 (HTTP) and 443 (HTTPs). To do so, we need to let Caddy to listen to port < 1024 by running :
sudo setcap cap_net_bind_service=+ep $GOPATH/bin/caddy
Next we need to increase the maximum number of open files in our server. To do this, we need to increase the limit of file descriptor by running :
ulimit -n 8192
Now we could run Caddy in background as the web server. There are several ways to do this. We can use nohup
and cron
to run Caddy in background when the server start. To do this, first open the cron
configuration file by running :
crontab -e
Next add these lines to the end of file :
@reboot . $HOME/.profile && nohup caddy -conf "/path/to/Caddyfile" >> ~/Caddy.log 2>&1 &
We can also create Caddy as system service which might be more preferrable for most people.