有關於虛擬主機的配置 ,大致可以分為三種即基於端口,基於ip和基於FQDN(域名)配置方法,接下來我就簡單的介紹一下三種不同的配置方式吧!
- (一)基於端口的虛擬主機配置
一個簡單的socket套接字有ip和端口號共同組成,基於端口的配置,即ip地址是相同的,服務所監聽的端口號不同。
1、先創建對應目錄和頁面對應文件
[root@bbs ~]# mkdir -pv /app/website{1,2}
[root@bbs ~]# cd /app/website1
[root@bbs website1]# echo website1 >index.html
[root@bbs ~]# cd /app/website2
[root@bbs website2]# echo website2 >index.html
2、編寫對應的http文件實現不同端口的監聽
[root@bbs ~]#vim /etc/httpd/conf.d/vhost.conf (Listen 8080 Listen 9090)
<VirtualHost *:8080> DocumentRoot "/app/website1" 對應的主目錄 <Directory "/app/website1"> Require all granted 允許所有可訪問 </Directory> </VirtualHost> <VirtualHost *:9090> DocumentRoot "/app/website2" <Directory "/app/website2"> Require all granted </Directory> </VirtualHost>
3、關閉防火牆和selinux,重啟服務
[root@bbs ~]# !sys
systemctl restart httpd [root@bbs ~]# setenforce 0
[root@bbs ~]# iptables –F
4、用curl命令進行測試或瀏覽器進行訪問注意瀏覽器進行訪問時要指定host文件,驗證配置是否成功。
[root@bbs ~]# curl http://172.18.254.181:8080
website1
- (二)基於ip的虛擬主機配置
由上可知基於ip,即是ip不同但是對應端口號相同。仍然使用上述對應目錄即可。
1、同上
[root@bbs ~]# mkdir -pv /app/website{1,2}
[root@bbs ~]# cd /app/website1
[root@bbs website1]# echo website1 >index.html
[root@bbs ~]# cd /app/website2
[root@bbs website2]# echo website2 >index.html
2、給網卡添加地址
[root@bbs ~]# ifconfig ens34:0 172.18.254.17/24
[root@bbs ~]# ifconfig ens34:1 172.18.254.27/24
3、編寫對應配置文件
[root@bbs ~]# vim /etc/httpd/conf.d/vhost.conf
<VirtualHost 172.18.254.17:80> DocumentRoot "/app/website1" <Directory "/app/website1"> Require all granted </Directory> </VirtualHost> <VirtualHost 172.18.254.27:80> DocumentRoot "/app/website2" <Directory "/app/website2"> Require all granted </Directory> </VirtualHost>
4、重啟服務並驗證
[root@bbs ~]# !sys
systemctl restart httpd [root@bbs ~]# curl http://172.18.254.17
website1
- (三)基於域名的虛擬主機
做這個實驗時,用到了dns域名解析服務,那么我們就先來配置一下dns,然后在搭建虛擬主機,當然你也可以使用別的方法來進行實驗。
1、修改及編寫dns相關的文件
[root@bbs ~]# vim /etc/named.conf
options { listen-on port 53 { any; }; listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; allow-query { any; }; 設置兩個any,其他內容保持不變。 [root@bbs named]# vim /etc/named.rfc1912.zones
zone "magedu.com" IN { type master; file "magedu.com.zone"; }; [root@bbs named]# vim magedu.com.zone
$TTL 1D @ IN SOA magedu.com. admin.magedu.com. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum magedu.com. NS bbs.magedu.com. magedu.com. NS www.magedu.com. www.magedu.com. A 172.18.254.181 bbs.magedu.com. A 172.18.254.181
2、指定本機訪問自己網址
[root@bbs named]# cat /etc/resolv.conf # Generated by NetworkManager
search magedu.com localdomain nameserver 172.18.254.181
3、用nslookup進行客服端驗證
[root@bbs named]# nslookup www.magedu.com
Server: 172.18.254.181 Address: 172.18.254.181#53
Name: www.magedu.com Address: 172.18.254.181 [root@bbs named]# nslookup bbs.magedu.com
Server: 172.18.254.181 Address: 172.18.254.181#53
Name: bbs.magedu.com Address: 172.18.254.181
4、編寫http對應配置文件
[root@bbs ~]# vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80> ServerName www.baidu.com DocumentRoot "/app/website1" CustomLog "logs/www.baidu.com_access_log" combined <Directory "/app/website1"> Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerName bbs.baidu.com DocumentRoot "/app/website2" CustomLog "logs/bbs.baidu.com_access_log" combined <Directory "/app/website2"> Require all granted </Directory> </VirtualHost>
5、重啟相應服務並驗證
[root@bbs named]# curl www.magedu.com
website1
以上就是虛擬主機的相關配置。