> 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](/files/LMfKtcUf5hOWOYiTeDqi)

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

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

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

The question should be specific, self-contained, and written in natural language.
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.
