Skip to content

Caddy

3 posts with the tag “Caddy”

Reverse Proxy with Caddy on RHEL

This article introducing a method on reverse proxy with Caddy web server on RHEL.

sudo dnf install 'dnf-command(copr)'
sudo dnf copr enable @caddy/caddy
sudo dnf install caddy

2. Edit Caddy Config File in /etc/nginx/nginx.conf

Section titled “2. Edit Caddy Config File in /etc/nginx/nginx.conf”
{
email email@example.com
}
(tls) {
tls {
dns cloudflare {env.CF_DNS_API_TOKEN}
protocols tls1.2 tls1.3
}
}
(common_headers) {
encode gzip
}
(secure_headers) {
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
X-Frame-Options SAMEORIGIN
X-Content-Type-Options nosniff
}
}
xxx.example.com {
import tls
import common_headers
import secure_headers
reverse_proxy localhost:80
}

3. Edit Systemd Service File in /usr/lib/systemd/system/caddy.service

Section titled “3. Edit Systemd Service File in /usr/lib/systemd/system/caddy.service”
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target
[Service]
Type=notify
User=caddy
Group=caddy
Environment="CF_DNS_API_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
ProtectHome=read-only
ReadWritePaths=/usr/share/caddy
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target

[1]. https://caddyserver.com/docs/

[2]. https://catcat.cc/post/h9bti/

[3]. RHEL9-Activation

[4]. RHEL9-RPM-Fusion-Repo

WebDav Based On Caddy

This article introducing a method on servering webdav service with CaddyServer.

The easiest way to install caddy is using package manager.

  • On Debian/Ubuntu

    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
  • On RHEL/CentOS/RockyLinux

    dnf install 'dnf-command(copr)'
    dnf copr enable @caddy/caddy
    dnf install caddy
  • Add Packages Online

    sudo caddy add-package XXX

    XXX is the package name, which always start with github.com/xxxxxx.

    It’s easy to find packages name from Download Page.

    Examples:

    install webdav module:

    sudo caddy add-package github.com/mholt/caddy-webdav

  • Add Packages Manually

    Select and Download CaddyServer with modules from Download Page.

    Replace executable caddy file with downloaded:

    sudo mv caddy /usr/bin
    sudo chmod +x /usr/bin/caddy
    sudo restorecon /usr/bin/caddy
    sudo chown root:root /usr/bin/caddy

Due to the configuration in default systemd file, the parameter ProtectSystem=full, caddy lose the permission on writing /usr, /boot and /etc.(Reference-2 for details).

  • Add ReadWrite permission to caddy web folder /usr/share/caddy Adding paramter to service file /usr/lib/systemd/system/caddy.service under [service] lable:

  • Reload systemd

    sudo systemctl daemon-reload

4. Make caddy has authority to access files

Section titled “4. Make caddy has authority to access files”
  • Change the owner of /path/to/data with caddy

    sudo chwon caddy /path/to/data -R

  • Add caddy to the group of the files’ owner

    sudo usermod -a -G $GROUP

  • webdav Module Configure introduction is available: caddy-webdav

    {
    basicauth {
    username password_generate_by_openssl
    }
    root * /path/to/data/
    route {
    rewrite /webdav /webdav/
    webdav /webdav/* {
    prefix /webdav
    }
    }
    file_server browse
    }
  • global config set webdav before file_server:

    {
    order webdav before file_server
    }

[1]. https://caddyserver.com/docs/install

[2]. https://www.redhat.com/sysadmin/mastering-systemd

[3]. https://github.com/abiosoft/caddy-webdav

[4]. Reverse-Proxy-with-Caddy-on-RHEL

Webhook Based On Caddy

This article introducing a method on building webhook service with CaddyServer.

The easiest way to install caddy is using package manager.

  • On Debian/Ubuntu

    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
  • On RHEL/CentOS/RockyLinux

    dnf install 'dnf-command(copr)'
    dnf copr enable @caddy/caddy
    dnf install caddy
  • Add Packages Online

    sudo caddy add-package XXX

    XXX is the package name, which always start with github.com/xxxxxx.

    It’s easy to find packages name from Download Page.

    Examples:

    install webhook module:

    sudo caddy add-package github.com/WingLim/caddy-webhook

    install exec module:

    sudo caddy add-package github.com/mholt/caddy-events-exec

  • Add Packages Manually

    Select and Download CaddyServer with modules from Download Page.

    Replace executable caddy file with downloaded:

    sudo mv caddy /usr/bin
    sudo chmod +x /usr/bin/caddy
    sudo restorecon /usr/bin/caddy
    sudo chown root:root /usr/bin/caddy

Due to the configuration in default systemd file, the parameter ProtectSystem=full, caddy lose the permission on writing /usr, /boot and /etc.(Reference-2 for details).

  • Add ReadWrite permission to caddy web folder /usr/share/caddy Adding paramter to service file /usr/lib/systemd/system/caddy.service under [service] lable:

    ReadWritePaths=/usr/share/caddy

  • Reload systemd

    sudo systemctl daemon-reload

  • Webhook Module Configure introduction is available: caddy-webhook

    route /webhook {
    webhook {
    repo https://github.com/WingLim/winglim.github.io.git
    path /usr/share/caddy
    branch master
    depth 1
    type github/gitlab/gitee/gogs/bitbucket
    command
    username
    password
    key
    token
    submodule
    }
    }
  • Exec Module Configure introduction is available: caddy-exec

    route /webhook {
    exec {
    command git
    args pull
    directory /usr/share/caddy
    log file /var/log/caddy/git.log
    }
    }

[1]. https://caddyserver.com/docs/install

[2]. https://www.redhat.com/sysadmin/mastering-systemd

[3]. https://github.com/abiosoft/caddy-exec

[4]. https://github.com/WingLim/caddy-webhook

[5]. Reverse-Proxy-with-Caddy-on-RHEL