Caddyfile Quick-start

Create a new text file named Caddyfile (no extension).

The first thing to type in a Caddyfile is your site’s address:

  1. localhost

If the HTTP and HTTPS ports (80 and 443, respectively) are privileged ports on your OS, you will either need to run with elevated privileges or use a higher port. To use a higher port, just change the address to something like localhost:2015 and change the HTTP port using the http_port Caddyfile option.

Then hit enter and type what you want it to do, so it looks like this:

  1. localhost
  2. respond "Hello, world!"

Save this and run Caddy from the same folder that contains your Caddyfile:

  1. caddy start

You will probably be asked for your password, because Caddy serves all sites — even local ones — over HTTPS by default. (The password prompt should only happen the first time!)

For local HTTPS, Caddy automatically generates certificates and unique private keys for you. The root certificate is added to your system’s trust store, which is why the password prompt is necessary. It allows you to develop locally over HTTPS without certificate errors.

If you get permission errors, you may need to run with elevated privileges.

Either open your browser to localhost or curl it:

  1. curl https://localhost
  2. Hello, world!

You can define multiple sites in a Caddyfile by wrapping them in curly braces { }. Change your Caddyfile to be:

  1. localhost {
  2. respond "Hello, world!"
  3. }
  4. localhost:2016 {
  5. respond "Goodbye, world!"
  6. }

You can give Caddy the updated configuration two ways, either with the API directly:

  1. curl localhost:2019/load \
  2. -X POST \
  3. -H "Content-Type: text/caddyfile" \
  4. --data-binary @Caddyfile

or with the reload command, which does the same API request for you:

  1. caddy reload

Try out your new “goodbye” endpoint in your browser or with curl to make sure it works:

  1. curl https://localhost:2016
  2. Goodbye, world!

When you are done with Caddy, make sure to stop it:

  1. caddy stop

Further reading