Serve static websites with Caddy web server


A new and super-easy-to-configure webserver written in Go

346 words · 2 min read

First, a little bit of history.

I have been running websites—personal or otherwise—on-and-off for long enough to remember Geocities. Although I wouldn’t claim to be an expert, I’ve seen and done quite a bit over the years, from hand-coding HTML files and uploading files via FTP to running Wordpress on LAMP and LEMP stacks and experimenting with Varnish Cache.

I stopped making websites for a while before building this website with static site generators (first with Jekyll, then with Hugo). Static sites are simpler, cheaper to run, and with far better performances than dynamic sites such as Wordpress.

Initially, I put this website on Github Pages, which is free and fast, but lacks flexibility. For months, I’ve been itching to migrate (the move to Firebase proved short-lived) while dreading the prospect of having to configure and maintain a server.

Then I discovered Caddy web server, the easiest-to-set-up web server I’ve ever used.

Setting up Caddy

At the most basic level, the configuration file, Caddyfile, can be as short as this:

example.com:443
root /path-to-your-site-root/

Or this:

And this is all you need to enable HTTPS with a certificate issued by Let’s Encrypt. No additional work required!

Note: since this website is behind Cloudflare, you won’t see Caddy in the response header, and the certificate is issued through Cloudflare; the Let’s Encrypt certficate is used for traffic between the origin server and Cloudflare.

To allow non-root user to bind to port 80 and 443, run the following command in your terminal:

sudo setcap cap_net_bind_service=+ep /path-to/caddy

Finally, set up a systemd service with the following unit file:

[Unit]
Description=Caddy HTTP/2 web server %I
Documentation=https://caddyserver.com/docs
After=network.target

[Service]
User=username
LimitNOFILE=8192
Environment=STNORESTART=yes
ExecStart=/path-to/caddy -agree=true -conf=/path-to/Caddyfile
Restart=on-failure

[Install]
WantedBy=multi-user.target

Save it as /etc/systemd/system/caddy.service, then run these commands:

sudo systemctl daemon-reload
sudo systemctl enable caddy.service
sudo systemctl start caddy.service

Now, Caddy web server will start on boot.

The simplicity and automatic HTTPS feature are what set Caddy apart from other servers. With its git addon, which I’ve touched upon here, it fits nicely to my workflow.

Download Caddy here and try it out now.

 Tech    31 Aug, 2016
 Web Development    Caddy  
Copyright © Peter Y. Chuang 2017

Peter Y. Chuang is a Hong Kong-born novelist and short story writer who’s lived in London and calls Berlin his spiritual home. He has completed the manuscript of a literary science fiction novel, Twenty Forty-Seven,” and is currently re-writing another literary novel, Only You Know What It Means.”

You may also like...