> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/di-36-zhang-web-fu-wu-qi/di-36.3-jie-caddy-web-fu-wu-qi.md).

# 36.3 Caddy Web 服务器

Caddy 是一款以 Go 语言编写的开源 Web 服务器，提供安全的 Web 服务。

## 安装 Caddy

使用 pkg 包管理器安装：

```sh
# pkg install caddy
```

或使用 Ports 方式安装：

```sh
# cd /usr/ports/www/caddy/
# make install clean
```

安装完成后，可查看 Caddy 安装包的说明文档，了解必要的配置和注意事项：

```sh
# pkg info -D caddy
```

## 配置 Caddy

目录结构：

```sh
/
├── usr
│   └── local
│       ├── etc
│       │   ├── caddy
│       │   │   └── Caddyfile              # Caddy 主配置文件
│       │   └── rc.d
│       │       └── caddy                  # Caddy RC 服务脚本
│       └── www
│           └── caddy
│               └── index.html             # Caddy 测试页面
└── var
    ├── log
    │   └── caddy
    │       └── caddy.log                  # Caddy 运行时日志
    ├── db
    │   └── caddy
    │       ├── data
    │       │   └── caddy                  # 自动 SSL 证书存储
    │       └── config
    │           └── caddy                  # 配置自动保存
    └── run
        └── caddy
            └── caddy.sock                 # 管理端点 Unix 套接字
```

> **注意**
>
> 由于非特权用户无法绑定 443 端口，将产生错误 `listen tcp :443: bind: permission denied`。

安装上文提到的 `security/portacl-rc`，执行以下命令：

```sh
# pkg install security/portacl-rc
# sysrc portacl_users+=www
# sysrc portacl_user_www_tcp="http https"
# sysrc portacl_user_www_udp="https"
# service portacl enable
# service portacl start
```

配置服务，请按顺序执行以下命令：

```sh
# sysrc caddy_user=www caddy_group=www
# chown -R www:www /var/db/caddy /var/log/caddy /var/run/caddy
# service caddy enable
# service caddy start
```

新建测试页面，执行以下命令创建目录：

```sh
# mkdir -p /usr/local/www/caddy/
```

编辑 **/usr/local/www/caddy/index.html** 文件，写入测试内容：

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FreeBSD 中文社区欢迎你！</title>
</head>
<body>
    <h1 style="text-align: center;">Hello World !</h1>
</body>
</html>
```

启动 Caddy 服务：

```sh
# service caddy start
Starting caddy... done
Log: /var/log/caddy/caddy.log
```

在本机打开 `https://localhost/`，可查看测试页面：

![Caddy FreeBSD](/files/LEj5hwgfoT2n67M2NrCE)

### 参考文献

* OVVV. Caddy 安装与使用教程\[EB/OL]. \[2026-03-25]. <https://blog.ovvv.top/posts/f3ac7ef6/>. 本节测试页面的 HTML 来自此处。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/di-36-zhang-web-fu-wu-qi/di-36.3-jie-caddy-web-fu-wu-qi.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
