Caddy Installation & Config Guide
Caddy is a powerful modern Web server written in Go. Its biggest feature is Automatic HTTPS enabled by default (automatically applies for and renews certificates via Let's Encrypt), and its configuration file (Caddyfile) is extremely concise and readable.
HTTPSCaddy Installation
Linux (Debian / Ubuntu / Raspberry Pi)
Install necessary dependencies and add official key:
bash
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
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.listUpdate and install:
bash
sudo apt update
sudo apt install caddyCheck status:
bash
systemctl status caddyLinux (CentOS / RHEL / Fedora)
bash
sudo yum install yum-plugin-copr
sudo yum copr enable @caddy/caddy
sudo yum install caddyWindows
- Go to Caddy Official Download Page to download the Windows executable (
.exe). - Rename the downloaded file to
caddy.exeand place it in a suitable directory (e.g.,C:\caddy). - Open PowerShell or CMD in that directory to run.
API Command Cheat Sheet
| Command | Description |
|---|---|
caddy run | Run Caddy in foreground |
caddy start | Start Caddy in background |
caddy stop | Stop running Caddy |
caddy reload | Reload configuration (run in directory with config file) |
caddy fmt --overwrite | Format Caddyfile and save over it |
Caddyfile Configuration Examples
The default configuration file for Caddy is usually located at /etc/caddy/Caddyfile.
1. Minimalist Static Site
For local testing, a one-line config is enough:
nginx
:8080
file_serverThis starts a static file server on port 8080, with the current running directory as root.
2. Production Static Site (Auto HTTPS)
nginx
example.com {
root * /var/www/html
file_server
}- Note: Replace
example.comwith your real domain and ensure the domain resolves to the server IP. Caddy will automatically apply for an SSL certificate and enable HTTPS.
3. Reverse Proxy
Forward traffic to backend service (e.g., port 3000):
nginx
api.example.com {
reverse_proxy localhost:3000
}It's that simple! Caddy automatically handles HTTPS, Header forwarding, etc.
4. Common Directive Combinations
nginx
domain.com {
# Enable Gzip compression
encode gzip
# Reverse proxy
reverse_proxy 127.0.0.1:8080
# Log configuration
log {
output file /var/log/caddy/access.log
}
}