Using Caddy Server with VPS for Automatic HTTPS
Introduction
Caddy is a modern web server with automatic TLS, making HTTPS setup trivial. This guide covers installing Caddy on a VPS and configuring reverse proxies.
Prerequisites
- Ubuntu 20.04+ VPS
- Root or sudo access
- Domain name pointing to VPS IP
Step 1: Install Caddy
# Install via official repository
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo apt-key add -
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
Step 2: Basic Caddyfile
Edit /etc/caddy/Caddyfile
:
example.com {
reverse_proxy localhost:3000
}
example.com
: your domainreverse_proxy
: forwards traffic to local service
Step 3: Test Configuration
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
Step 4: Configure Multiple Sites
example.com, www.example.com {
reverse_proxy localhost:3000
}
api.example.com {
reverse_proxy localhost:4000
}
Caddy obtains and renews certificates automatically.
Step 5: Logging and Monitoring
By default, Caddy logs to systemd. To customize, configure log
in Caddyfile:
example.com {
log {
output file /var/log/caddy/example.access.log
format console
}
reverse_proxy localhost:3000
}
Summary
Caddy’s automatic HTTPS and simple configuration make it ideal for VPS-hosted applications. Define sites in the Caddyfile
, and Caddy handles certificates and proxying seamlessly.