【问题标题】:Second Apache virtual host not loading第二个 Apache 虚拟主机未加载
【发布时间】:2021-12-30 17:42:00
【问题描述】:

我在 Ubuntu 20.04 服务器上安装了 Apache,它运行良好。然后我开始搞乱虚拟主机,因为我想添加一个子域。

我的目标是让两个独立的项目可以通过两个独立的 URL 访问:

  • 主要项目-example.com
  • 子项目 - subdomain.example.com

在我的文件服务器上,我有这些文件结构:

- /var/www/html
  - index.php
- /var/www/subdomain.example.com
  - index.php

/etc/apache2/sites-available/example.conf 我有以下内容:

<VirtualHost *:80>
    ServerAdmin email@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin email@example.com
    ServerName subdomain.example.com
    ServerAlias www.subdomain.example.com
    DocumentRoot /var/www/subdomain.example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

我在保存文件后运行了以下命令:

# Enable the new Apache configuration
sudo a2ensite example.conf

# Disable the default Apache configuration
sudo a2dissite 000-default.conf

# Activate the new Apache configuration
sudo systemctl reload apache2

# Restart Apache server
sudo systemctl restart apache2

完成后,我去访问http://example.com,它工作正常。但是当我访问 http://subdomain.example.com 时,我在 Chrome 中收到以下错误:

找不到 subdomain.example.com 的服务器 IP 地址。

我继续编辑/etc/hosts 并有以下内容:

127.0.0.1 localhost
127.0.0.1 example.com
127.0.0.1 subdomain.example.com

我不确定在修改 /etc/hosts 后是否需要做任何事情,但即使我保存了新的 hosts 文件也没有任何更改。

如何修复我的虚拟主机,以达到我在本问题开头提到的目标?

【问题讨论】:

  • 你能显示httpd -D DUMP_VHOSTS的输出吗
  • @Raxi 我收到命令'httpd' not found, did you mean: command 'http' from deb httpie (1.0.3-2) command 'xttpd' from deb xtide (2.13.2-1build2) Try: apt install &lt;deb name&gt;
  • 改用apache -D DUMP_VHOSTS(或者可能是apache2),我猜它在你的发行版上有一个不同的名字
  • 如果您的意思是 Chrome 无法解析 subdomain.example.com 的 IP 地址,尽管已将其添加到 /etc/hosts,可能是它在初始失败后没有获取新信息,或者可能有问题文件。可以用host subdomain.example.com等其他工具解析IP吗? (无论如何,这些都与 Apache 无关。)
  • @Raxi 我收到了:dokgu@example:/var/www$ apache2 -D DUMP_VHOSTS [Thu Dec 30 17:49:58.265327 2021] [core:warn] [pid 21637] AH00111: Config variable ${APACHE_RUN_DIR} is not defined apache2: Syntax error on line 80 of /etc/apache2/apache2.conf: DefaultRuntimeDir must be a valid directory, absolute or relative to ServerRoot

标签: php apache subdomain virtualhost


【解决方案1】:

我发现问题不在于 Apache 的配置方式。

在 Google Cloud Platform 仪表板的 Cloud DNS 上,我必须添加两个记录集:

  • A 用正确的 IP 地址记录 subdomain.example.com
  • CNAME记录www.subdomain.example.com

现在它可以工作了。

【讨论】:

    猜你喜欢
    • 2017-02-06
    • 2019-11-14
    • 2012-12-03
    • 2013-05-16
    • 2016-06-13
    • 2019-12-27
    • 2011-06-19
    • 2012-07-08
    • 2019-02-21
    相关资源
    最近更新 更多