安装

  • 安装必要软件包
    1
    sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
  • 获取Caddy的安全密钥
    1
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
  • 从指定的 URL 下载 Caddy 的官方 GPG 密钥文件
    1
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
  • 更新软件包列表
    1
    sudo apt update
  • 安装Caddy2
    1
    sudo apt install caddy

测试

  • 静态页面
    • 进入/etc/caddy,打开Caddyfile,清空文件内容,然后输入下面的代码
      1
      2
      3
      4
      解析好的域名 {
      root * /var/www/html
      file_server
      }
    • 创建文件夹
      1
      mkdir -p /var/www/html
    • 进入/var/www/html文件夹并创建文件index.html,打开index.html然后输入下面的代码
      1
      2
      3
      4
      5
      6
      7
      <!DOCTYPE html>
      <html>
      <body>
      <h1>caddy</h1>
      <p>验证成功</p>
      </body>
      </html>
  • 反向代理
    1
    2
    3
    4
    解析好的域名 {
    reverse_proxy 127.0.0.1:8080 #这里输入你想反代的服务器IP和端口
    encode gzip
    }
  • 重定向
    1
    2
    3
    解析好的域名 {
    redir https://example.com{uri} #这里输入你想重定向的网址
    }

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
# 启动Caddy2
systemctl start caddy
# 开机自启
systemctl enable caddy
# 重启Caddy2
systemctl restart caddy
# 停止Caddy2
systemctl stop caddy
# 重载配置Caddy配置文件(修改配置文件后执行)
systemctl reload caddy
# 查看Caddy2运行状态
systemctl status caddy