Simple Static Website Hosting with Caddy and Let’s Encrypt
Introduction
This step-by-step guide shows how to host a static website with Caddy (automatic HTTPS included) on a ServerStadium dedicated server or a ServerStadium VM running Ubuntu 24.04. You’ll install Caddy, point your domain, enable free TLS via Let’s Encrypt, and add simple caching—no prior Nginx/Apache experience required.
Prerequisites
- A ServerStadium dedicated server or VM with Ubuntu 24.04.
- A domain name with an A/AAAA record pointing to your server’s public IP.
- SSH access with a sudo-capable user, and an email address for TLS notices.
Step 1 — Install Caddy (Official Repository)
Install from Caddy’s official repository to get the latest stable version.
sudo apt update
sudo apt -y install 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.list
sudo apt update
sudo apt -y install caddy
Step 2 — Create Your Web Root and a Test Page
sudo mkdir -p /var/www/static-site
sudo tee /var/www/static-site/index.html >/dev/null <<'EOF'
Welcome
It works! Caddy on ServerStadium
Your static site is live and ready for content.
EOF
# The Caddy service on Ubuntu runs as the "caddy" user; grant read access:
sudo chown -R caddy:caddy /var/www/static-site
Step 3 — Configure Your Caddyfile (Automatic HTTPS)
Replace example.com
with your domain. Caddy will automatically obtain and renew TLS certificates from Let’s Encrypt as long as DNS points to your server.
sudo tee /etc/caddy/Caddyfile >/dev/null <<'EOF'
{
email you@example.com
}
example.com, www.example.com {
root * /var/www/static-site
file_server
encode gzip zstd
@assets {
path *.css *.js *.png *.jpg *.jpeg *.svg *.ico *.webp *.woff *.woff2
}
header @assets Cache-Control "public, max-age=31536000, immutable"
}
EOF
# Validate and apply:
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl enable --now caddy
sudo systemctl reload caddy
Step 4 — Open the Firewall
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
Step 5 — Test Your Site
- Visit
http://example.com
first—Caddy will redirect to HTTPS after the certificate is issued. - Check the service logs if needed:
sudo journalctl -u caddy -n 100 --no-pager
.
Optional: Redirect “www” to Root (or Vice Versa)
The site label above already covers both example.com
and www.example.com
. If you prefer a strict redirect, use two site blocks like this:
sudo tee /etc/caddy/Caddyfile >/dev/null <<'EOF'
{
email you@example.com
}
www.example.com {
redir https://example.com{uri} permanent
}
example.com {
root * /var/www/static-site
file_server
encode gzip zstd
@assets { path *.css *.js *.png *.jpg *.jpeg *.svg *.ico *.webp *.woff *.woff2 }
header @assets Cache-Control "public, max-age=31536000, immutable"
}
EOF
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
Deploying Content
- Place files under
/var/www/static-site
(HTML, CSS, JS, images). Caddy serves them instantly—no reload required unless you change the Caddyfile. - For simple versioning, keep your site in a Git repo and deploy with
git pull
to the web root.
Optional: Basic Password Protection
Protect a folder with Basic Auth while you stage content. Generate a hash and add a rule:
# Generate a password hash:
caddy hash-password --plaintext 'YourStrongPassword!'
# Example Caddyfile snippet to protect /private
example.com {
root * /var/www/static-site
file_server
basicauth /private {
admin JDJhJDE...paste-the-generated-hash-here...
}
}
Troubleshooting
- Certificate not issued: Confirm DNS A/AAAA records point to your server and ports 80/443 are open. Check logs:
journalctl -u caddy
. - 403/404 errors: Verify
root *
path and file permissions. Ensure web root is readable by thecaddy
user. - Changes not applied: After editing
/etc/caddy/Caddyfile
, runsudo caddy validate
thensudo systemctl reload caddy
.
Want a hassle-free start? Choose an instant, low-cost ServerStadium dedicated server and deploy your static site in minutes.
Conclusion
You’ve deployed a secure static website with automatic HTTPS using Caddy on ServerStadium infrastructure—fast, simple, and production-ready. For more help or information about ServerStadium services, visit our knowledge base or the ServerStadium website.