如何配置apache虛擬主機

如何配置apache虛擬主機

實驗目標:在apache實現基於域名的虛擬主機 

實驗用的XAMPP版本爲1.7.7,內含apache版本爲2.2.21 

 

實驗前準備:

1. 爲了測試不同的域名,在Windows/System32/drivers/etc/下覓得hosts文件,在其中添加實驗用的域名若干,如 -

127.0.0.1   test1.net

127.0.0.1   test2.net

如此,則在瀏覽器中輸入該倆域名時,Windows將其解析爲127.0.0.1本地地址。即,在瀏覽器中訪問localhost, test1.net, test2.net均可訪問XAMPP的歡迎頁。

 

2. 在apache目錄下建立目錄,以放置您不同的網站。爲保護XAMPP原有的htdocs中的歡迎頁內容,實驗另外建立了與htdocs平級的htdocs1目錄,在其下建立了test1.net, test2.net兩個子目錄用以放置實驗用的網站。如下 -

apache/htdocs1/test1.net - 放置test1.net網站內容

apache/htdocs1/test2.net - 放置test2.net網站內容

 在這兩個目錄中各新建hello world一網頁 index.html,內容 -


<HTML>
<HEAD></HEAD>
<BODY>
<H1>hello~, 這是[對應的網站名,用以區別].net</H1></BODY>
</HTML>  

 

實驗步驟:

1. 找到apache/conf/httpd.conf, 將其中的

ServerAdmin

ServerName

DocumentRoot

註釋掉。

 

2. 在httpd.conf中,找到行

 Include "conf/extra/httpd-vhosts.conf"

如被註釋則解注。該文件記載了虛擬主機的參數。[以前虛擬主機參數是直接填寫在httpd.conf中的,爲了更好地組織文件,將其分離出去,類似於某些編程語言一樣。因此httpd.conf中include它,即相當於把它的內容填在了httpd.conf中。]

 

3. 這個httpd-vhosts.conf文件格式基本如下 - 

複製代碼
#blah-blah
NameVirtualHost *:80

#blah-blah
#blah-blah
<VirtualHost *:80>
    ServerAdmin XXXXXXXX
    DocumentRoot "XXXXXXXX"
    ServerName XXXXXXX
    ServerAlias XXXXXX
    ErrorLog "logs/XXXXXX-error.log"
    CustomLog "logs/XXXXXXX-error.log" combined   
</VirtualHost> 
複製代碼

需要修改的,就是<VirtualHost>中的參數了。這個可以參見apache官方文檔。根據實驗域名,可以增加兩個<VirtualHost>:

 

複製代碼
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "C:/xampp/htdocs1/test1.net"
    ServerName test1.net
    ServerAlias www.test1.net
    ErrorLog "logs/test1-error.log"
    CustomLog "logs/test1-access.log" combined
    
    <Directory "C:/xampp/htdocs1/test1.net">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "C:/xampp/htdocs1/test2.net"
    ServerName test2.net
    ServerAlias www.test2.net
    ErrorLog "logs/test1-error.log"
    CustomLog "logs/test1-access.log" combined
    
    <Directory "C:/xampp/htdocs1/test2.net">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>
複製代碼

注意,如果不在各VirtualHost中定義Directory的可訪問性,你將遇到的是Access Forbidden!就連原來的localhost也是。

 

4. 由於之前註釋掉了httpd.conf中的ServerName, DocumentRoot等,爲了仍然能以localhost訪問原XAMPP歡迎頁,就在增加一個VirtualHost,如下 - 

複製代碼
<VirtualHost *:80>
    ServerAdmin adm@localhost
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
    
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" combined
    
    <Directory "C:/xampp/htdocs">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>
複製代碼

 爲了避免出錯,把它放置在第一個Virtualhost位置。

 

至此,apache基於域名的虛擬主機配置完成。可以通過http://localhost訪問XAMPP歡迎頁,通過http://test1.net和http://test2.net訪問各自的主頁。 


#
# Virtual Hosts
#


# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.


#
# Use name-based virtual hosting.
#
NameVirtualHost *:80


#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#


<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "E:/skydao/apache2/htdocs"
    ServerName localhost
    ServerAlias www.skydao.com
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" combined
    
    <Directory "E:/skydao/apache2/htdocs">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>


<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "E:/skydao/apache2/htdocs/project1"
    ServerName project1.com
    ServerAlias www.project1.com
    ErrorLog "logs/project1-error.log"
    CustomLog "logs/project1-access.log" combined
    
    <Directory "E:/skydao/apache2/htdocs/project1">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>


<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "E:/skydao/apache2/htdocs/zendTest/public"
    ServerName zendTest.com
    ServerAlias www.zendTest.com
    DirectoryIndex index.php
    <Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
    </Directory>    
</VirtualHost>


<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "E:/skydao/apache2/htdocs/testRewrite"
    ServerName testRewrite.com
    ServerAlias www.testRewrite.com
    # DirectoryIndex index.php
    <Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
    </Directory>    
</VirtualHost>


<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "E:/skydao/apache2/htdocs/test"
    ServerName test.com
    ServerAlias www.test.com
    ErrorLog "logs/zendTest-error.log"
    CustomLog "logs/zendTest-access.log" combined
    
    <Directory "E:/skydao/apache2/htdocs/test">
    order allow,deny
    allow from all
    </Directory>    
</VirtualHost>

發佈了51 篇原創文章 · 獲贊 14 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章