Caddy is a web server that gets and renews TLS certificates on its own. You give it a domain name and a folder (or a backend port), and HTTPS just works, including the HTTP-to-HTTPS redirect. For a personal site, a project page or a small app, that removes most of the moving parts of a classic nginx + certbot setup.
This guide takes a fresh Ubuntu 24.04 or Debian 12 VPS to a live HTTPS site.
Before you start
- A domain you control.
- DNS records pointing at the server, created before you start Caddy, because the certificate authority checks them:
| Type | Name | Value |
|---|---|---|
| A | example.com | your server's IPv4 |
| A | www | your server's IPv4 |
| AAAA | example.com / www | your server's IPv6 (only if the server really has one) |
Check from your laptop: dig +short example.com A must print the server IP. A stale or wrong AAAA record is a classic cause of failed certificates.
-
Ports 80 and 443 free on the server:
ss -tlnp | grep -E ':80 |:443 'should print nothing (stop and remove any other web server first).
1. Install Caddy
Both distributions ship a caddy package (version 2.6.x), which works for everything in this article:
apt update && apt install -y caddy
For the current release, use the official Caddy repository as documented on caddyserver.com:
apt install -y debian-keyring debian-archive-keyring apt-transport-https curl gnupg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \
| gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
| tee /etc/apt/sources.list.d/caddy-stable.list
chmod o+r /usr/share/keyrings/caddy-stable-archive-keyring.gpg
chmod o+r /etc/apt/sources.list.d/caddy-stable.list
apt update && apt install -y caddy
Either way you get a caddy systemd service running as the unprivileged caddy user, with its config at /etc/caddy/Caddyfile.
caddy version
systemctl status caddy --no-pager
2. Open the firewall
HTTP is needed for the certificate challenge and the redirect; UDP 443 enables HTTP/3.
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 443/udp
ufw status
3. Put your site in place
mkdir -p /var/www/example
cat > /var/www/example/index.html <<'EOF'
<!doctype html>
<html lang="en"><meta charset="utf-8"><title>It works</title>
<h1>Served by Caddy over HTTPS</h1></html>
EOF
chown -R root:root /var/www/example
chmod -R a+rX /var/www/example
The caddy user only needs to read the files. Keep them owned by root or by your deploy user, not by caddy.
4. Write the Caddyfile
Replace the default file with this (tabs or spaces both work; caddy fmt will normalise them):
{
email admin@example.com
}
example.com {
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/example.log
}
}
www.example.com {
redir https://example.com{uri} permanent
}
What each part does:
- The global
emailis used for the certificate account and expiry notices. Optional, but useful. - A site address with a domain name tells Caddy to get a certificate for it and redirect HTTP to HTTPS automatically. No extra lines needed.
-
root+file_serverserve the folder;encodecompresses responses. -
headeradds common security headers and removes theServerheader. - The
wwwblock sends visitors to the bare domain with a 301 redirect (and gets its own certificate so the redirect works over HTTPS too).
Validate, format and load it:
caddy fmt --overwrite /etc/caddy/Caddyfile
caddy validate --config /etc/caddy/Caddyfile
systemctl reload caddy
reload applies the new config without dropping connections. If the new config is invalid, Caddy keeps running the old one.
5. Watch the certificate arrive
journalctl -u caddy -f
Within a few seconds you should see lines about obtaining a certificate for example.com and www.example.com. Then test from your laptop:
curl -I http://example.com # 308 redirect to https
curl -I https://www.example.com # 301 to https://example.com/
curl -I https://example.com # 200, with the headers above
Certificates are renewed automatically well before they expire. There is no cron job to maintain.
Testing without hitting rate limits
If you are experimenting (wrong DNS, repeated reinstalls), point Caddy at the staging CA first. Browsers will not trust those certificates, but you cannot exhaust the production rate limits:
{
email admin@example.com
acme_ca https://acme-staging-v02.api.letsencrypt.org/directory
}
Remove the acme_ca line once everything works, then reload.
6. Serving an app instead of files
For a Node, Python, Go or any other app listening on a local port, replace the file server with a reverse proxy:
app.example.com {
reverse_proxy 127.0.0.1:3000
encode zstd gzip
}
Caddy passes the usual X-Forwarded-For and X-Forwarded-Proto headers to the backend. Make the app listen on 127.0.0.1 only, so it is never reachable without TLS, and do not open its port in ufw.
You can mix both in one file: several site blocks, each with its own domain, each with its own certificate.
7. A simple deploy workflow
From your machine, push a built static site with rsync:
rsync -avz --delete ./public/ deploy@SERVER_IP:/var/www/example/
Give the deploy user write access to that folder (chown -R deploy:deploy /var/www/example), and Caddy picks up the new files immediately; no reload is needed for content changes.
Troubleshooting
| Symptom | Check |
|---|---|
| Certificate errors in the journal |
dig the A/AAAA records; port 80 reachable from outside; no other service on 80/443 |
permission denied reading files |
namei -l /var/www/example/index.html: every directory needs x for others |
| Log file errors | The log path must be writable by caddy; /var/log/caddy/ is created by the package |
| Old config still active |
caddy validate the file; check journalctl -u caddy after reload
|
Where to host it
I work with PhantomVPS: the crypto VPS page lists the plans you can pay for with Monero, Bitcoin, Litecoin, USDT, USDC, SOL or TRX. Fast VPS plans start at $7.99 a month with root access, and sign-up needs an email address and a password, no ID documents and no phone number. A small plan is plenty for a static site behind Caddy. Everything in this guide works the same on any Ubuntu or Debian server.
Top comments (0)