apache配置本地測試多網站域名與虛擬主機

          1、修改域名訪問方式:

          運行:C:\WINDOWS\system32\drivers\etc

          打開:hosts文件

          添加域名指向。

          2、修改httpd.conf.

          配置生效前提,必須修改跟目錄為:

          <Directory />
          Options FollowSymLinks
          AllowOverride None
          ### Order deny,allow
          ### Deny from all
          Order allow,deny
          Allow from all
          Satisfy all
          </Directory>

          否則會出現無權訪問問題。

          3、虛擬主機的配置
          (1)基于IP地址的虛擬主機配置
          Listen 80
          <VirtualHost 172.20.30.40>
          DocumentRoot /www/example1
          ServerName www.example1.com
          </VirtualHost>
          <VirtualHost 172.20.30.50>
          DocumentRoot /www/example2
          ServerName www.example2.org
          </VirtualHost>

          (2) 基于IP和多端口的虛擬主機配置
          Listen 172.20.30.40:80
          Listen 172.20.30.40:8080
          Listen 172.20.30.50:80
          Listen 172.20.30.50:8080

          <VirtualHost 172.20.30.40:80>
          DocumentRoot /www/example1-80
          ServerName www.example1.com
          </VirtualHost>

          <VirtualHost 172.20.30.40:8080>
          DocumentRoot /www/example1-8080
          ServerName www.example1.com
          </VirtualHost>

          <VirtualHost 172.20.30.50:80>
          DocumentRoot /www/example2-80
          ServerName www.example1.org
          </VirtualHost>

          <VirtualHost 172.20.30.50:8080>
          DocumentRoot /www/example2-8080
          ServerName www.example2.org
          </VirtualHost>

          (3)單個IP地址的服務器上基于域名的虛擬主機配置:
          # Ensure that Apache listens on port 80
          Listen 80

          # Listen for virtual host requests on all IP addresses
          NameVirtualHost *:80

          <VirtualHost *:80>
          DocumentRoot /www/example1
          ServerName www.example1.com
          ServerAlias example1.com. *.example1.com
          # Other directives here
          </VirtualHost>

          <VirtualHost *:80>
          DocumentRoot /www/example2
          ServerName www.example2.org
          # Other directives here
          </VirtualHost>

          (4)在多個IP地址的服務器上配置基于域名的虛擬主機:
          Listen 80

          # This is the "main" server running on 172.20.30.40
          ServerName server.domain.com
          DocumentRoot /www/mainserver

          # This is the other address
          NameVirtualHost 172.20.30.50

          <VirtualHost 172.20.30.50>
          DocumentRoot /www/example1
          ServerName www.example1.com
          # Other directives here …
          </VirtualHost>
          IXDBA.NET社區論壇

          <VirtualHost 172.20.30.50>
          DocumentRoot /www/example2
          ServerName www.example2.org
          # Other directives here …
          </VirtualHost>

          (5)在不同的端口上運行不同的站點(基于多端口的服務器上配置基于域名的虛擬主機):
          Listen 80
          Listen 8080

          NameVirtualHost 172.20.30.40:80
          NameVirtualHost 172.20.30.40:8080

          <VirtualHost 172.20.30.40:80>
          ServerName www.example1.com
          DocumentRoot /www/domain-80
          </VirtualHost>

          <VirtualHost 172.20.30.40:8080>
          ServerName www.example1.com
          DocumentRoot /www/domain-8080
          </VirtualHost>

          <VirtualHost 172.20.30.40:80>
          ServerName www.example2.org
          DocumentRoot /www/otherdomain-80
          </VirtualHost>

          <VirtualHost 172.20.30.40:8080>
          ServerName www.example2.org
          DocumentRoot /www/otherdomain-8080
          </VirtualHost>

          (6)基于域名和基于IP的混合虛擬主機的配置:
          Listen 80

          NameVirtualHost 172.20.30.40

          <VirtualHost 172.20.30.40>
          DocumentRoot /www/example1
          ServerName www.example1.com
          </VirtualHost>

          <VirtualHost 172.20.30.40>
          DocumentRoot /www/example2
          ServerName www.example2.org
          </VirtualHost>

          <VirtualHost 172.20.30.40>
          DocumentRoot /www/example3
          ServerName www.example3.net
          </VirtualHost>

          ==========================================================================

          簡單的說,打開httpd.conf 在最后加入如下內容:

          <VirtualHost 127.0.0.2:80>
              DocumentRoot d:/AppServ/www2
              ServerName 127.0.0.2:80
          </VirtualHost>


          <Directory "d:/AppServ/www2">
              Options Indexes FollowSymLinks Multiviews
              AllowOverride All
              Order Allow,Deny
              Allow from all
          </Directory>

          "d:/AppServ/www2" 為你的站點存放目錄:重啟apache2以后,你的虛擬主機就配置好了,以后就可以通過127.0.0.2,和127.0.0.3進入不同的站點了。

          下面為詳細說明分析:

          在我們安裝APACHE的時候一般默認的apache的配置是只有一個網站,這樣切換起來很不方便。其實這個問題很好解決,就是把本機的 apache配置成為虛擬服務器。但是,網上大多數教程的是教用 apache如何配置基于域名的虛擬主機的,而在本機調試網站的時候,一般都是用本地ip(127.0.0.1 或 localhost)直接訪問,沒有用到域名。所以得把apache配置成為基于ip地址的虛擬主機。

          首先,我們都知道,所有以127打頭的ip地址都應該指向本機,并不只有127.0.0.1,這點大家可以試試。
          這樣一來,也就是說本機有足夠多的ip地址供你來開設虛擬主機了。

          廢話少說,進入正式的配置工作,下面是apache的httpd.conf里相關配置部分( httpd.conf 位于 Apache2.2\conf ):
          1、Listen部分,必須直接指定端口,不指定ip地址,配置應寫為:
          Listen 80
          2、不用像基于域名的虛擬主機那樣寫“NameVirtualHost”。

          3、虛擬主機配置段:在httpd.conf 最后加上
          <VirtualHost 127.0.0.2:80>
              DocumentRoot d:/AppServ/www2
              ServerName 127.0.0.2:80
          </VirtualHost>

          <VirtualHost 127.0.0.3:80>
              DocumentRoot d:/AppServ/www3
              ServerName 127.0.0.3:80
          </VirtualHost>...

          4、然后相應的配置好各個目錄屬性,下面是一個目錄屬性的典型配置:
          <Directory "d:/AppServ/www2">
              Options Indexes FollowSymLinks Multiviews
              AllowOverride All
              Order Allow,Deny
              Allow from all
          </Directory>

          <Directory "d:/AppServ/www3">
              Options Indexes FollowSymLinks Multiviews
              AllowOverride All
              Order Allow,Deny
              Allow from all
          </Directory>

          重啟apache2以后,你的虛擬主機就配置好了,以后就可以通過127.0.0.1和127.0.0.2,127.0.0.3進入不同的站點了。

          posted on 2010-10-09 12:39 丁克設計 閱讀(3308) 評論(0)  編輯  收藏 所屬分類: Apache技術文檔

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          留言簿(6)

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 东城区| 通河县| 阳谷县| 清水县| 阿克苏市| 六枝特区| 溧阳市| 封开县| 三原县| 福建省| 河源市| 扶风县| 垣曲县| 桐庐县| 舒兰市| 宣武区| 城固县| 临泽县| 盐亭县| 洞头县| 安平县| 米脂县| 揭西县| 西平县| 浮山县| 庄浪县| 沈丘县| 峨山| 广丰县| 南开区| 修水县| 达日县| 桐庐县| 马尔康县| 广灵县| 五河县| 浠水县| 永州市| 湘潭县| 石渠县| 德格县|