【问题标题】:Apache2 virtual host missing Document RootApache2 虚拟主机缺少文档根目录
【发布时间】:2026-02-02 02:50:01
【问题描述】:

提前谢谢你。

我只是按照 digitalocean 的教程进行操作:https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts。 我完成了所有指令,但是当我开始测试配置并从我的停车域添加一条 A 记录时,比如说 example.com,我的 digitalocean vps 地址是 192.168.10.2。 在这个方案中,我添加了这 3 个具有 3 个不同 DocumentRoot 的虚拟主机:

  1. /var/www/domainone.example.com/index.html
  2. /var/www/domaintwo.example.com/index.html
  3. /var/www/domainthree.example.com/index.html

然后在我的停车域中添加 3 条不同的 A 记录,如下所示:

  1. 名称:domainone.example.com 地址:192.168.10.2
  2. 名称:domaintwo.example.com 地址:192.168.10.2
  3. 名称:domainthree.example.com 地址:192.168.10.2

当我执行时,它发生如下:

  1. URL domainone.example.com 转到 /var/www/domainone.example.com/index.html
  2. URL domaintwo.example.com 转到 /var/www/domaintwo.example.com/index.html
  3. 但是此 URL domainthree.example.com 转到 /var/www/domaintwo.example.com/index.html

我确定我在 /etc/apache2/sites-available/(for each domain).config 处写了正确的 documentRoot 并重新启动 apache2 服务

再次感谢您

编辑

  1. 这是用于 /etc/apache2/sites-available/domaintwo.example.com.conf

    <VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com
    
        ServerAdmin localhost@admin
        DocumentRoot /var/www/domaintwo.example.com/public
        ServerName domaintwo.example.com
        ServerAlias www.domaintwo.example.com
    
        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
        SSLEngine on
    

    SSLCertificateFile /etc/apache2/ssl/apache.crt SSLCertificateKeyFile /etc/apache2/ssl/apache.key

  2. 这是用于 /etc/apache2/sites-available/domainthree.example.com.conf

        <VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com
    
        ServerAdmin localhost@admin
        DocumentRoot /var/www/domainthree.example.com/public
        ServerName domainthree.example.com
        ServerAlias www.domainthree.example.com
    
        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key
        </VirtualHost>
    

【问题讨论】:

  • 我们可以提供您的Apache VirtualHosts 配置文件吗?
  • @D4V1D 我只是添加了virtualhost的配置文件,请看一下:)
  • 为什么没有domainone VirtualHost conf文件?
  • 您能否在sudo apache2ctl -S 的输出中验证是否一切都按预期启用?

标签: ubuntu apache2 virtualhost a-records


【解决方案1】:

这是 IMO 的 SSL 问题。域 2 按字母顺序排序高于 3(优先级)。 Apache 默认通过 HTTPS (SSL) 执行 domaintwo。所以域 3 的 VH 是错误的,或者没有为这两个协议启用。 我猜测或多或少。希望对您有所帮助。

【讨论】:

  • 两种协议的结果是否相同? (HTTPS/HTTP)