Skip to content

Caddy

Background

Caddy - The Ultimate Server with Automatic HTTPS

Outline

  • Caddy Install and Setup
  • Static HTML Hosting
  • HTTP and HTTPS Proxies
  • Securing Caddy's API
  • Examples using Caddy's API
  • Using a Hostname on Local Network
  • Deploying to VPS
  • Setting DNS
  • Use API to setup Server
  • Static Site Hosting Script
  • Cloudflare tutorial

Caddy Install and Setup

Debian/Ubuntu Install

sudo apt install -y 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 install caddy

For other systems check out, Install — Caddy Documentation

Try not to use docker, it's a bitch with Caddy, plus Caddy can proxy into your docker containers anyways.

Caddy Config Files

Getting Started — Caddy Documentation

Static HTML Hosting


caddy file-server --root ~/mysite

HTTP and HTTPS Proxies

HTTPS, signed and unsigned, is the default for Caddy. To enable HTTP you need to make sure the http_port setting is set in the correct part of the config.

  • apps
    • $APP_NAME
      • http_port : $PORT_NUMBER_1
      • servers
        • Server Name
          • listen [":$PORT_NUMBER_1", ":$PORT_NUMBER_2"]

Below is an example where port 8080 is http and 4443 is https.


{
  "apps": {
    "http": {
      "http_port": 8080,
      "servers": {
        "srv0": {
          "listen": [
            ":4443",
            ":8080"
          ],
          ...
        }
      }
    }
  }
}


Securing Caddy's API

  • Examples using Caddy's API
  • Using a Hostname on Local Network
  • Deploying to VPS
  • Setting DNS
  • Use API to setup Server
  • Static Site Hosting Script
  • Cloudflare tutorial

Forward port 80 to 8080 and 443 to 4443


iptables -t nat -A OUTPUT -o lo -p tcp --dport 80 -j REDIRECT --to-port 8080