共计 3911 个字符,预计需要花费 10 分钟才能阅读完成。
Httpd 配置文件上传
因为公司产品功能需要实现文件存储,所以调研下 Apache httpd 如何开启上传功能
实现方式
目前存在两种方式:
- WebDAV 方式
- CGI 方式
WebDAV 方式
Apache 中默认已经集成了 WebDAV 模块,只需要在 httpd.conf 中开启即可。
介绍
WebDAV 是一种基于 HTTP 协议的文件传输协议,它允许用户通过 HTTP 协议访问和管理文件系统。WebDAV 协议可以让用户在 Web 浏览器中访问和管理文件,而不需要安装任何客户端软件。WebDAV 协议还支持文件版本控制、文件锁定、文件共享等功能。
查看是否开启 WebDAV
[root@centos conf]# httpd -M | grep dav
dav_module (shared)
dav_fs_module (shared)
dav_lock_module (shared)
[root@centos conf]#
显示已经开启模块,实际上 WebDAV 模块是在 conf.modules.d 目录下的 00-dav.conf 文件中开启的
[root@centos conf.modules.d]# pwd
/etc/httpd/conf.modules.d
[root@centos conf.modules.d]# ls -l
total 28
-rw-r--r--. 1 root root 3739 May 30 2023 00-base.conf
-rw-r--r--. 1 root root 139 May 30 2023 00-dav.conf
-rw-r--r--. 1 root root 41 May 30 2023 00-lua.conf
-rw-r--r--. 1 root root 742 May 30 2023 00-mpm.conf
-rw-r--r--. 1 root root 957 May 30 2023 00-proxy.conf
-rw-r--r--. 1 root root 88 May 30 2023 00-systemd.conf
-rw-r--r--. 1 root root 451 May 30 2023 01-cgi.conf
[root@centos conf.modules.d]#
其中 00-dav.conf 文件中已经开启了 WebDAV 模块,主配置文件中采用 Include 引入了 00-dav.conf 文件
IncludeOptional conf.d/*.conf
配置 WebDAV
在 httpd.conf 文件中修改目录配置
# Dav 锁数据库的位置
DAVLockDB "/var/www/html/uploads/Dav.lock"
<Directory "/var/www/html/uploads">
# 开启 Dav
Dav on
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
需改后重启服务
systemctl restart httpd
测试
WebDav 标准上传使用 PUT 方法
curl 命令格式
curl -X PUT -T 本地文件路径 -u 用户名: 密码 http:// 服务器地址 / 远程路径 /
curl -X PUT -T start.sh http://192.168.100.188:80/dstore_repo/
分块上传大文件
上传测试文件,返回状态码为 201 则上传成功,注意:无论目录是什么最后的‘/’不能忽略不写。
curl -X PUT -T largefile.zip --limit-rate 1M http://example.com/dav/largefile.zip
# test
curl -X PUT -T hbis-xa.nginx.1.26.2.docker.img.gz --limit-rate 1M http://192.168.100.188:80/dstore_repo/hbis-xa.nginx.1.26.2.docker.img.gz
CGI 方式
这种方式需要在 httpd.conf 中开启 cgi 模块,并且在 cgi.conf 中配置 cgi 解释器。
介绍
CGI(Common Gateway Interface)是一种用于在 Web 服务器和应用程序之间传输数据的协议。CGI 是一种标准协议,它允许 Web 服务器将请求转发给应用程序,并将应用程序的输出返回给 Web 服务器。CGI 协议的主要目的是将 Web 服务器和应用程序解耦,使得 Web 服务器可以处理多个应用程序。
原理
Apache 服务器通过 CGI(Common Gateway Interface,通用网关接口)或 FastCGI 技术来处理文件上传请求。当用户通过 Web 表单提交文件时,Apache 服务器将文件数据发送到 CGI 或 FastCGI 脚本进行处理。
查看是否开启 cgi
[root@centos conf]# httpd -M | grep cgi
cgi_module (shared)
显示已经开启模块
配置
修改 http.conf 中的 Files 为如下
<Files ~ ".(cgi|fcgi|pl|py|htaccess)$">
Order allow,deny
Allow from all
</Files>
ScriptAlias /upload "/var/www/html/script.py"
这样,Apache 服务器允许上传的文件扩 s 展名包括 cgi、fcgi、pl、py 和 htaccess。
创建 Html 表单
vi /var/www/html/index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> 文件上传 </title>
</head>
<body>
<h1> 文件上传 </h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="file"> 选择文件:</label>
<input type="file" name="file" id="file" required>
<br><br>
<input type="submit" value="上传">
</form>
</body>
</html>
创建 cgi 脚本
vi /var/www/script.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import cgi
import cgitb
# 启用 CGI 错误跟踪
cgitb.enable()
def upload_file():
# 解析表单数据
form = cgi.FieldStorage()
# 检查是否有文件上传
if "file" not in form:
print "Content-Type: text/plain" # 输出 HTTP 头
print # 确保在头部之后有一个空行
print "Error: No file uploaded."
return
# 获取文件对象
file_item = form["file"]
upload_path = "/var/www/html/uploads"
# 检查上传目录是否存在,如果不存在则创建
if not os.path.exists(upload_path):
os.makedirs(upload_path)
# 保存文件
try:
file_name = os.path.basename(file_item.filename)
with open(os.path.join(upload_path, file_name), "wb") as f:
f.write(file_item.file.read())
print "Content-Type: text/plain"
print # 确保在头部之后有一个空行
print "File {} uploaded successfully.".format(file_name)
except Exception as e:
print "Content-Type: text/plain"
print # 确保在头部之后有一个空行
print "Error: {}".format(str(e))
if __name__ == "__main__":
upload_file()
将脚本文件上传到 Apache 服务器的虚拟目录,并赋予执行权限。
遗留:大文件会失败
认证示例
以下是一个使用 Basic 认证的示例配置,其中只有 lisi 和wanwu可以访问 /var/www/html/uploads/ 目录下的资源。
用户和密码文件生成:
mkdir -p /usr/local/apache/
创建用户
htpasswd -cb /usr/local/apache/a_com.pass lisi 123456
htpasswd -b /usr/local/apache/a_com.pass wanwu 123456
修改 Apache 配置文件:
<Directory "/var/www/html/uploads">
Options Indexes FollowSymLinks
AllowOverride Authconfig
AuthType Basic
AuthName "Login Notice"
AuthUserFile /usr/local/apache/a_com.pass
Require user lisi wanwu
</Directory>
- ServerRoot:如果
AuthUserFile或AuthGroupFile使用相对路径,那么它们是相对于ServerRoot目录的。 htpasswd命令 :用来创建和管理用户认证文件。-c选项用于创建新文件,-b选项用于提供密码。