This article applies to both Debian 10 Buster and Ubuntu 20.04 Focal.
xcaddy is the official Caddy tool for custom compiling Caddy, it can help us compile Caddy to meet our needs quickly.
Install xcaddy
Let’s follow the official installation method and first, install some necessary packages.
1
2
3
|
apt update
apt upgrade -y
apt install curl vim wget gnupg dpkg apt-transport-https lsb-release ca-certificates
|
Then follow the official tutorial to install Go.
1
2
|
wget https://go.dev/dl/go1.19.5.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.19.5.linux-amd64.tar.gz
|
Then add go to the system environment variables.
1
|
echo "export PATH=\$PATH:/usr/local/go/bin" >> /etc/profile
|
Then add Caddy’s GPG public key and apt source.
1
2
|
curl -sSL https://dl.cloudsmith.io/public/caddy/xcaddy/gpg.key | gpg --dearmor > /usr/share/keyrings/xcaddy.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/xcaddy.gpg] https://dl.cloudsmith.io/public/caddy/xcaddy/deb/debian any-version main" > /etc/apt/sources.list.d/xcaddy.list
|
Then you can install xcaddy after updating your system.
1
2
|
apt update
apt install xcaddy
|
After rebooting and opening SSH, check the version of go and xcaddy.
1
2
3
4
5
|
root@debian ~ # go version
go version go1.19.5 linux/amd64
root@debian ~ # xcaddy version
v0.3.1 h1:XKmnGnGTeB53hLUgnGr/R4JbTNSxh8IBAFcJkrtycso=
|
Custom Compilation Caddy
We can choose some modules we like, such as Cache Module and Brotli Compression Module.
1
2
3
|
xcaddy build \
--with github.com/caddyserver/cache-handler \
--with github.com/ueffel/caddy-brotli
|
After some compilation time, we can see a binary file named caddy
in the current directory, which is our custom compiled Caddy.
Custom Caddy and System Caddy coexist
If we want to customize Caddy to coexist with the system Caddy, we can use the official tutorial.
First, follow our tutorial Install Caddy, and after installation, stop the Caddy service first:
Then use the dpkg-divert
command to move the system Caddy binary to /usr/bin/caddy.default
and make a soft link to it.
1
|
dpkg-divert --divert /usr/bin/caddy.default --rename /usr/bin/caddy
|
Then move our own compiled Caddy binaries to /usr/bin/caddy.custom
.
1
|
mv ./caddy /usr/bin/caddy.custom
|
Then set the priority so that our custom Caddy starts first.
1
2
|
update-alternatives --install /usr/bin/caddy caddy /usr/bin/caddy.default 10
update-alternatives --install /usr/bin/caddy caddy /usr/bin/caddy.custom 50
|
At this point we can see that the default /usr/bin/caddy
is now our custom Caddy.
1
2
3
4
5
|
root@debian ~ # ls -l /usr/bin/caddy
lrwxrwxrwx 1 root root 23 Jan 22 10:52 /usr/bin/caddy -> /etc/alternatives/caddy*
root@debian ~ # ls -l /etc/alternatives/caddy
lrwxrwxrwx 1 root root 21 Jan 22 10:52 /etc/alternatives/caddy -> /usr/bin/caddy.custom
|
We can also use the update-alternatives --config caddy
command to switch between the system-installed Caddy and the custom Caddy.
1
2
3
4
5
6
7
8
9
10
|
root@be ~ # update-alternatives --config caddy
There are 2 choices for the alternative caddy (providing /usr/bin/caddy).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/caddy.custom 50 auto mode
1 /usr/bin/caddy.custom 50 manual mode
2 /usr/bin/caddy.default 10 manual mode
Press <enter> to keep the current choice[*], or type selection number:
|
We can see that the default Caddy binary is our custom one, you can modify and switch the default Caddy version by entering 0 (automatically according to priority) 1 (manually switch custom Caddy) or 2 (use system default Caddy).