簡體   English   中英

Apache 2.4 忽略 httpd-vhosts.conf 文件

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

我一直在按照這個步驟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”命令嘗試訪問每個主機。 我收到來自本地主機的回復,但沒有收到來自 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 文件。

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

這是一個 4 年前的問題,顯然沒有得到回答。 即使有合理的答案,我在 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://的服務器名稱返回

希望這可以幫助遇到此問題的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM