Setup of a Caddy HTTP Server on Debian 12

This tutorial installs a Caddy HTTP server with TLS/HTTPS.

Caddy does all what we expect from a modern website automatically. No extra configuration is necessary to serve HTTPS. Certificates are obtained and renewed for all domain names. Also it redirects from the HTTP port by default.

Prerequisites

  • Being firm in using the command line.

  • A domain name, called foo.net in this tutorial.

  • A Debian server, set up as depicted in Setup of a Server with Debian 12.

Install

Caddy

# Install
sudo aptitude install caddy

# Start the server on each boot
sudo systemctl enable caddy.service

# Start the server now
sudo systemctl start caddy.service

Firewall

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
sudo ufw status

Configure

Base Configuration

Add this to sudo nvim /etc/caddy/Caddyfile:

# As an alternative to editing the above site block, you can add your own site
# block files in the Caddyfile.d directory, and they will be included as long
# as they use the .caddyfile extension.
import Caddyfile.d/*.caddyfile
sudo mkdir /etc/caddy/Caddyfile.d

For each website

sudo nvim /etc/caddy/Caddyfile.d/foo.net.caddyfile:

foo.net {
    root * /usr/share/caddy/foo
    file_server
    log {
        output file /var/log/caddy/foo.net-access.log
    }
}
sudo systemctl restart caddy

That's really all it needs to obtain certificates for all domain names, serve a static website over HTTPS, and also to redirect from HTTP. See https://caddyserver.com/docs/caddyfile/patterns or https://caddyserver.com/docs/automatic-https for more information.

Copy project to server

rsync -avz --delete -e ssh build/html/ root@<SERVER>:/usr/share/caddy/foo