一。定義
所謂虛擬主機是指在一臺服務器里運行幾個網站,提供WEB、FTP、Mail等服務。
二。虛擬主機的實現方法有三種:
基于IP的方法,基于主機名的方法和基于端口的法官法。
①基于IP的方法:
在服務器里綁定多個IP,然后配置WEB服務器,把多個網站綁定在不同的IP上。訪問不同的IP,就看到不同的網站。
②基于端口的方法:
一個IP地址,通過不同的端口實在不同網站的訪問。
③基于主機名的方法:
設置多個域名的A記錄,使它們解析到同一個IP地址上,即同一個服務器上。然后,在服務器上配置WEB服務端,添加多個網站,為每個網站設定一個主機名。因為HTTP協(xié)議訪問請求里包含有主機名信息,當WEB服務器收到訪問請求時,就可以根據不同的主機名來訪問不同的網站。
三。三種虛擬主機實現的基本配置
①基于IP虛擬主機的實現:
多個ip,需要把中心主機取消
打開web服務的主配置文檔:vim /etc/httpd/conf/httpd.conf
DocumentRoot 注釋掉
配置虛擬主機:
DocumentRoot "/www/a.com"
ServerName www.a.com
DocumentRoot "/www/b.com"
ServerName www.b.com
vim /etc/hosts
192.168.0.20 www.a.com
192.168.0.25 www.b.com
瀏覽器中輸入IP地址進行實驗效果的驗證。
②基于端口:
DocumentRoot "/www/a.com"
ServerName www.a.com
DocumentRoot "/www/b.com"
ServerName www.b.com
③基于主機名:
開啟:NameVirtualHost 192.168.0.20: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
ServerAdmin www.b.com
DocumentRoot /etc/httpd/aaa/b.com
四。案例綜合實現
建立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
DocumentRoot "/var/www/html/www1"
ServerName www1.ilinux.org
Errorlog /var/log/httpd/www1.err
CustomLog /var/log/httpd/www1.access common
Options Indexes
AllowOverride None
Order allow,deny
Allow from 192.168.0.0/24
DocumentRoot "/var/www/html/www2"
ServerName www2.ilinux.org
Errorlog /var/log/httpd/www2.err
CustomLog /var/log/httpd/www2.access common
Options Indexes
AllowOverride None
Order allow,deny
Allow from all
DocumentRoot /var/www/html/www3
ServerName www3.ilinux.org
ErrorLog /var/log/httpd/www3.err
CustomLog /var/log/httpd/www3.access combined
Options Indexes
AllowOverride AuthConfig
AuthName "AuthConfig"
AuthType basic
AuthUserFile /etc/httpd/.htpasswd
require user webuser1 webuser2
Order allow,deny
Allow from all
htpasswd -cm /etc/httpd/.htpasswd webuser1
htpasswd -m /etc/httpd/.htpasswd webuser2
③分別在/var/www/html目錄下創(chuàng)建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