Caddy is a powerful, extensible platform to serve your sites,
services, and apps. It is written in Go.
In addition to being a web server, I've used Caddy with a
reverse proxy to Node to perform other web services.
Prior To Installing Caddy
- log into the remote machine
sudo apt update
sudo apt dist-upgrade
sudo apt autoclean
sudo apt autoremove
- make sure your A and AAAA DNS records point to this machine
Install Caddy on Ubuntu
Note: for other operating systems,
this page
shows how to download and install Caddy.
sudo apt install -y debian-keyring
debian-archive-keyring apt-transport-https
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 install caddy
sudo setcap CAP_NET_BIND_SERVICE=+eip $(which
caddy)
Run Caddy (even on reboot)
whereis caddy
sudo mv caddy /usr/bin/
caddy version # tests that it worked
Caddy may have created a user and group, but just in case:
sudo groupadd --system caddy
sudo useradd --system \
--gid caddy \
--create-home \
--home-dir /var/lib/caddy \
--shell /usr/sbin/nologin \
--comment "Caddy web server" \
caddy
Now create the webroot location and some web content:
sudo mkdir /var/www
sudo chown ubuntu:caddy /var/www
sudo mkdir /var/www/public
sudo chown ubuntu:caddy /var/www/public
- create some HTML to show:
emacs /var/www/public/index.html
<!DOCTYPE html>
<html>
<head><title>Hello from Caddy!</title></head>
<body>
<h1 style="font-family:arial; text-align:center;">
This page is being served via Caddy
</h1>
</body>
</html>
Download the systemd unit file from the Caddy GitHub and run systemctl:
sudo sh -c 'curl https://raw.githubusercontent.com/caddyserver/dist/master/init/caddy.service > /etc/systemd/system/caddy.service'
more /etc/systemd/system/caddy.service # make
sure ExecStart and ExecReload are correct
sudo systemctl daemon-reload #
reload systemctl to detect the caddy
service
sudo systemctl enable --now caddy # keep
caddy running on a reboot
sudo systemctl status caddy #
verify running
Set up the Caddyfile Config
Note that the filename is uppercase.
cd /etc/caddy
sudo mv Caddyfile CaddyfileOrig
sudo emacs Caddyfile
http:// {
root * /var/www/public
encode gzip
file_server
}
sudo caddy fmt --overwrite Caddyfile
sudo caddy validate
I've noticed that logs stop on a simple reload, so do a
force stop/start of caddy:
sudo systemctl stop caddy
sudo systemctl restart caddy
sudo systemctl status caddy
You can visit your server’s IP in a web browser to see the page!
A More Detailed Caddyfile And Logging
To enable logging, set up the Caddyfile and create directories.
Caddy does some nice things to format logs and keep
them from filling up your file system.
Replace "exampleCom" with your domain name.
sudo mkdir /var/www/logs
sudo chown caddy /var/www/logs
sudo mkdir /var/www/logs/caddyLogs
sudo chown caddy /var/www/logs/caddyLogs
sudo mkdir /var/www/logs/exampleComLogs
sudo chown caddy /var/www/logs/exampleComLogs
sudo emacs /etc/caddy/Caddyfile
# @fileoverview: Caddyfile see also https://caddyserver.com/docs/caddyfile
#global
{
# enable caddy server logs -- sudo chown caddy this log
log {
output file /var/www/logs/caddyLogs
}
}
#:80 {
#example.com {
http:// {
root * /var/www/public # set the web root path to your site's directory
file_server # enable the static file server.
encode gzip # enable compression
# enable the site-specific logs -- sudo chown caddy this log
log {
output file /var/www/logs/exampleComLogs
}
handle_errors {
# make all errors (404, etc) go to error.html
rewrite * /error.html
file_server
}
# reverse_proxy localhost:3334 # set up a reverse proxy
# php_fastcgi localhost:9000 # serve a PHP site through php-fpm
}
# example of re-direction but cloudflare and caddy handle it
#www.example.com {
# redir https://example.com
#}
sudo caddy fmt --overwrite Caddyfile
sudo caddy validate
I've noticed that logs stop on a simple reload, so do a
force stop/start of caddy:
sudo systemctl stop caddy
sudo systemctl restart caddy
sudo systemctl status caddy
- create /var/www/error.html
Of Note:
-
To read the logs:
journalctl -u caddy --no-pager | less +G
-
The Caddy process will run as the caddy user, which
has its
$HOME
set to /var/lib/caddy
-
The default data storage location (for certificates and
other state information) will be in
/var/lib/caddy/.local/share/caddy
-
When Caddy runs as a service it runs as caddy user.
It won't have permission to install its root CA
certificate to the system trust store. To do this, run
sudo caddy trust
to install.
Caddy Paths
- caddy executable:
/usr/bin/caddy
- caddy config file:
/etc/caddy/Caddyfile
- web pages:
/var/www/public
Caddy Service Commands
- caddy status:
sudo systemctl status caddy
- stop caddy:
sudo systemctl stop caddy
- restart caddy:
sudo systemctl restart caddy
- gracefully reload caddy:
sudo systemctl reload caddy
- see the caddy service config:
more /etc/systemd/system/caddy.service
Uninstalling Caddy
sudo systemctl stop caddy
sudo systemctl disable --now caddy
sudo systemctl daemon-reload
sudo systemctl reset-failed
systemctl cat service
sudo rm /etc/apt/sources.list.d/caddy-stable.list
sudo apt remove caddy
sudo groupdel --system caddy
sudo killall -u caddy
sudo userdel -rf caddy
References
-
Welcome — Caddy Documentation
-
Install — Caddy Documentation
-
Getting Started — Caddy Documentation
-
Keep Caddy Running — Caddy Documentation
-
Caddyfile Tutorial — Caddy Documentation
-
How To Host a Website with Caddy on Ubuntu 22.04 |
DigitalOcean
-
How to solve status 403 in Caddy version 2 - Help
-
JSON Config Structure - Caddy Documentation
-
Caddy Wiki