Log inSign up

Serving a site over HTTPS with Caddy

Caddy fetches and renews TLS certificates on its own, so HTTPS for example.net takes a three-line Caddyfile once DNS points at the instance and ports 80 and 443 are open. This guide covers install, static files, PHP, reverse proxying an app and checking the result.

What has to be ready before Caddy?

Three things, in this order, or the certificate request fails:

  • DNS: example.net and www.example.net resolve to the instance. Check with dig +short example.net @1.1.1.1; the answer must be your IPv4 from Overview, and the AAAA must match the IPv6. See the DNS records tutorial.
  • Ports: TCP 80 and 443 accept traffic from anywhere. Caddy uses port 80 for the HTTP challenge and redirects, and 443 for TLS.
  • No other web server holds those ports. Run sudo ss -ltnp '( sport = :80 or sport = :443 )' and stop anything listed, such as an Apache left over from an image.

How do you install Caddy?

Use the official Caddy apt repository on Ubuntu and Debian, which tracks current releases:

sudo apt-get install -y debian-keyring debian-archive-keyring apt-transport-https curl gnupg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \
  | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
  | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt-get update
sudo apt-get install -y caddy
caddy version

The package creates a caddy user, a systemd unit and the config at /etc/caddy/Caddyfile. On Fedora use sudo dnf install caddy; on AlmaLinux and Rocky enable the Caddy COPR repository first. Alpine has it as apk add caddy.

Right after install Caddy serves a welcome page on port 80. Open http://198.51.100.24 to see it; that proves the port is reachable.

What goes in the Caddyfile for a static site?

The site name, a document root and the file server. Put your files in /var/www/example and replace /etc/caddy/Caddyfile with:

example.net, www.example.net {
	root * /var/www/example
	encode zstd gzip
	file_server
}
sudo mkdir -p /var/www/example
echo '<h1>It works</h1>' | sudo tee /var/www/example/index.html
sudo caddy fmt --overwrite /etc/caddy/Caddyfile
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy

Naming the domain is what turns on automatic HTTPS. Within a few seconds Caddy requests certificates for both names, stores them under /var/lib/caddy and starts redirecting plain HTTP to HTTPS.

How do you proxy an app running on localhost?

Bind the app to 127.0.0.1 and let Caddy face the internet. For a Node, Python or Go service on port 3000:

app.example.net {
	reverse_proxy 127.0.0.1:3000
}

Caddy passes the client address in X-Forwarded-For and the scheme in X-Forwarded-Proto, which most frameworks read once you tell them to trust the local proxy. WebSockets work through reverse_proxy without extra lines.

For several backends, list them and pick a policy:

api.example.net {
	reverse_proxy 127.0.0.1:8001 127.0.0.1:8002 {
		lb_policy least_conn
		health_uri /healthz
	}
}

How do you run PHP behind Caddy?

Install PHP-FPM and point php_fastcgi at its socket. On Ubuntu 24.04 the version is 8.3:

sudo apt-get install -y php8.3-fpm php8.3-mysql php8.3-curl php8.3-xml php8.3-mbstring
sudo usermod -aG www-data caddy
example.net {
	root * /var/www/example
	php_fastcgi unix//run/php/php8.3-fpm.sock
	file_server
	encode gzip
}

Reload Caddy and drop a test file with echo '<?php echo PHP_VERSION;' | sudo tee /var/www/example/v.php. Visiting /v.php should print the version; delete the file afterwards. On Debian 12 the socket is php8.2-fpm.sock, on Debian 13 it is php8.4-fpm.sock.

Which redirects and headers are worth adding?

Pick one canonical name and send the other to it, and add a few security headers. This keeps the bare domain as the main address:

www.example.net {
	redir https://example.net{http.request.uri} permanent
}

example.net {
	root * /var/www/example
	file_server
	encode zstd gzip
	header {
		Strict-Transport-Security "max-age=31536000"
		X-Content-Type-Options "nosniff"
		Referrer-Policy "strict-origin-when-cross-origin"
		-Server
	}
	log {
		output file /var/log/caddy/access.log
	}
}

Add HSTS only once HTTPS works on every name you serve, because browsers remember it for the full max-age.

How do you check the certificate and the site?

From your own computer, ask for headers and the certificate details:

curl -sI https://example.net | head -n 5
curl -sI http://example.net | grep -i location
echo | openssl s_client -connect example.net:443 -servername example.net 2>/dev/null \
  | openssl x509 -noout -issuer -dates

The first command should show HTTP/2 200, the second a redirect to https, the third the issuer and a valid date range of about 90 days. Caddy renews well before expiry; you never set up a timer for it.

Check IPv6 separately with curl -6 -sI https://example.net. If that fails while IPv4 works, either the AAAA record is wrong or a firewall rule only opened 443 on IPv4.

What if the certificate does not issue?

Read the log; Caddy states the reason plainly.

sudo journalctl -u caddy --since "15 min ago" --no-pager | grep -iE 'error|challenge|acme'
  • NXDOMAIN or wrong IP: DNS is not live yet or points elsewhere. Fix the record and wait for the TTL.
  • Timeout during connect: port 80 or 443 is blocked in your nftables, ufw or firewalld rules.
  • CAA error: your CAA record excludes the authority Caddy tried. Allow it or remove the record.
  • Too many requests: repeated failed attempts hit the authority's limits. Fix the cause and wait an hour; for experiments add acme_ca https://acme-staging-v02.api.letsencrypt.org/directory in the global options block.

Once the error clears, sudo systemctl reload caddy retries straight away.

Questions about this tutorial

Do I need Certbot with Caddy?

No. Caddy requests and renews certificates itself. Installing Certbot alongside it only adds a second process competing for port 80.

Can Caddy serve several domains on one instance?

Yes. Add one block per site name in the Caddyfile; each gets its own certificate automatically.

Does it work on IPv6 only?

Caddy listens on IPv4 and IPv6 by default. With matching A and AAAA records it serves both.

Where are the certificates stored?

Under /var/lib/caddy/.local/share/caddy when installed from the package. Include that folder in your backups to avoid re-issuing after a restore.

Is Caddy a good fit for the WordPress image?

The WordPress app image comes with its own web stack. Use Caddy on a plain OS image, or read the WordPress image page for what that image includes.