【问题标题】:Apache 2.4 ignore httpd-vhosts.conf fileApache 2.4 忽略 httpd-vhosts.conf 文件
【发布时间】:2017-07-28 02:49:31
【问题描述】:

我一直按照这个步骤setup apache virtualhost (windows) 在 Windows 中创建一个虚拟主机,但我不知道出了什么问题,因为它不起作用。

我想要一个带有这个 url 的虚拟主机:http://local.shop

httpd-vhosts.conf

<VirtualHost *:80>    
DocumentRoot "C:/Apache24/htdocs/"
ServerName localhost
ServerAlias localhost
<Directory "C:/Apache24/htdocs/">
    AllowOverride All
</Directory>

<VirtualHost *:80>    
    DocumentRoot "C:/Apache24/htdocs/shop/"
    ServerName local.shop
    ServerAlias local.shop
    <Directory "C:/Apache24/htdocs/shop/">
        AllowOverride All
    </Directory>
    ErrorLog "logs/localhost.html-error.log"
    CustomLog "logs/localhost.html-access.log" common
</VirtualHost>

主机

    127.0.0.1   localhost
    127.0.0.1   local.shop

如果我尝试访问 http://localhost,则提供的页面是 C:/Apache24/htdocs/shop/ 上的页面,但如果我尝试访问 http://local.shop,则会出现下一个错误 ERR_NAME_NOT_RESOLVED

我做错了什么?

编辑 1:

我已经删除了 ServerAlias 指令,现在如果我尝试访问 http://localhost 所提供的页面是正确的页面,但如果我尝试访问 http://local.shop 仍然无法正常工作。我遇到了同样的错误ERR_NAME_NOT_RESOLVED

编辑 2: 我已经使用 Windows 中的“ping”命令来尝试访问每个主机。我从 localhost 得到响应,但没有从 local.shop 得到响应

编辑 3: 我在 httpd-vhosts.conf 中对 localhost 的定义进行了更改。我已将 DocumentRoot 更改为“C:/Apache24/htdocs/shop”

<VirtualHost *:80>    
    DocumentRoot "C:/Apache24/htdocs/shop"
    ServerName localhost
    ServerAlias localhost
    <Directory "C:/Apache24/htdocs/">
       AllowOverride All
    </Directory>
</VirtualHost>

我仍然从原始本地主机获取默认页面,而不是从商店目录获取默认页面。 Apache 似乎忽略了 httpd-vhosts.conf 文件。

【问题讨论】:

  • 不是问题,但请删除那些 ServerAlias 指令,那样它们没有任何意义。
  • 当您重新启动 http 服务器时,您的 http 服务器错误日志文件中的条目?
  • 对主机名local.shop 的命令行解析尝试会产生什么结果?
  • 我该怎么做?
  • 你怎么能做什么

标签: windows apache virtualhost apache2.4


【解决方案1】:

我在 2021 年 11 月 2 日星期二下午 2:37 安装 Apache 2.4 时遇到了同样的问题。

如果您在没有 WAMP 或 LAMP 的情况下安装 Apache,您需要做的事情:

  1. 如果您不想做太多配置,请按照许多教程的要求将下载的文件解压缩到 c:\Apache 2.4
  2. 如果您想使用httpd 命令变量,您需要将c:\Apache 2.4\bin 添加到您的环境变量路径中。
  3. 这是大多数人未能解决的问题 :: 如果您要创建虚拟主机,您需要在您最喜欢的文本编辑器或 IDE 中打开 c:\Apache 2.4\bin\conf\httpd.conf 并查找行或语句 #Include conf/extra/httpd-vhosts.com 和取消注释它,这意味着删除开头的#(磅)符号。在撰写本文时是第 510 行。它应该如下所示:
509 #Virtual hosts
510 Include conf/extra/httpd-vhosts.conf
  1. 您需要以管理员身份打开文本编辑器并编辑 c:\windows\system32\drivers\etc\hosts 文件并添加所有虚拟主机,这样您就明白了:
127.0.0.1    vhOne.com #your virtual host server name
127.0.0.1    vhTwo.com #your second virutal host server name

关闭任何终端并再次打开它后,您应该能够转到您的终端,而不必一直 cd 到 Apache 目录,只需键入:

> httpd -k install
> httpd -v
> httpd -k restart

您还应该能够从终端 ping 您的虚拟主机并期望得到所需的响应:

ping vhOne.com

您应该能够访问您的虚拟主机 URL。请记住,起初大多数浏览器不会将 127.0.0.1 URL 识别为网址而是搜索字符串,因此您必须在 URL 中输入 http://vhOne.comhttp://。在第一次访问之后,您可以通过键入不带 http:// 的服务器名称返回

【讨论】:

    【解决方案2】:

    Apache 可能正在监听(如果您在编辑 vhosts 文件后重新启动它),但没有任何指向它。您需要编辑您的主机文件(位于 C:\Windows\System32\drivers\etc\hosts 下)并将域“local.shop”指向 IP 地址 127.0.0.1,以便 Apache 可以从那里获取。您可以使用记事本编辑该文件,并以管理员权限打开。

    它应该看起来像这样:

    # Copyright (c) 1993-2009 Microsoft Corp.
    #
    # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
    #
    # This file contains the mappings of IP addresses to host names. Each
    # entry should be kept on an individual line. The IP address should
    # be placed in the first column followed by the corresponding host name.
    # The IP address and the host name should be separated by at least one
    # space.
    #
    # Additionally, comments (such as these) may be inserted on individual
    # lines or following the machine name denoted by a '#' symbol.
    #
    # For example:
    #
    #      102.54.94.97     rhino.acme.com          # source server
    #       38.25.63.10     x.acme.com              # x client host
    
    # localhost name resolution is handled within DNS itself.
    #   127.0.0.1       localhost
    #   ::1             localhost
    127.0.0.1   local.shop
    

    【讨论】:

    • 感谢您的评论,但我的 Windows 主机文件中仍然包含域 local.shop。
    • 仍然错误不在 Apache 端,因为实际错误是“ERR_NAME_NOT_RESOLVED”(即“我不知道该域是哪个服务器)。
    猜你喜欢
    • 2014-06-06
    • 2016-10-02
    • 2014-02-15
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 2016-12-07
    • 2018-06-27
    • 2015-07-29
    相关资源
    最近更新 更多