All Articles

Deploying static websites over https with Caddy and 1 line of code

I still remember the days, when we had to configure our SSL-Certificates with nginx and apache and it was really painful. But in the last months we started to use caddy. A fresh web server with an simple config and https by default.

Installing Caddy is pretty easy with the following command:

curl https://getcaddy.com | bash

To serve a static .html-site you only need an index.html-file and a file named Caddyfile that looks like this (rember to edit the dns entries to the right IP-Address at your domain service):

my-domain.com, www.my-domain.com

Starting the server is as easy as typing caddy in your root directory of the static site. Basically, that’s it. However, you will see that the caddy exits if you close your SSH-Connection. So you have to make your caddy deployable and autostart when rebooting your server.

You can use systemd to do this. From Ubuntu 15.04 systemd superseded upstart. Create a new file called caddystaticsite.service with the following content:

[Unit]
Description=Caddy Server startup script for static website

[Service]
WorkingDirectory=/code/static-website/
ExecStart=/usr/local/bin/caddy
Restart=always

[Install]
WantedBy=multi-user.target

Remember to set the right WorkDirectory at Line 5. The next step is to copy the file into the right directory for systemd and start the service with the following commands:

cp caddy_static_site.service /etc/systemd/system/caddy_static_site.service
systemctl daemon-reload
systemctl enable caddy_static_site.service
systemctl start caddy_static_site.service
sudo systemctl status caddy_static_site.service # get status of the service - should be active

Now your caddy runs in the background and starts by default, when you reboot your server.