Caddy foundations

Write your first Caddyfile

Create a site block, understand its address, and run the configuration in the foreground.

10 minute lesson

~~~

Shortcuts are fine for experiments. For anything you want to keep, you write a Caddyfile, a small text file that describes your sites.

The Caddyfile exists because Caddy’s native configuration format is JSON, and nobody wants to write that by hand. The Caddyfile is a human-friendly layer that Caddy converts to JSON for you.

Create a file named Caddyfile in an empty directory:

:8080 {
  respond "hello from a Caddyfile"
}

The first line is the site address. It tells Caddy what to listen for. :8080 means: any hostname, port 8080, plain HTTP. Everything inside the braces defines how that site responds.

Run it in the foreground:

caddy run --config Caddyfile

Then test it from another terminal:

curl http://127.0.0.1:8080/

While you’re learning, always run Caddy in the foreground with caddy run. You see startup errors and log lines the moment they happen, and Ctrl-C stops the process cleanly instead of leaving a mystery server running in the background.

Why did :8080 give us plain HTTP? Because Caddy has no name to issue a certificate for. Change the site address to a hostname like blog.example.com and Caddy switches to automatic HTTPS for that site. That behavior gets its own module later in this course.

Now the mistake everyone makes on day one: you edit the Caddyfile and nothing changes. A running Caddy does not watch the file. You have to stop and start it, or better, tell the running server to pick up the change with caddy reload. We’ll build a safe reload habit in the operations module — for now, remember that saving the file does nothing by itself.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →