1. 程式人生 > >apache虛擬主機配置的三種方式

apache虛擬主機配置的三種方式

Apache 配置虛擬主機三種方式

記事本開啟httpd.conf檔案 ,該檔案在apache的目錄下,如: D:\AppServ\Apache2.2\conf,修改如下兩處:

LoadModule vhost_alias_module modules/mod_vhost_alias.so //去掉前面的#,意思是啟用apache的虛擬主機功能,第203行

Include conf/extra/httpd-vhosts.conf //去掉#的意思是從httpd-vhosts.conf這個檔案匯入虛擬主機配置

配置虛擬主機後 不能用localhost 訪問 
只需要把httpd.conf檔案的ServerName localhost:80 那行註釋掉 就可以了

一、基於IP

  1. 假設伺服器有個IP地址為192.168.1.10,使用ifconfig在同一個網路介面eth0上繫結3個IP:

[[email protected] root]# ifconfig eth0:1 192.168.1.11 
[[email protected] root]# ifconfig eth0:2 192.168.1.12 
[[email protected] root]# ifconfig eth0:3 192.168.1.13 
2. 修改hosts檔案,新增三個域名與之一一對應:

192.168.1.11 www.test1.com 
192.168.1.12 www.test2.com 
192.168.1.13 www.test3.com 
3. 建立虛擬主機存放網頁的根目錄,如在/www目錄下建立test1、test2、test3資料夾,其中分別存放1.html、2.html、3.html

/www/test1/1.html 
/www/test2/2.html 
/www/test3/3.html 
4. 在httpd.conf中將附加配置檔案httpd-vhosts.conf包含進來,接著在httpd-vhosts.conf中寫入如下配置:

複製程式碼

<VirtualHost 192.168.1.11:80>
  ServerName www.test1.com
  DocumentRoot /www/test1/
  <Directory "/www/test1">
     Options Indexes FollowSymLinks
     AllowOverride None
     Order
allow,deny      Allow From All   </Directory> </VirtualHost> <VirtualHost 192.168.1.12:80>   ServerName www.test1.com   DocumentRoot /www/test2/   <Directory "/www/test2">     Options Indexes FollowSymLinks      AllowOverride None      Order allow,deny      Allow From All   </Directory> </VirtualHost> <VirtualHost 192.168.1.13:80>   ServerName www.test1.com   DocumentRoot /www/test3/   <Directory "/www/test3">     Options Indexes FollowSymLinks      AllowOverride None      Order allow,deny      Allow From All   </Directory> </VirtualHost>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

複製程式碼 
5. 大功告成,測試下每個虛擬主機,分別訪問www.test1.com、www.test2.com、www.test3.com

二、基於主機名

  1. 設定域名對映同一個IP,修改hosts:

127.0.0.1 gm.998gx.com 
127.0.0.1 www.998gx.com 
127.0.0.1 r.998gx.com 
127.0.0.1 localhost 
2. 跟上面一樣,建立虛擬主機存放網頁的根目錄

/www/dxGM/index.php 
/www/dxskadmin/index.php 
/www/88qp/index.php 
3. 在httpd.conf中將附加配置檔案httpd-vhosts.conf包含進來,接著在httpd-vhosts.conf中寫入如下配置:

  為了使用基於域名的虛擬主機,必須指定伺服器IP地址(和可能的埠)來使主機接受請求。可以用NameVirtualHost指令來進行配置。 如果伺服器上所有的IP地址都會用到, 你可以用*作為NameVirtualHost的引數。在NameVirtualHost指令中指明IP地址並不會使伺服器自動偵聽那個IP地址。 這裡設定的IP地址必須對應伺服器上的一個網路介面。 
  下一步就是為你建立的每個虛擬主機設定配置塊,的引數與NameVirtualHost指令的引數是一樣的。每個定義塊中,至少都會有一個ServerName指令來指定伺服哪個主機和一個DocumentRoot指令來說明這個主機的內容存在於檔案系統的什麼地方。

  如果在現有的web伺服器上增加虛擬主機,必須也為現存的主機建造一個定義塊。其中ServerName和DocumentRoot所包含的內容應該與全域性的保持一致,且要放在配置檔案的最前面,扮演預設主機的角色。

複製程式碼

<VirtualHost *:80> 
    DocumentRoot "D:/phpstudy/WWW/dxGM" 
    ServerName gm.998gx.com 
</VirtualHost>

<VirtualHost *:80> 
    DocumentRoot "D:/phpstudy/WWW/88qp" 
    ServerName www.998gx.com 
</VirtualHost>

<VirtualHost *:80> 
    DocumentRoot "D:/phpstudy/WWW/dxskadmin" 
    ServerName r.998gx.com 
</VirtualHost>

<VirtualHost *:80> 
    DocumentRoot "D:/phpstudy/WWW" 
    ServerName localhost 
</VirtualHost>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

複製程式碼 
4. 大功告成,測試下每個虛擬主機,分別訪問gm.998gx.com、www.998gx.com、r.998gx.com

三、基於埠

  1. 修改配置檔案 
      將原來的

        Listen 80 
    改為 
      Listen 80 
       Listen 8080

  2. 更改虛擬主機設定:

複製程式碼