> 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/ask/flat/chapter-38-web-servers/di-38.3-jie-caddy-web-fu-wu-qi.md).

# 38.3 Caddy Web Server

Caddy is an open-source web server written in Go that provides secure web services.

## Installing Caddy

Install using the pkg package manager:

```sh
# pkg install caddy
```

Or install using Ports:

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

After installation, you can view the Caddy package documentation to understand the necessary configuration and precautions:

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

## Configuring Caddy

Directory structure:

```sh
/
├── usr
│   └── local
│       ├── etc
│       │   ├── caddy
│       │   │   └── Caddyfile              # Caddy main configuration file
│       │   └── rc.d
│       │       └── caddy                  # Caddy RC service script
│       └── www
│           └── caddy
│               └── index.html             # Caddy test page
└── var
    ├── log
    │   └── caddy
    │       └── caddy.log                  # Caddy runtime log
    ├── db
    │   └── caddy
    │       ├── data
    │       │   └── caddy                  # Automatic SSL certificate storage
    │       └── config
    │           └── caddy                  # Configuration auto-save
    └── run
        └── caddy
            └── caddy.sock                 # Management endpoint Unix socket
```

> **Note**
>
> Since non-privileged users cannot bind to port 443, the error `listen tcp :443: bind: permission denied` will occur.

Install the `security/portacl-rc` mentioned above by executing the following commands:

```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
```

Configure the service by executing the following commands in order:

```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
```

Create a test page by executing the following command to create the directory:

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

Edit the **/usr/local/www/caddy/index.html** file and write the test content:

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chinese FreeBSD Community (CFC) Welcomes You!</title>
</head>
<body>
    <h1 style="text-align: center;">Hello World !</h1>
</body>
</html>
```

Start the Caddy service:

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

Open `https://localhost/` locally to view the test page:

![Caddy FreeBSD](https://3986180810-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmPkxHcwmULmFWbY1hchs%2Fuploads%2Fgit-blob-0363043013bcada53d98d590673a9d2e9f96d6c4%2Fcaddy1.png?alt=media)

### References

* OVVV. Caddy Installation and Usage Tutorial\[EB/OL]. \[2026-03-25]. <https://blog.ovvv.top/posts/f3ac7ef6/>. The HTML for the test page in this section is from this source.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://book.bsdcn.org/ask/flat/chapter-38-web-servers/di-38.3-jie-caddy-web-fu-wu-qi.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
