今天在集成環境下配虛擬主機,沒想到虛擬主機開啟后,localhost竟然無法訪問了,解決辦法是這樣的:
實例一,Apache 配置localhost虛擬主機步驟
1,用記事本打開apache目錄下httpd文件(如:D:\wamp\bin\apache\apache2.2.8\conf),找到如下模塊
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
去掉前面的#,這樣就開啟了httpd-vhosts虛擬主機文件。這時候重啟wamp環境,無法打開localhost,需要在httpd- vhosts.conf配置一下。
2,用記事本打開httpd-vhosts文件,配置好localhost虛擬主機,參照httpd- vhosts文件中實例,修改成如下:
ServerAdmin webmaster@dummy-host.localhost
DocumentRoot "D:\wamp\www"
ServerName localhost
ServerAlias localhost
ErrorLog "logs/dummy-host.localhost-error.log"
CustomLog "logs/dummy-host.localhost-access.log" common
修改配置如下:
DocumentRoot 修改為本地wamp環境下的www目錄(如:D:\wamp\www)
ServerName改為localhost
3,重啟Apache,發現localhost可以正常打開,配置localhost比較簡 單。
實例二,Apache配置 test.biuuu.com虛擬主機步驟
1,方法同上,復制配置代碼修改如下:
ServerAdmin test@biuuu.com
DocumentRoot E:\WebRoot\biuuu
ServerName test.biuuu.com
ErrorLog "logs/dummy-host2.localhost-error.log"
CustomLog "logs/dummy-host2.localhost-access.log" common
2,打開host文件(C:\WINDOWS\system32\drivers\etc\hosts),增加一行代碼
127.0.0.1 test.biuuu.com
3,在瀏覽器中打開test.biuuu.com,發現如下錯誤403 Forbidden錯誤
Forbidden
You don't have permission to access / on this server.
分析:這主要是目錄訪問權限沒有設置,需要設置對目錄的訪問權!
4,打開httpd文件,找到 如下語句
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
復制以上代碼,並進行目錄修改,把/替換為E:\WebRoot\biuuu,修改virtualHost代碼如下
在瀏覽器中測試發現還是打不開,提示如上403 Forbidden錯誤,修改其中的Deny from all為allow from all
5,重啟Apache,虛擬主機配置成 功!
注意事項
1,目錄路徑,如E:\WebRoot\biuuu
2,訪問權限,如上Deny from all修改為allow from all
3,host文件,配置虛擬域名host指向
4,httpd文件,打開Include conf/extra/httpd-vhosts.conf模塊
5,httpd-vhosts文件,配置虛擬主機
使用 Apache配置httpd-vhosts虛擬主機對於開發人員來說比較簡單,但卻非常重要,僅供參考!
PS: D:\wamp\alias 也可以這樣配置虛擬域名,和例二相同效果
ServerName blog.cc
ServerAlias blog.cc
DocumentRoot "D:\wamp\www\blog"
Options All FollowSymLinks IncludesNOEXEC Indexes
DirectoryIndex index.html index.htm default.htm index.php default.php index.cgi default.cgi index.shtml index.aspx default.aspx
AllowOverride All
Order Deny,Allow
Allow from all
////////////////////////////////////////////////////////////////////////////////////////////
環境要求
Apache(HTTPD)我的版本是2.4.3 Win32,你可能需要php引擎模塊來測試php網站。
配置文件
假設您已經知道在 httpd.conf 文件中為 main server 配置各種參數包括 DocumentRoot、ServerAdmin、ServerName等。那么配置虛擬主機就很方便了。為了配置文件方便管理,httpd.conf 中有一行指令用來包含外部的配置文件:
Include conf/extra/httpd-vhosts.conf這行默認是注釋掉的,主要是為虛擬主機的配置。所以在該文件(httpd-vhosts.conf)中添加虛擬主機的配置內容:
如果我在 e:/etc/www/ 下存放了兩個網站,一個是 ggicci.cn,一個是 chongwuxingqiu.com。分別存放在 ggicci.cn 目錄和 chongwuxingqiu.com 目錄下。我現在要為 ggicci.cn 配置成一個虛擬主機,占據端口 81,也就是說我訪問該網站需要輸入 localhost:81 (本文內容只與本地測試相關,不涉及遠程服務器)。chongwuxingqiu.com 同理占據端口 82。我的配置內容如下:
Listen 81 # 監聽81端口 <VirtualHost *:81> ServerAdmin ggicci@163.com DocumentRoot "e:/etc/www/ggicci.cn" # 網站根目錄 DirectoryIndex index.html index.php # 主頁索引,因為是php網站,所以添加一個.php的 # ServerName ErrorLog "logs/ggicci.cn-error.log" # 錯誤日志 CustomLog "logs/ggicci.cn-access.log" common # 訪問日志 </VirtualHost> Listen 82 <VirtualHost *:82> ServerAdmin ggicci@163.com DocumentRoot "e:/etc/www/chongwuxingqiu.com" DirectoryIndex index.html index.jsp # jsp網站,所以添加一個.jsp的 # ServerName ErrorLog "logs/chongwuxingqiu.com-error.log" CustomLog "logs/chongwuxingqiu.com-access.log" common JkMount /*.jsp chongwuxingqiu # 這個是 mod_jk 的指令,與 tomcat 相關 </VirtualHost>
測試結果
#e:/etc/www/ggicci.cn/index.php <?php echo "ggicci.cn"; ?>

//e:/etc/www/pet.com/index.jsp <!DOCTYPE html> <%@ page contentType="text/html; charset=utf-8"%> <html> <head> <title>寵物星球</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <% out.println("寵物星球"); %> </body> </html>
