Goodbye NGINX, Hello Caddy

So recently I switched my personal website away from Nginx to Caddy. I've been planning this for quite a while as I wanted to give it a try.

Goodbye NGINX, Hello Caddy

So recently I switched my personal website away from Nginx to Caddy. I've been planning this for quite a while as I wanted to give it a try, as I've heard a lot about it and it's perks. So in this post I'm going to go over my journey and setup, the downs and ups I've had thus far, and what my jurisdiction is on it.

Obtaining the web server

The first thing in any web server journey is installing and setting up the web server software itself. As my VPS is running ubuntu, installing things like Nginx and Caddy are usually as simple as running

sudo apt install -y <package>

however this was not the case this time, as due to my server being behind cloudflare, I had to enable a plugin for caddy to enable cloudflare dns support.

To do this I first had to install the latest version of the Go language, as Caddy is built on Go, however the latest version in the Ubuntu repositories at the time was Go 1.13, and Caddy needed Go 1.14, to fix this I ran the following commands to get Go 1.14.2 and finally added Go to my path.

wget -c https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local

After Go was installed, I installed Caddy's xcaddy tool, to allow me to build the web server from source using the plugin, the tool was fairly easy to use and allowed me to build the executable with a single command

xcaddy build --with github.com/caddy-dns/cloudflare

After it finished compiling I just slapped the single executable into /usr/bin and now had Caddy installed.

Configuration

After installing Caddy came configuration, this part was a little tricky at first but after a while I understood what I needed to do. The first thing I had to do was create a scoped cloudflare API token with 2 permissions, those can be read about here.

Making the actual configuration for the web server was extremely simple compared to Nginx configuration files, below is my current configuration for my website

<domain here> {
  root * /var/www/html
  file_server

  tls <email here> {
    dns cloudflare {env.CLOUDFLARE_API_TOKEN}
  }
}

After that you can either tell Caddy to use the Caddyfile or convert it to json with a command, and use that, so I went ahead and did the latter with the below command

caddy adapt --config Caddyfile >> /etc/caddy/config.json

then finally I was able to start the web server with a simple command

sudo caddy start --config /etc/caddy/config.json

Systemd Configuration

As I wanted to be able to run Caddy in the background and let systemd manage it, I created a simple service for it

[Unit]
Description=Caddy Web Server
Requires=network.target

[Service]
ExecStart=/usr/bin/caddy run --config /etc/caddy/config.json
ExecReload=/usr/bin/caddy reload --config /etc/caddy/config.json
ExecStop=/usr/bin/caddy stop

[Install]
WantedBy=multi-user.target

Final Words

Installing Caddy was surely a journey, but caddy does have it's upsides, from automatic certificate renewal, automatic https/ssl in general, being built on newer technologies, and simpler configurations and plugin installation, I think it was worth it in the end. You can read more about Caddy here. Thanks for reading!