简体   繁体   English

Apache 2.4 忽略 httpd-vhosts.conf 文件

[英]Apache 2.4 ignore httpd-vhosts.conf file

I have been following this steps setup apache virtualhost (windows) to create a Virtual Host in Windows but I don't know what is wrong because it doesn't work.我一直在按照这个步骤setup apache virtualhost (windows)在 Windows 中创建一个虚拟主机,但我不知道出了什么问题,因为它不起作用。

I want a virtual host with this url: http://local.shop .我想要一个带有这个 url 的虚拟主机: http : //local.shop

httpd-vhosts.conf 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>

hosts主机

    127.0.0.1   localhost
    127.0.0.1   local.shop

If I try to access to http://localhost the page served is the page which is on C:/Apache24/htdocs/shop/ but if I try to access to http://local.shop I've got the next error ERR_NAME_NOT_RESOLVED如果我尝试访问http://localhost,则服务的页面是位于 C:/Apache24/htdocs/shop/ 上的页面,但如果我尝试访问http://local.shop,则会出现下一个错误ERR_NAME_NOT_RESOLVED

What am I doing wrong?我究竟做错了什么?

Edit 1:编辑1:

I have removed ServerAlias directives and now If I try to access to http://localhost the page served is the right page, but if I try to acces to http://local.shop still doesn't work.我已经删除了 ServerAlias 指令,现在如果我尝试访问http://localhost 提供的页面是正确的页面,但是如果我尝试访问http://local.shop仍然不起作用。 I've got the same error ERR_NAME_NOT_RESOLVED我有同样的错误ERR_NAME_NOT_RESOLVED

Edit 2: I have used "ping" command from windows to try to reach to each host.编辑 2:我使用 Windows 中的“ping”命令尝试访问每个主机。 And I get a response from localhost but not from local.shop我收到来自本地主机的回复,但没有收到来自 local.shop 的回复

在此处输入图片说明 在此处输入图片说明

Edit 3: I have made a change in my definition of localhost in httpd-vhosts.conf.编辑 3:我在 httpd-vhosts.conf 中对 localhost 的定义进行了更改。 I have changed DocumentRoot to "C:/Apache24/htdocs/shop"我已将 DocumentRoot 更改为“C:/Apache24/htdocs/shop”

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

Instead of getting the default page from shop directory, I still getting the default page from original localhost.我没有从商店目录中获取默认页面,而是从原始本地主机获取默认页面。 It seems like Apache ignore httpd-vhosts.conf file. Apache 似乎忽略了 httpd-vhosts.conf 文件。

Apache maybe listening (in case you restarted it after editing vhosts file), but nothing points to it. Apache可能正在监听(如果您在编辑vhosts文件后重新启动它),但没有任何指向它。 You need to edit your hosts file (located under C:\\Windows\\System32\\drivers\\etc\\hosts) and point domain "local.shop" to IP address 127.0.0.1 so Apache can pickup from there. 您需要编辑主机文件(位于C:\\ Windows \\ System32 \\ drivers \\ etc \\ hosts下)并将域“local.shop”指向IP地址127.0.0.1,以便Apache可以从那里进行拾取。 You may edit that file with Notepad, opened with admin rights. 您可以使用记事本编辑该文件,并使用管理员权限打开。

It should looks something like this: 它应该看起来像这样:

# 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

This is a 4 years old question, which apparently was not answered.这是一个 4 年前的问题,显然没有得到回答。 And even though there are legitimate answers, I encountered this same issue installing Apache 2.4 on Nov. Tuesday 2nd, 2021 at 2:37 pm.即使有合理的答案,我在 2021 年 11 月 2 日星期二下午 2:37 安装 Apache 2.4 时也遇到了同样的问题。 Just for the record.只是为了记录。

Things you need to do if you are installing Apache without WAMP or LAMP:如果您在没有 WAMP 或 LAMP 的情况下安装 Apache,您需要做的事情:

  1. If you do not want to do much configurations, unzip your downloaded files on c:\\Apache 2.4 as many tutorials asked you to do.如果您不想进行太多配置,请按照许多教程要求将下载的文件解压缩到c:\\Apache 2.4
  2. If you want to use httpd command variable you need to add c:\\Apache 2.4\\bin into your Environment Variables path.如果您想使用httpd命令变量,您需要将c:\\Apache 2.4\\bin到您的环境变量路径中。
  3. THIS IS WHAT MOST PEOPLE FAILED TO ADDRESS :: If you are going to create Virtual Hosts you need to open c:\\Apache 2.4\\bin\\conf\\httpd.conf in your favorite text editor or IDE and look for the line or statement #Include conf/extra/httpd-vhosts.com and uncomment it, which means removing the # (pound) sign at the beginning.这是大多数人无法解决的问题:: 如果您要创建虚拟主机,您需要在您喜欢的文本编辑器或 IDE 中打开c:\\Apache 2.4\\bin\\conf\\httpd.conf并查找行或语句#Include conf/extra/httpd-vhosts.com并取消注释,这意味着删除开头的 #(井号)符号。 At the time of this writing is line 510. It should look as follow:在撰写本文时是第 510 行。它应该如下所示:
509 #Virtual hosts
510 Include conf/extra/httpd-vhosts.conf
  1. You need to open you text editor as admin and edit c:\\windows\\system32\\drivers\\etc\\hosts file and add all your virtual hosts as so, you get the idea:您需要以管理员身份打开文本编辑器并编辑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

After doing that close any terminal and open that again, and you should be able to go to your terminal, without having to cd to the Apache directory all the time, just by typing:这样做之后,关闭任何终端并再次打开它,您应该能够进入终端,而不必一直cd到 Apache 目录,只需键入:

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

You should also be able to ping your virtual host from the terminal and expect the response desired:您还应该能够从终端 ping 您的虚拟主机并期望得到所需的响应:

ping vhOne.com

And you should be able to visit your virtual host URL.您应该能够访问您的虚拟主机 URL。 Keep in mind that at first most browsers won't recognized 127.0.0.1 URL to be web addresses but search string so you have to type http://vhOne.com with the http:// in the URL.请记住,一开始大多数浏览器不会将 127.0.0.1 URL 识别为网址而是搜索字符串,因此您必须在 URL 中键入http://vhOne.comhttp:// After that first visit you can go back by typing the server name without the http://在第一次访问之后,您可以通过键入不带http://的服务器名称返回

Hope this helps people having this issue.希望这可以帮助遇到此问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM