Apache 文件服务器部署文档

文档版本:v1.0 适用系统:CentOS 7 / Rocky Linux 8 / Ubuntu 20.04+ Apache 版本:2.4+ 编写日期:2026-05-14


1. 概述

Apache HTTP Server(httpd)是目前最广泛使用的 Web 服务器之一,其内置的 mod_autoindex 模块可将指定目录以 HTTP/HTTPS 形式对外提供文件浏览与下载服务,适用于以下场景:

  • 内网软件包、ISO 镜像分发
  • 局域网共享文件存储
  • 构建产物归档与对外发布
  • 临时文件传输与共享

2. 环境准备

2.1 服务器信息规划

配置项示例值说明
服务器 IP192.168.1.100根据实际环境填写
服务端口80 / 443HTTP / HTTPS
文件根目录/data/fileserver存放共享文件的目录
访问路径/files/URL 访问路径前缀
运行用户apache / www-dataCentOS / Ubuntu 默认用户

2.2 关闭 SELinux(CentOS/Rocky)

# 临时关闭
setenforce 0

# 永久关闭(重启后生效)
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

⚠️ 注意:生产环境建议通过 SELinux 策略放行,而非直接关闭。 若需保留 SELinux,执行以下命令赋予目录正确上下文:

chcon -Rt httpd_sys_content_t /data/fileserver

3. 安装 Apache

3.1 CentOS 7 / Rocky Linux 8

# 安装 httpd
yum install -y httpd

# 设置开机自启并立即启动
systemctl enable httpd --now

3.2 Ubuntu / Debian

# 更新软件源
apt update

# 安装 apache2
apt install -y apache2

# 设置开机自启并立即启动
systemctl enable apache2 --now

3.3 验证安装

# 查看版本
httpd -v          # CentOS/Rocky
apache2 -v        # Ubuntu/Debian

# 示例输出:
# Server version: Apache/2.4.57 (CentOS)

# 查看运行状态
systemctl status httpd        # CentOS/Rocky
systemctl status apache2      # Ubuntu/Debian

4. 配置文件服务器

4.1 创建文件存储目录

# 创建目录
mkdir -p /data/fileserver

# CentOS/Rocky 设置属主
chown -R apache:apache /data/fileserver
chmod -R 755 /data/fileserver

# Ubuntu/Debian 设置属主
chown -R www-data:www-data /data/fileserver
chmod -R 755 /data/fileserver

4.2 上传测试文件

# 创建测试文件
echo "Hello, Apache FileServer!" > /data/fileserver/test.txt
mkdir -p /data/fileserver/packages
touch /data/fileserver/packages/app-v1.0.tar.gz

4.3 编写虚拟主机配置

CentOS/Rocky 新建配置文件:

vim /etc/httpd/conf.d/fileserver.conf

Ubuntu/Debian 新建配置文件:

vim /etc/apache2/sites-available/fileserver.conf

写入以下内容:

<VirtualHost *:80>
    ServerName  192.168.1.100

    # 字符集,避免中文文件名乱码
    AddDefaultCharset UTF-8

    # 访问日志
    CustomLog  /var/log/httpd/fileserver_access.log  combined
    ErrorLog   /var/log/httpd/fileserver_error.log

    # 文件服务根路径
    Alias /files/ /data/fileserver/

    <Directory /data/fileserver/>
        # 开启目录浏览
        Options Indexes FollowSymLinks

        # 关闭 .htaccess 覆盖(提升性能)
        AllowOverride None

        # 允许所有来源访问(后续可改为 IP 白名单)
        Require all granted

        # 目录列表样式设置
        IndexOptions FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=*
        IndexOrderDefault Descending Date
    </Directory>

    # 根路径重定向到文件目录
    RedirectMatch ^/$ /files/

</VirtualHost>

⚠️ Ubuntu 日志路径:将日志路径中的 /var/log/httpd/ 替换为 /var/log/apache2/

4.4 Ubuntu 启用站点配置

# 启用站点(Ubuntu 专属命令)
a2ensite fileserver.conf

# 启用 autoindex 模块(通常默认已启用)
a2enmod autoindex

# 重载配置
systemctl reload apache2

4.5 CentOS/Rocky 检查并重载配置

# 检查配置语法
httpd -t
# 输出:Syntax OK

# 重载配置(不中断服务)
systemctl reload httpd

5. 访问控制与安全加固

5.1 IP 白名单限制

仅允许内网网段访问:

<Directory /data/fileserver/>
    Options Indexes FollowSymLinks
    AllowOverride None

    # 仅允许内网访问
    Require ip 192.168.1.0/24
    Require ip 10.0.0.0/8
</Directory>

5.2 HTTP Basic 认证

Step 1:安装 htpasswd 工具

# CentOS/Rocky
yum install -y httpd-tools

# Ubuntu/Debian
apt install -y apache2-utils

Step 2:创建密码文件

# 创建用户 admin(首次用 -c 创建文件)
htpasswd -c /etc/httpd/.htpasswd admin

# 追加更多用户(去掉 -c 参数,否则会覆盖文件)
htpasswd /etc/httpd/.htpasswd user2

Step 3:在配置中启用认证

<Directory /data/fileserver/>
    Options Indexes FollowSymLinks
    AllowOverride None

    # 启用 Basic 认证
    AuthType Basic
    AuthName "File Server - Please Login"
    AuthUserFile /etc/httpd/.htpasswd
    Require valid-user
</Directory>

Step 4:重载配置

# CentOS/Rocky
systemctl reload httpd

# Ubuntu/Debian
systemctl reload apache2

5.3 隐藏服务器版本信息

在主配置文件中添加(CentOS:/etc/httpd/conf/httpd.conf,Ubuntu:/etc/apache2/apache2.conf):

# 隐藏版本号和操作系统信息
ServerTokens Prod
ServerSignature Off

重载后响应头中的 Server 字段仅显示 Apache,不暴露具体版本。

5.4 禁止特定文件类型访问(可选)

<Directory /data/fileserver/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted

    # 禁止访问 .sh .py 等脚本文件
    <FilesMatch "\.(sh|py|pl|rb)$">
        Require all denied
    </FilesMatch>
</Directory>

6. HTTPS 配置(可选)

6.1 生成自签名证书(测试环境)

# 创建证书目录
mkdir -p /etc/httpd/ssl

# 生成自签名证书(有效期 3650 天)
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \
  -keyout /etc/httpd/ssl/server.key \
  -out    /etc/httpd/ssl/server.crt \
  -subj "/C=CN/ST=Guangdong/L=Shenzhen/O=MyOrg/CN=192.168.1.100"

6.2 安装 SSL 模块

# CentOS/Rocky
yum install -y mod_ssl

# Ubuntu/Debian
a2enmod ssl

6.3 HTTPS 虚拟主机配置

<VirtualHost *:443>
    ServerName  192.168.1.100
    AddDefaultCharset UTF-8

    # SSL 证书配置
    SSLEngine on
    SSLCertificateFile    /etc/httpd/ssl/server.crt
    SSLCertificateKeyFile /etc/httpd/ssl/server.key

    # 推荐协议与加密套件
    SSLProtocol           all -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite        HIGH:!aNULL:!MD5
    SSLHonorCipherOrder   on

    CustomLog  /var/log/httpd/fileserver_ssl_access.log  combined
    ErrorLog   /var/log/httpd/fileserver_ssl_error.log

    Alias /files/ /data/fileserver/

    <Directory /data/fileserver/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
        IndexOptions FancyIndexing FoldersFirst NameWidth=*
    </Directory>
</VirtualHost>

# HTTP 自动跳转 HTTPS
<VirtualHost *:80>
    ServerName  192.168.1.100
    Redirect permanent / https://192.168.1.100/
</VirtualHost>

重载配置:

# CentOS/Rocky
systemctl reload httpd

# Ubuntu/Debian
systemctl reload apache2

7. 防火墙设置

7.1 firewalld(CentOS/Rocky)

# 开放 HTTP
firewall-cmd --permanent --add-service=http

# 开放 HTTPS(如启用)
firewall-cmd --permanent --add-service=https

# 重载规则
firewall-cmd --reload

# 验证
firewall-cmd --list-all

7.2 ufw(Ubuntu/Debian)

# 开放 HTTP
ufw allow 80/tcp

# 开放 HTTPS(如启用)
ufw allow 443/tcp

# 重载
ufw reload

# 验证
ufw status

8. 验证与测试

8.1 命令行验证

# 测试 HTTP 访问
curl -I http://192.168.1.100/files/

# 期望响应:HTTP/1.1 200 OK

# 带 Basic 认证访问
curl -u admin:password http://192.168.1.100/files/

# 下载指定文件
curl -O http://192.168.1.100/files/test.txt

# 测试 HTTPS(自签名证书需加 -k 跳过验证)
curl -k -I https://192.168.1.100/files/

8.2 浏览器访问

在浏览器中访问以下地址,应看到 Apache 样式的目录列表页:

http://192.168.1.100/files/

目录列表页面示例(FancyIndexing 效果):

Index of /files/

      Name              Last modified      Size  Description
[DIR] packages/         2026-05-14 09:00      -
[TXT] test.txt          2026-05-14 09:00     26

9. 常用管理命令

操作CentOS/RockyUbuntu/Debian
启动服务systemctl start httpdsystemctl start apache2
停止服务systemctl stop httpdsystemctl stop apache2
重启服务systemctl restart httpdsystemctl restart apache2
重载配置systemctl reload httpdsystemctl reload apache2
检查配置语法httpd -tapache2ctl -t
查看运行状态systemctl status httpdsystemctl status apache2
查看已加载模块httpd -Mapache2ctl -M
启用站点配置a2ensite <conf>
禁用站点配置a2dissite <conf>
启用模块a2enmod <module>
查看访问日志tail -f /var/log/httpd/fileserver_access.logtail -f /var/log/apache2/fileserver_access.log
查看错误日志tail -f /var/log/httpd/fileserver_error.logtail -f /var/log/apache2/fileserver_error.log

10. 常见问题排查

❌ 问题一:403 Forbidden

可能原因与解决方案:

# 1. 目录权限不足
chmod -R 755 /data/fileserver
chown -R apache:apache /data/fileserver    # CentOS
chown -R www-data:www-data /data/fileserver # Ubuntu

# 2. 配置中未加 Options Indexes
# 确保 Directory 块中包含:
Options Indexes FollowSymLinks

# 3. SELinux 阻止访问(CentOS)
setenforce 0
# 或赋予正确上下文:
chcon -Rt httpd_sys_content_t /data/fileserver

# 4. Require 指令配置错误
# 检查 <Directory> 块中是否有:
Require all granted

❌ 问题二:404 Not Found

# 1. Alias 路径末尾缺少斜杠
# 错误示例:Alias /files /data/fileserver
# 正确示例:Alias /files/ /data/fileserver/

# 2. Directory 块路径与 Alias 不对应
# Alias /files/ /data/fileserver/
# <Directory /data/fileserver/>  ← 必须与 Alias 目标路径一致

# 3. 目录不存在
ls -la /data/fileserver

❌ 问题三:中文文件名乱码

# 在 VirtualHost 或全局配置中添加
AddDefaultCharset UTF-8

❌ 问题四:端口被占用

# 查看端口占用情况
ss -tlnp | grep :80

# 修改 Apache 监听端口(如改为 8080)
# CentOS:/etc/httpd/conf/httpd.conf
# Ubuntu:/etc/apache2/ports.conf
Listen 8080

# 同时修改 VirtualHost
<VirtualHost *:8080>
...
</VirtualHost>

# 重载配置
systemctl reload httpd

❌ 问题五:autoindex 目录列表不显示

# 1. 确认 mod_autoindex 模块已加载
httpd -M | grep autoindex        # CentOS
apache2ctl -M | grep autoindex   # Ubuntu

# Ubuntu 若未加载,执行:
a2enmod autoindex
systemctl reload apache2

# 2. 确认配置中包含 Options Indexes
# 若父级 Directory 设置了 Options -Indexes,子目录会继承,需显式覆盖
<Directory /data/fileserver/>
    Options Indexes FollowSymLinks
</Directory>

❌ 问题六:修改配置后不生效

# 第一步:检查语法
httpd -t          # CentOS
apache2ctl -t     # Ubuntu

# 第二步:确认重载命令执行成功
systemctl reload httpd

# 第三步:确认是否有多个配置文件冲突
ls /etc/httpd/conf.d/          # CentOS
ls /etc/apache2/sites-enabled/ # Ubuntu

# 第四步:查看错误日志定位原因
tail -50 /var/log/httpd/error_log

附录:完整配置文件示例

# /etc/httpd/conf.d/fileserver.conf(CentOS/Rocky)
# /etc/apache2/sites-available/fileserver.conf(Ubuntu/Debian)

<VirtualHost *:80>
    ServerName     192.168.1.100
    AddDefaultCharset UTF-8

    CustomLog  /var/log/httpd/fileserver_access.log  combined
    ErrorLog   /var/log/httpd/fileserver_error.log

    # 隐藏版本信息
    ServerTokens Prod
    ServerSignature Off

    Alias /files/ /data/fileserver/

    <Directory /data/fileserver/>
        Options Indexes FollowSymLinks
        AllowOverride None

        # 目录列表样式
        IndexOptions FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=*
        IndexOrderDefault Descending Date

        # 访问控制(按需选择一种)
        Require all granted
        # Require ip 192.168.1.0/24    # IP 白名单(取消注释以启用)

        # Basic 认证(取消注释以启用)
        # AuthType Basic
        # AuthName "File Server"
        # AuthUserFile /etc/httpd/.htpasswd
        # Require valid-user

        # 禁止上传
        <LimitExcept GET HEAD>
            Require all denied
        </LimitExcept>
    </Directory>

    # 根路径重定向
    RedirectMatch ^/$ /files/

</VirtualHost>

📌 文档维护提示:本文档适用于基础文件服务器场景。如需支持大规模并发下载,建议结合反向代理缓存(如 Varnish)或对象存储(如 MinIO)进行架构扩展。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容