CaddyServer Configuration

Configure

Your site is already fit for production! But it's not ideal, because we happen to running on localhost (your home computer):

  1. The site is being served on port 2015, not 80 (the standard HTTP port).
  2. The site isn't protected with HTTPS.

It's easy to fix both of these simply by telling Caddy the name of the site to serve. By "name", we mean a domain name. We'll use example.com here, but you use your real domain. This next part is only going to work if your computer is accessible from the wider Internet on ports 80 and 443, and your domain name points to the computer you're on. If not, or you don't have a real domain name, then use localhost as your domain name.

The name of the site is also called the host or hostname. One way to specify the host is with a command line option:

 
caddy -host example.com
 

The first time you run Caddy with a real hostname (not localhost), you'll be asked to enter your email address. This is because Caddy needs to verify you own the domain and to store the certificate safely on disk for you.

After submitting your email address, do you see an error like permission denied? That's because Caddy is trying to bind to ports 80 and 443 for a real site, but doing this requires root or Administrator privileges:

 
On a real server, you would consider using setcap like this: sudo setcap cap_net_bind_service=+ep $(which caddy)
$ caddy -host example.com  
 
But for your own computer, running Caddy with sudo probably fine:  sudo caddy -host example.com


If you got your permissions right and run Caddy again, you'll see:

Activating privacy features... done. https://example.com http://example.com



Using a real domain name triggers Caddy's privacy features, which operate on ports 80 and 443. If you're just using localhost as your hostname, Caddy continues to serve on port 2015 unless you change it with the -port option.

The command line interface is great for quick Caddy configurations. But what if you want to reuse the same config every time? It's easy with the Caddyfile.

The Caddyfile is a text file that tells Caddy how to serve. It usually lives next to your site. Let's make one:

 
Use your actual domain name (or localhost):
echo example.com > Caddyfile
 

Caddy will automatically find that file when you start it:

caddy

This works because the first line of a Caddyfile is always the address (or name) of the site to serve.

If the Caddyfile is ever in another directory than the current one, you can tell Caddy where to get the Caddyfile:

 
caddy -conf ../path/to/Caddyfile



Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 1316