Getting started with Caddy
(Originally posted on 2017-12-01 on Medium.) (warning: This article is about v1. A v2 equivalent coming soon!)
Recently I’ve been enjoying using Caddy web server to get automatic-HTTPS websites. I took the time to package it for Fedora and CentOS/RHEL to make it more accessible and more maintainable. This guide demonstrates how to use those packages.
Preparation
In order for Caddy to provision your Let’s Encrypt certificates as soon as it starts, you will need two things configured beforehand.
- a DNS record pointing your domain to your server’s IP address
- port 80 allowed for the ACME HTTP challenge
Since you’ll need port 443 open later anyways, just go ahead and open both now.
firewall-cmd --permanent --add-service=http --add-service=https
firewall-cmd --reload
For the rest of the guide, make sure you replace example.com
with the actual domain name you want to use.
Installation
CentOS and RHEL (make sure EPEL is enabled first):
yum install caddy
Fedora:
dnf install caddy
Content
Put your own content in /var/www/example.com
, or use this example index file to get started now and swap in your own content later.
mkdir -p /var/www/example.com
echo '<h1>Hello world!</h1>' > /var/www/example.com/index.html
If you care about selinux (you should), restore the file context for your content files and directories.
restorecon -r /var/www
Configuration
Create a Caddyfile as /etc/caddy/conf.d/example.com.conf
. Files in /etc/caddy/conf.d
ending in .conf
will be automatically imported by the global Caddyfile /etc/caddy/caddy.conf
, which in turn is read by the systemd service unit.
example.com {
root /var/www/example.com
}
Service
Enable and start the Caddy daemon.
systemctl enable --now caddy
Conclusion
You should now be able to open https://example.com in your browser and view your content.