Apache 配置虛擬主機三種方式

2021-07-03 17:42:13 字數 3298 閱讀 6427

一、基於ip

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

[root@localhost root]# ifconfig eth0:1 192.168.1.11

[root@localhost root]# ifconfig eth0:2 192.168.1.12

[root@localhost 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中寫入如下配置:

servername www.test1.com

documentroot /www/test1/

options indexes followsymlinks

allowoverride none

order allow,deny

allow from all

servername www.test1.com

documentroot /www/test2/

options indexes followsymlinks

allowoverride none

order allow,deny

allow from all

servername www.test1.com

documentroot /www/test3/

options indexes followsymlinks

allowoverride none

order allow,deny

allow from all

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

二、基於主機名

1. 設定網域名稱對映同乙個ip,修改hosts:

192.168.1.10  www.test1.com

192.168.1.10  www.test2.com

192.168.1.10  www.test3.com

2. 跟上面一樣,建立虛擬主機存放網頁的根目錄

/www/test1/1.html

/www/test2/2.html

/www/test3/3.html

3. 在httpd.conf中將附加配置檔案httpd-vhosts.conf包含進來,接著在httpd-vhosts.conf中寫入如下配置:

為了使用基於網域名稱的虛擬主機,必須指定伺服器ip位址(和可能的埠)來使主機接受請求。

可以用namevirtualhost

指令來進行配置。 如果伺服器上所有的ip位址都會用到, 你可以用

*作為namevirtualhost的引數。

在namevirtualhost指令中指明ip位址

並不會使伺服器自動偵聽那個ip位址

。 這裡設定的ip位址必須對應伺服器上的乙個網路介面。

下一步就是為你建立的每個虛擬主機設定配置塊,<

virtualhost>的引數與namevirtualhost指令的引數是一樣的。

每個定義塊中,至少都會有乙個

servername

指令來指定伺服哪個主機和乙個

documentroot

指令來說明這個主機的內容存在於檔案系統的什麼地方。

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

namevirtualhost *:80

servername *

documentroot /www/ 

servername www.test1.com

documentroot /www/test1/

options indexes followsymlinks

allowoverride none

order allow,deny

allow from all

servername www.test2.com

documentroot /www/test2/

options indexes followsymlinks

allowoverride none

order allow,deny

allow from all

servername www.test3.com

documentroot /www/test3/

options indexes followsymlinks

allowoverride none

order allow,deny

allow from all

4. 大功告成,測試下每個虛擬主機,分別訪問www.test1.com、www.test2.com、www.test3.com

三、基於埠

1.  修改配置檔案

將原來的

listen 80

改為listen 80

listen 8080

2. 更改虛擬主機設定:

documentroot /var/www/test1/

servername www.test1.com

documentroot /var/www/test2

servername www.test2.com

whereis httpd   檢視httpd的位置

或者用 which httpd

2 rpm包形式

rpm -aq|grep http   //檢視是否安裝了apache包,若已安裝,則會列印出包名

rpm -qi 輸入獲取到的包名  //檢視該apache包資訊

rpm -ql 輸入包名          //檢視apache包中所有檔案的安裝位置 

Apache 配置虛擬主機三種方式

一 基於ip root localhost root ifconfig eth0 1 192.168.1.11 root localhost root ifconfig eth0 2 192.168.1.12 root localhost root ifconfig eth0 3 192.168.1...

Apache 配置虛擬主機三種方式

一 基於ip 1.假設伺服器有個ip位址為192.168.1.10,使用ifconfig在同乙個網路介面eth0上繫結3個ip root localhost root ifconfig eth0 1 192.168.1.11 root localhost root ifconfig eth0 2 1...

Apache 配置虛擬主機三種方式

一 基於ip 1.假設伺服器有個ip位址為192.168.1.10,使用ifconfig在同乙個網路介面eth0上繫結3個ip root localhost root ifconfig eth0 1 192.168.1.11 root localhost root ifconfig eth0 2 1...