使用Apache搭建WebDav服务

安装Apache:

sudo apt install apache2 apache2-utils

启用所需模块:

sudo a2enmod dav
sudo a2enmod dav_fs
sudo a2enmod ssl
sudo a2enmod auth_digest

创建用于登录WebDav的用户:

sudo htpasswd -c /etc/apache2/webdav.password username

为WebDav目录添加权限:

sudo chown -R www-data:www-data /webdav-dir

测试WebDav目录是否有权限:

sudo -u www-data ls /webdav-dir

创建配置文件:

sudo vi /etc/apache2/sites-available/webdav.conf

内容如下:


	# 将访问路径 /webdav 映射到服务器目录 /webdav-dir
	# 访问 http://example.com/webdav实际访问的是 /webdav-dir 目录
	Alias /webdav /webdav-dir

	
		DAV On # 允许对该目录进行 WebDAV 操作
		AllowOverride None # 不允许目录中的 .htaccess 文件覆盖这里的配置
		Options Indexes # 当目录中没有索引文件(如 index.html)时,显示文件列表
		DirectoryIndex disabled # 禁用默认索引文件
		php_admin_flag engine off # 禁用PHP模块在这个目录生效
		
		# 使用 HTTP Basic 认证
		AuthType Basic
		AuthName "My WebDAV" # 弹出认证对话框时显示的名称
		AuthUserFile /etc/apache2/webdav.password # 密码文件路径
		Require valid-user # 必须登录才能访问
	

其中需要注意DirectoryIndex disabled,如果没有这一行,当目录中包含index.html或index.php等索引文件时,客户端在列出该目录时就会返回405错误。

如果需要SSL安全访问,可创建配置文件webdav-ssl.conf:

sudo vi /etc/apache2/sites-available/webdav-ssl.conf

配置内容如下:


	Alias /webdav /webdav-dir

	# 配置SSL证书
	SSLEngine On
	SSLCertificateFile /etc/ssl/certs/webdav.pem
	SSLCertificateKeyFile /etc/ssl/private/webdav-key.pem

	
		DAV On
		AllowOverride None
		Options Indexes
		DirectoryIndex disabled
		php_admin_flag engine off
		
		AuthType Basic
		AuthName "My WebDAV"
		AuthUserFile /etc/apache2/webdav.password
		Require valid-user
	

注意其中的证书路径,根据实际情况填写。

检测配置是否正确:

sudo apache2ctl configtest

返回Syntax OK则说明一切正常。

创建符号链接:

sudo a2ensite webdav.conf

以上命令相当于:

ln -s /etc/apache2/sites-available/webdav.conf \
/etc/apache2/sites-enabled/webdav.conf

sites-available目录存放可用的站点配置文件,sites-enabled目录存放已启用的站点配置文件,通常是指向sites-available中对应配置文件的符号链接。

只有当创建新的配置文件,且需要启用时,才需要执行a2ensite。

同样的,需要创建webdav-ssl.conf的符号链接:

sudo a2ensite webdav-ssl.conf

重载配置:

sudo systemctl reload apache2

如果需要重启Apache服务,使用命令:

sudo systemctl restart apache2

如果已有相同端口、相同域名的配置文件,可能会引起冲突,则需要合并相关配置到同一个文件中。

原创文章,作者:,如若转载,请注明出处:https://ce.771633.xyz/4458.html

Like (0)
Previous 2025年10月25日
Next 2025年10月25日

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注