使用Caddy2作为文件服务器


Caddy 是一个简单的 Web 服务器,自带 HTTPS 功能,非常适合用来搭建简单的文件服务器。


安装 Caddy

1
2
3
4
5
6
7
8
# Debian/Ubuntu
apt install -y debian-keyring debian-archive-keyring apt-transport-https curl

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo apt-key add -
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee -a /etc/apt/sources.list.d/caddy-stable.list

apt update
apt install caddy

配置文件服务器

编辑 Caddyfile 配置文件:

1
vim /etc/caddy/Caddyfile

添加以下内容:

1
2
3
4
5
6
7
8
9
http://yourdomain.com {
basicauth {
bob $2a$14$Eh9lNXTF9yWBmxA... # 使用 caddy hash-password 生成
}
file_server {
root /home/share
browse
}
}

说明

  • basicauth - 设置访问用户名和密码
  • file_server - 启用文件服务器功能
  • root - 指定共享目录路径
  • browse - 开启目录浏览功能

生成密码

密码需要使用 Caddy 工具生成:

1
caddy hash-password

将生成的密码字符串替换到配置文件中。


启动服务

1
2
systemctl enable caddy
systemctl start caddy

访问 http://yourdomain.com 即可看到文件列表页面。