Debian Apache2 实战:自定义站点目录、运行用户与 Basic Auth 认证

Debian Apache2 实战:自定义站点目录、运行用户与 Basic Auth 认证

1. 服务介绍

Apache HTTP Server 是常用 Web 服务软件,可发布静态网页、配置域名虚拟主机、提供 HTTPS、反向代理,并与 PHP 等后端程序协作。本实验在 Debian 上部署 www.sdskills.com,把站点目录改为 /data/share/htdocs/skills,调整 Apache 工作进程用户,并为 staff.html 增加 HTTP Basic Auth 认证。

原实验文字提到"账号存储在 LDAP",但现有截图实际使用 htpasswd 本地密码文件,不是 LDAP 认证。本文按截图实现本地 Basic Auth;若必须连接 LDAP,应改用 mod_authnz_ldap,不能把密码文件命名为 ldap 后当作 LDAP。

2. 准备运行环境

• 操作系统:Debian 10/11 或同类 Debian 系统。

• 操作账号:root 或具备 sudo 权限的管理员。

• 站点域名:www.sdskills.com

• 站点目录:/data/share/htdocs/skills

• Apache 工作进程用户:webuser,组为 www-data

• 认证账号:zsuserlsus

确认系统、地址和端口占用:

bash 复制代码
cat /etc/os-release
ip -br addr
getent hosts www.sdskills.com
ss -lntp | grep -E ':80|:443'

实验环境没有 DNS 时,可在客户端 /etc/hosts 中临时添加服务器地址与域名映射。

3. 相关知识与注意事项

3.1 Debian Apache2 配置结构

• 主配置:/etc/apache2/apache2.conf

• 运行环境变量:/etc/apache2/envvars

• 可用站点:/etc/apache2/sites-available/

• 已启用站点:/etc/apache2/sites-enabled/

• 模块管理:a2enmoda2dismod

• 站点管理:a2ensitea2dissite

• 日志:/var/log/apache2/access.log/var/log/apache2/error.log

不要长期直接修改 sites-enabled 中的符号链接目标不明文件。推荐在 sites-available 新建独立虚拟主机,再用 a2ensite 启用。

3.2 DocumentRoot 与 Directory 权限

DocumentRoot 决定 URL 根目录对应的文件路径;<Directory> 决定 Apache 是否允许访问该目录、是否允许目录索引、符号链接和 .htaccess 覆盖。仅修改 DocumentRoot 而未授权目录,常导致 403 Forbidden

3.3 Basic Auth 与 LDAP 的区别

• Basic Auth + AuthUserFile:账号密码保存在 htpasswd 文件,部署简单。

• LDAP 认证:账号位于目录服务器,需要 libapache2-mod-ldap-userdirmod_authnz_ldap 相关模块、LDAP URI、Base DN 和查询规则。

• Basic Auth 只进行 Base64 编码,不加密传输;生产环境必须配合 HTTPS。

3.4 修改运行用户的影响

Debian Apache 工作进程默认使用 www-data。改为 webuser 会影响所有虚拟主机和模块对文件、日志、套接字的访问权限。生产环境若只需隔离单个站点,应评估 mpm-itk、容器或后端进程池,而不是全局修改运行用户。

4. 实验步骤

4.1 安装 Apache2 和认证工具

bash 复制代码
apt update
apt install -y apache2 apache2-utils
systemctl enable --now apache2

检查版本:

bash 复制代码
apache2ctl -v
systemctl status apache2 --no-pager

4.2 创建站点目录和页面

bash 复制代码
mkdir -p /data/share/htdocs/skills
cat > /data/share/htdocs/skills/index.html <<'EOF'
This is the front page of sdskills's website.
EOF
cat > /data/share/htdocs/skills/staff.html <<'EOF'
Staff Information
EOF

4.3 创建 Apache 运行用户并设置目录权限

bash 复制代码
id webuser >/dev/null 2>&1 || useradd --system --no-create-home --shell /usr/sbin/nologin --gid www-data webuser
chown -R webuser:www-data /data/share/htdocs/skills
find /data/share/htdocs/skills -type d -exec chmod 0750 {} \;
find /data/share/htdocs/skills -type f -exec chmod 0640 {} \;

编辑 /etc/apache2/envvars

bash 复制代码
cp -a /etc/apache2/envvars /etc/apache2/envvars.bak
vi /etc/apache2/envvars

设置:

bash 复制代码
export APACHE_RUN_USER=webuser
export APACHE_RUN_GROUP=www-data

4.4 创建虚拟主机

bash 复制代码
vi /etc/apache2/sites-available/sdskills.conf

写入:

apache 复制代码
<VirtualHost *:80>
    ServerName www.sdskills.com
    DocumentRoot /data/share/htdocs/skills

    <Directory /data/share/htdocs/skills>
        Options FollowSymLinks
        AllowOverride None
        Require all granted

        <Files "staff.html">
            AuthType Basic
            AuthName "Staff Area"
            AuthUserFile /etc/apache2/staff.htpasswd
            Require user zsuser lsus
        </Files>
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/sdskills-error.log
    CustomLog ${APACHE_LOG_DIR}/sdskills-access.log combined
</VirtualHost>

旧实验直接修改默认站点的 DocumentRoot

本文使用独立虚拟主机,便于回退且不会混入全局配置。

4.5 创建 Basic Auth 用户

首次创建密码文件时使用 -c

bash 复制代码
htpasswd -c /etc/apache2/staff.htpasswd zsuser
htpasswd /etc/apache2/staff.htpasswd lsus
chown root:www-data /etc/apache2/staff.htpasswd
chmod 0640 /etc/apache2/staff.htpasswd

只有第一次使用 -c。为第二个用户再次使用 -c 会覆盖原密码文件。

旧实验配置使用 AuthUserFile 保护页面,但把文件名写成 /etc/apache2/ldap

该配置仍是本地密码文件认证,不是 LDAP。

4.6 启用站点并加载配置

bash 复制代码
a2dissite 000-default.conf
a2ensite sdskills.conf
apache2ctl configtest
systemctl restart apache2

修改前后都应执行语法检查。只有返回 Syntax OK 才重启服务。

确认进程用户:

bash 复制代码
ps -eo user,group,pid,cmd | grep '[a]pache2'

主进程通常仍为 root,工作进程应为 webuser

5. 验证结果

5.1 验证虚拟主机和首页

bash 复制代码
apache2ctl configtest
apache2ctl -S
systemctl status apache2 --no-pager
ss -lntp | grep ':80'
curl -i -H 'Host: www.sdskills.com' http://127.0.0.1/

响应应为 200 OK,正文包含:

text 复制代码
This is the front page of sdskills's website.

5.2 验证 staff.html 认证

未携带账号时应返回 401 Unauthorized

bash 复制代码
curl -I -H 'Host: www.sdskills.com' http://127.0.0.1/staff.html

使用正确账号时应返回 200 OK

bash 复制代码
curl -i -u zsuser -H 'Host: www.sdskills.com' http://127.0.0.1/staff.html
curl -i -u lsus -H 'Host: www.sdskills.com' http://127.0.0.1/staff.html

浏览器访问时应弹出账号密码窗口。

5.3 查看日志

bash 复制代码
tail -n 50 /var/log/apache2/sdskills-access.log
tail -n 50 /var/log/apache2/sdskills-error.log
journalctl -u apache2 -n 50 --no-pager

首页返回 200、未认证访问返回 401、正确账号返回 200、工作进程显示 webuser,说明配置生效。

6. 常见问题与回退

6.1 返回 403

bash 复制代码
namei -l /data/share/htdocs/skills/index.html
sudo -u webuser test -r /data/share/htdocs/skills/index.html && echo readable
tail -n 50 /var/log/apache2/sdskills-error.log

检查父目录执行权限、文件读取权限和 <Directory>Require all granted

6.2 认证一直失败

bash 复制代码
apache2ctl configtest
ls -l /etc/apache2/staff.htpasswd
htpasswd -v /etc/apache2/staff.htpasswd zsuser

确认 AuthUserFile 路径一致、Apache 工作进程组可读密码文件,并检查用户名是否写入 Require user

6.3 域名进入错误站点

bash 复制代码
apache2ctl -S
getent hosts www.sdskills.com
curl -I -H 'Host: www.sdskills.com' http://127.0.0.1/

检查 ServerName、DNS 或 hosts 映射以及默认虚拟主机顺序。

6.4 回退配置

bash 复制代码
a2dissite sdskills.conf
a2ensite 000-default.conf
cp -a /etc/apache2/envvars.bak /etc/apache2/envvars
apache2ctl configtest
systemctl restart apache2

删除站点目录和密码文件前先保留配置与日志,便于审计和复盘。

相关推荐
AKAMAI4 小时前
你的源服务器可能是你做出的最昂贵决定
运维·人工智能·云计算
Dxy12393102164 小时前
Linux 编译安装 Python 3.12.10(多版本共存,不破坏系统Python)
linux·运维·python
技术深耕者5 小时前
AI Agent从演示到生产:企业进入“自动化层”时代
运维·人工智能·自动化
xz驱动分享6 小时前
Linux DMA 子系统学习笔记
linux·dma·rk3588·嵌入式软件
w67820077 小时前
Prisma不能优雅的支持DTO,试试Vona ORM吧
linux·运维·ubuntu
APItesterCris7 小时前
Open Claw 实战教程:5 分钟搭建京东商品自动化监控与数据分析系统
大数据·运维·数据库·数据仓库·自动化
Darkwanderor7 小时前
Linux系统编程实战项目:模拟实现shell
linux·c++
杨某不才9 小时前
如何能让Linux服务器对shell 终端 + sftp 文件传输长期保活
linux·运维·服务器
vance049 小时前
免费Cloudflare隧道隐藏公网IP
linux·tcp/ip·github
三言老师10 小时前
文本工具组合统计服务器日志数据
linux·运维·服务器