You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache2访问子域名时指向全局DocumentRoot而非对应站点目录问题求助

遇到的问题

  • 尝试配置Apache2将子域名readerlol.chaimstanton.co.uk映射到目录/var/www/html/readerlol.chaimstanton.co.uk,但访问该地址时仅指向全局网站根目录,页面展示根目录下的文件列表。

预期实现效果

  • 用户访问readerlol.chaimstanton.co.uk时,应加载/var/www/html/readerlol.chaimstanton.co.uk路径下的index.html文件。

运行环境

  • 使用Digital Ocean的Droplet服务,搭载默认LAMP镜像,运行Ubuntu系统
  • 通过SSH连接服务器命令行操作

已尝试的排查方案

  • 重启apache2服务
  • 重启服务器
  • 核对apache2.conf中文件名与实际文件是否一致
  • 将虚拟主机的站点根目录修改为DocumentRoot /var/www/html/readerlol.chaimstanton.co.uk/index.html
  • 尝试关闭SSL功能

日志与配置信息

  • apachectl configtest返回结果为Syntax OK

备注:我认为站长板块不适合发布该问题,因此发布在此处。


解决方案

该问题由Apache虚拟主机匹配优先级异常、配置不规范导致,按以下步骤操作即可修复:

  1. 调整配置存放路径
    不要将虚拟主机配置直接写在apache2.conf主配置文件中,Ubuntu系统下Apache站点配置统一存放在/etc/apache2/sites-available/目录,先新建对应子域名的独立配置文件:
nano /etc/apache2/sites-available/readerlol.chaimstanton.co.uk.conf
  1. 写入正确配置内容
<VirtualHost *:80>
    ServerName readerlol.chaimstanton.co.uk
    # 注意DocumentRoot只能填写目录路径,不能指向具体index.html文件,你之前的修改属于错误操作
    DocumentRoot /var/www/html/readerlol.chaimstanton.co.uk
    <Directory /var/www/html/readerlol.chaimstanton.co.uk>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
        # 指定默认索引文件优先级
        DirectoryIndex index.html index.php
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/readerlol.error.log
    CustomLog ${APACHE_LOG_DIR}/readerlol.access.log combined
</VirtualHost>
  1. 启用配置、解决冲突
# 启用当前子域名站点配置
a2ensite readerlol.chaimstanton.co.uk.conf
# 可选:如果默认站点抢占匹配,可禁用默认站点配置
a2dissite 000-default.conf
  1. 重载配置生效
systemctl reload apache2

操作完成后再次访问子域名即可正常加载对应目录下的index.html文件。


内容的提问来源于stack exchange,提问作者Chaim Stanton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 16:06:10