Help Running your server Websites and domains
Host a website with HTTPS using Caddy
Install Caddy from its own repository, serve a site or an app from a short Caddyfile, and get certificates that renew themselves.
Caddy is a web server that sets up HTTPS for you: give it a domain and it gets a Let's Encrypt certificate, renews it, and sends http:// visitors to https://.
Before you start#
- The domain must already point at your server:
dig +short example.comshould answer its address. See Point a domain at your server. - Ports 80 and 443 must be open in both firewalls, the server's and the portal's.
- Nothing else may be using ports 80 and 443. Stop Apache or nginx first.
Open the ports on the server:
Ubuntu / Debian
sudo ufw allow 80/tcp && sudo ufw allow 443/tcpAlmaLinux / Rocky
sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reloadOn a VPS, add HTTP and HTTPS rules on the Networking tab too, if the portal firewall is on: see Set up the firewall.
Install Caddy#
Install it from Caddy's own repository, which is current.
Ubuntu / Debian
Add the repository and its signing key:
sudo apt 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 chmod o+r /usr/share/keyrings/caddy-stable-archive-keyring.gpg /etc/apt/sources.list.d/caddy-stable.listThen install it. It starts straight away, and at every boot:
sudo apt update && sudo apt install -y caddyAlmaLinux / Rocky
Caddy's packages for AlmaLinux are on Fedora's COPR service:
sudo dnf install -y 'dnf-command(copr)' && sudo dnf copr enable -y @caddy/caddyInstall it, then start it now and at every boot:
sudo dnf install -y caddy && sudo systemctl enable --now caddyVisit http://203.0.113.10 in your browser, with your server's address. You should see Caddy's welcome page.
Serve a website#
Make a folder for the site, with a page in it:
sudo mkdir -p /var/www/example.comecho '<h1>Hello from example.com</h1>' | sudo tee /var/www/example.com/index.htmlCaddy's settings live in the Caddyfile. Replace what's there with your site:
sudo tee /etc/caddy/Caddyfile <<'EOF'
example.com {
root * /var/www/example.com
file_server
encode zstd gzip
}
www.example.com {
redir https://example.com{uri} permanent
}
EOF| Line | What it does |
|---|---|
example.com { | Starts the site. Naming a domain turns on HTTPS |
root, file_server | Serves the files in that folder |
encode zstd gzip | Compresses pages so they load faster |
www.example.com { redir ... } | Sends www visitors to example.com |
Check the file, then load it. A reload doesn't drop visitors:
caddy validate --config /etc/caddy/Caddyfilesudo systemctl reload caddyVisit https://example.com. The first visit can take a few seconds while the certificate is fetched.
To update the site, copy new files into /var/www/example.com: see Move files to and from your server.
Put an app behind it#
An app listening on a local port like 3000 can sit behind Caddy, which handles HTTPS:
app.example.com {
reverse_proxy 127.0.0.1:3000
}Reload Caddy. Keep the app on 127.0.0.1, so the only way in is through Caddy. One Caddyfile holds as many sites as you like. See Install Docker and Run a Node.js app.
If something's wrong#
Caddy's log says why, usually in plain words:
sudo journalctl -u caddy --no-pager -n 50| The log says | It means |
|---|---|
| no such host, or a timeout getting a certificate | The domain doesn't point here yet, or port 80 or 443 is closed in a firewall |
| address already in use | Something else is on port 80 or 443. See below |
| too many certificates, or rate limit | Let's Encrypt wants you to wait. Fix the cause; Caddy keeps trying |
To find what's on ports 80 and 443:
sudo ss -ltnp | grep -E ':(80|443) 'Still stuck? Open a ticket and say which guide you followed.