Web服務器(Apache)虛擬主機的配置

一.定義

所謂虛擬主機是指在一臺服務器裏運行幾個網站,提供WEB、FTP、Mail等服務。

二.虛擬主機的實現方法有三種:

基於IP的方法,基於主機名的方法和基於端口的法官法。

①基於IP的方法:
  
  在服務器裏綁定多個IP,然後配置WEB服務器,把多個網站綁定在不同的IP上。訪問不同的IP,就看到不同的網站。
②基於端口的方法:

一個IP地址,通過不同的端口實在不同網站的訪問。
③基於主機名的方法:
  
 設置多個域名的A記錄,使它們解析到同一個IP地址上,即同一個服務器上。然後,在服務器上配置WEB服務端,添加多個網站,爲每個網站設定一個主機名。因爲HTTP協議訪問請求裏包含有主機名信息,當WEB服務器收到訪問請求時,就可以根據不同的主機名來訪問不同的網站。
三.三種虛擬主機實現的基本配置

①基於IP虛擬主機的實現:
多個ip,需要把中心主機取消

打開web服務的主配置文檔:vim /etc/httpd/conf/httpd.conf

DocumentRoot 註釋掉
配置虛擬主機:
<VirtualHost 192.168.0.20:80>
DocumentRoot "/www/a.com"
ServerName  www.a.com
<VirtualHost/>
<VirtualHost 192.168.0.25:80>
DocumentRoot "/www/b.com"
ServerName  www.b.com
<VirtualHost/>

vim /etc/hosts

192.168.0.20  www.a.com

192.168.0.25 www.b.com

瀏覽器中輸入IP地址進行實驗效果的驗證。

②基於端口:
<VirtualHost 192.168.0.20:80>
DocumentRoot "/www/a.com"
ServerName  www.a.com
<VirtualHost/>
<VirtualHost 192.168.0.20:8080>
DocumentRoot "/www/b.com"
ServerName  www.b.com
<VirtualHost/>

③基於主機名:
開啓:NameVirtualHost 192.168.0.20:80
<VirtualHost *:80>
    ServerAdmin    www.a.com
    DocumentRoot /etc/httpd/aaa/a.com
    ServerName dummy-host.example.com
  ErrorLog logs/dummy-host.example.com-error_log
  CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin    www.b.com
    DocumentRoot /etc/httpd/aaa/b.com
</VirtualHost>
四.案例綜合實現

建立http服務器,要求:
 1)提供兩個基於名稱的虛擬主機:
  (a)www1.ilinux.org,頁面文件目錄爲/var/www/html/www1;錯誤日誌 爲/var/log/httpd/www1.err,訪問日誌爲/var/log/httpd/www1.access;
  (b)www2.ilinux.org,頁面文件目錄爲/var/www/html/www2;錯誤日誌爲/var/log/httpd/www2.err,訪問日誌爲/var/log/httpd/www2.access;
  (c)爲兩個虛擬主機建立各自的主頁文件index.html,內容分別爲其對應的主機名;
  2)www1主機僅允許192.168.0.0/24網絡中的客戶機訪問;www2主機可以被所有主機訪問;

爲http服務提供第3個虛擬主機,要求:
 1)www3.ilinux.org,頁面文件目錄爲/var/www/html/www3;錯誤日誌爲/var/log/httpd/www3.err,訪問日誌爲/var/log/httpd/www3.access;
 2)爲此虛擬主機提供基本認證功能,併爲其提供兩個虛擬用戶webuser1和webuser2,
  密碼均爲redhat,要求允許此兩用戶在提供密碼的情況下訪問此站點;


配置過程如下:

 ①安裝web服務:yum -y install httpd

②進入主配置文檔vim /etc/httpd/conf/httpd.conf

<VirtualHost *:80>
  DocumentRoot "/var/www/html/www1"
  ServerName  www1.ilinux.org
  Errorlog /var/log/httpd/www1.err
  CustomLog /var/log/httpd/www1.access common
   <Directory "/var/www/html/www1">
                Options Indexes
                AllowOverride None
                Order allow,deny
                Allow from 192.168.0.0/24
     </Directory>

</VirtualHost>

<VirtualHost *:80>
  DocumentRoot "/var/www/html/www2"
  ServerName  www2.ilinux.org
  Errorlog /var/log/httpd/www2.err
 CustomLog /var/log/httpd/www2.access common
    <Directory "/var/www/html/www2">
                Options Indexes
                AllowOverride None
               Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

<VirtualHost *:80>
        DocumentRoot /var/www/html/www3
        ServerName www3.ilinux.org
        ErrorLog /var/log/httpd/www3.err
        CustomLog /var/log/httpd/www3.access combined
        <Directory "/var/www/html/www3">
                Options Indexes
                AllowOverride AuthConfig
                AuthName "AuthConfig.."
                AuthType basic
                AuthUserFile /etc/httpd/.htpasswd
                require user webuser1 webuser2
                Order allow,deny
                Allow from all
        </Directory>
 </VirtualHost>

 htpasswd -cm /etc/httpd/.htpasswd webuser1
 htpasswd -m /etc/httpd/.htpasswd webuser2

③分別在/var/www/html目錄下創建www1,www2,www3目錄

vim /var/www/html/www1/index.html

This is www1 test!

vim /var/www/html/www2/index.html

This is www2 test!

vim /var/www/html/www3/index.html

This is www3 test!

④service httpd start 啓動web服務

⑤進行實驗效果的驗證:瀏覽器中分別輸入www1.ilinux.org  www2.ilinux.org www3.ilinux.org

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章