1. 程式人生 > >2.4 httpd 構建虛擬Web主機的三種方式

2.4 httpd 構建虛擬Web主機的三種方式

service 區分 0.11 amp clu p地址 站點 common req

##############################構建Web虛擬主##################################
httpd支持的虛擬主機類型包括以下三種。
基於域名:為每個虛擬主機使用不同的域名,但是其對於的ip地址是相同的。
基於IP地址:為每個虛擬主機使用不同的域名,且各自對於的ip地址也不相同。
基於端口:不使用域名,IP地址來區分不同的站點內容,而是使用了不同的TCP端口號。
`

#################################基於IP地址的虛擬主機#########################
<br/>####開啟虛擬機主機功能模塊####<br/>


vi /usr/local/httpd/conf/httpd.conf
#Listen localhost:80 #必須禁用
#Virtual hosts
Include conf/extra/httpd-vhosts.conf #這行前面的#去掉 vhosts模塊生效
`
cd /usr/local/httpd/conf/extra/
vi httpd-vhosts.conf

<br/>######為了直接顯示源代碼,在後面添加ab、aa為主頁文件##<br/>vi /usr/local/httpd/httpd.conf <br/>**&lt;IfModule dir_module&gt;**<br/>DirectoryIndex index.html ab.html aa.html<br/>**&lt;/IfModule&gt;**<br/>


####默認的實列#去掉####
#<VirtualHost :80>
#ServerAdmin [email protected]
#DocumentRoot "/usr/local/httpd/docs/dummy-host.example.com"
#ServerName dummy-host.example.com
#ServerAlias www.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 [email protected]
#DocumentRoot "/usr/local/httpd/docs/dummy-host2.example.com"
#ServerName dummy-host2.example.com
#ErrorLog "logs/dummy-host2.example.com-error_log"
#CustomLog "logs/dummy-host2.example.com-access_log" common
#</VirtualHost>
`

###########基於IP地址的虛擬主機##########
Listen 192.168.10.11:80 ####必須要開啟監聽

<VirtualHost 192.168.10.11:80>
ServerAdmin [email protected] ####設置http服務器管理員的E-mail地址,可以通過E-mail地址聯系WEB站點的管理員
DocumentRoot /opt/aa/ ####設置網站跟目錄,即網頁在系統中存放的路徑
ServerName www.aa.com ####設置網站的完整主機名,即(主機名+域名)
ErrorLog logs/aa.com-error_log ####錯誤日誌存放路徑
CustomLog logs/aa.com-access_log common ####訪問日誌文件路勁
</VirtualHost>

<Directory "/opt/aa"> ####區域配置的根目錄 是/opt/aa目錄
Options Indexes FollowSymLinks ###基礎配置
AllowOverride All ######允許所有主機
Require all granted ####允許所有主機訪問
</Directory>

Listen 192.168.20.11:80
<VirtualHost 192.168.20.11:80>
ServerAdmin [email protected]
DocumentRoot /opt/ab/
ServerName www.ab.com
ErrorLog logs/ab.com-error_log
CustomLog logs/ab.com-access_log common
</VirtualHost>

<Directory "/opt/ab">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<br/>######驗證#####<br/>service httpd restart<br/>登錄客戶端的瀏覽器測試!!!<br/>測試2個地址: <br/>1、192.168.10.11<br/>2、192.168.20.11<br/>
###################################基於端口號################################
Listen 192.168.10.11:80

<VirtualHost 192.168.10.11:80>
ServerAdmin [email protected]
DocumentRoot /opt/aa/
ServerName www.aa.com
ErrorLog logs/aa.com-error_log
CustomLog logs/aa.com-access_log common
</VirtualHost>

<Directory "/opt/aa">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Listen 192.168.10.11:8080
<VirtualHost 192.168.10.11:8080>
ServerAdmin [email protected]
DocumentRoot /opt/ab/
ServerName www.ab.com
ErrorLog logs/ab.com-error_log
CustomLog logs/ab.com-access_log common
</VirtualHost>
<Directory "/opt/ab">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
`
######驗證#####
service httpd restart
登錄客戶端的瀏覽器測試!!!
測試2個地址:
1、192.168.10.11:80
2、192.168.10.11:8080

###########################基於域名的虛擬主機###################
Listen 192.168.10.74:80
`
*<VirtualHost :80>
ServerAdmin [email protected]
DocumentRoot /opt/aa/
ServerName www.aa.com
ErrorLog logs/aa.com-error_log
CustomLog logs/aa.com-access_log common
</VirtualHost>**

<Directory "/opt/aa">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>

*<VirtualHost :80>
ServerAdmin [email protected]
DocumentRoot /opt/ab/
ServerName www.ab.com
ErrorLog logs/ab.com-error_log
CustomLog logs/ab.com-access_log common
</VirtualHost>**

<Directory "/opt/ab">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

######驗證#####
service httpd restart
登錄客戶端的瀏覽器測試!!
####
客戶端需要在hosts文件添加域名解析:

192.168.10.11 www.aa.com
192.168.10.11 www.ab.com

####
測試2個地址:
1、www.aa.com
2、www.ab.com

2.4 httpd 構建虛擬Web主機的三種方式