利用 Apache 架設網站伺服器服務 這一篇文章裡我們已經建立好 Apache 網站伺服器,它預設安裝在 C:\AppServ (以後稱此目錄為 「APACHE_ROOT」 ),預設網址的主目錄是 「APACHE_HOME\www」 ,這是在 Apache 伺服器的設定檔( 「APACHE_ROOT\Apache2.2\conf\httpd.conf」 )中定義的。

APACHE_ROOT\Apache2.2\conf\httpd.conf 」檔案的內容: 

Apache 伺服器的設定檔 httpd.conf

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "C:/AppServ/www"

  預設一台主機只有一個獨立的網站,若您想要在同一台主機能夠有一個以上的獨立網站,可以利用 Apache 伺服器中的 Virtual Host 的設定,從下面的示意圖可以瞭解,這些不同的獨立網站都架設在同一個IP的主機上。

virtualHost.png  

要有這樣的效果,可以藉由下面的步驟來達成:

1. 申請個人網域後(mistech.localhost.com),在原有主機上(127.0.0.1)加設虛擬主機為個人網站(http://mistech.localhost.com/)。這裡的 http://mistech.localhost.com/ 是以本站為例,您應該改為您個人專屬的網域。

2. 在 DNS 伺服器的設定
 

dns                   IN   A      127.0.0.1  ;這個IP的名稱是dns,是本尊
www                 IN   CNAME    dns    ;這是第一個虛擬網站 mistech.localhost.com   --分身
forum                IN   CNAME    dns   ;這是第二個虛擬網站 forum.mistech.localhost.com --分身
phpMyAdmin     IN   CNAME    dns ;這是第三個虛擬網站 phpmyadmin.mistech.localhost.com --分身

若您沒有 DNS 伺服器,也可以直接修改 C:\Windows\System32\drivers\etc\host 檔案也可以,只是這樣的設定只限這台主機適用。

Windows XP/Vista/7 的 hots 檔案
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.

127.0.0.1  www.mistech.localhost.com forum.mistech.localhost.com mistech.localhost.com

 

 3. 在 APACHE_ROOT\Apache2.2\conf\httpd.conf 的內容

# Real-time info on requests and configuration
#Include conf/extra/httpd-info.conf

# Virtual hosts
Include conf/extra/httpd-vhosts.conf        ; 將前面的 # 刪除,並修改 APACHE_ROOT\Apache2.2\conf\extra/httpd-vhosts.conf 這個檔案

# Local access to the Apache HTTP Server Manual
#Include conf/extra/httpd-manual.conf

 4. 在 APACHE_ROOT\Apache2.2\conf\extra\httpd-vhosts.conf 的內容

#
# 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.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
# Use name-based virtual hosting.
#
NameVirtualHost 127.0.0.1:80

# 這是第一個虛擬網站 www.mistech.localhost.com   --分身
<VirtualHost *:80>
    ServerAdmin webamin@mistech.localhost.com
    DocumentRoot "C:/AppServ/www/mistech"
    ServerName www.mistech.localhost.com
</VirtualHost>

#這是第二個虛擬網站 forum.mistech.localhost.com --分身
<VirtualHost *:80>
    ServerAdmin webamin@mistech.localhost.com
    DocumentRoot "C:/AppServ/www/forum"
    ServerName forum.mistech.localhost.com
</VirtualHost>

#這是第三個虛擬網站 phpmyadmin.workplace.netau.net --分身
<VirtualHost *:80>
    ServerAdmin webamin@mistech.localhost.com
    DocumentRoot "C:/AppServ/www/phpMyAdmin"
    ServerName phpmyadmin.mistech.localhost.com
</VirtualHost>

 重新啟動 Apache 伺服器,就可以了,測試一下吧。

arrow
arrow
    文章標籤
    apache VirtualHost 虛擬網站
    全站熱搜

    MIS 發表在 痞客邦 留言(0) 人氣()