[root@localhost ~]# yum install bind httpd -y //在服務器上安裝DNS與HTTP服務 [root@localhost ~]# cd /etc/ //進入etc目錄 [root@localhost etc]# vim named.conf //進入編輯DNS服務主配置文件 ...//省略部份內容... options { listen-on port 53 { any; }; //將監聽IP地址更改成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"; recursing-file "/var/named/data/named.recursing"; secroots-file "/var/named/data/named.secroots"; allow-query { any; }; //主機名更改成any,容許全部主機經過解析 ...//省略部份內容... :wq //保存退出 [root@localhost etc]# vim named.rfc1912.zones //進入編輯區域配置文件 ...//省略部份內容... zone "kgc.com" IN { //更改域名 type master; file "kgc.com.zone"; //更改數據文件名稱 allow-update { none; }; }; zone "aaa.com" IN { //更改域名 type master; file "aaa.com.zone"; //更改數據文件名稱 allow-update { none; }; }; ...//省略部份內容... [root@localhost etc]# cd /var/named/ //進入區域數據文件存放目錄 [root@localhost named]# cp -p named.localhost kgc.com.zone //複製區域數據文件模板 [root@localhost named]# vim kgc.com.zone //進入編輯模板 $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.144.133 //刪掉最後一行,更改成此行 :wq //保存退出 [root@localhost named]# cp -p kgc.com.zone aaa.com.zone //複製剛更改的數據文件,命名爲aaa.com.zone,不須要更改內容 [root@localhost named]# systemctl start named //啓動DNS服務 [root@localhost named]# systemctl stop firewalld.service //關閉防火牆 [root@localhost named]# setenforce 0 //關閉加強性安全功能
[root@localhost html]# cd /etc/httpd/conf/ //進入http服務配置文件目錄 [root@localhost conf]# mkdir extra //建立文件夾 [root@localhost conf]# ls //查看目錄 extra httpd.conf magic [root@localhost extra]# vim vhost.conf //編輯子配置文件 <VirtualHost *:80> DocumentRoot "/var/www/html/aaa/" ServerName www.aaa.com ErrorLog "logs/www.aaa.com.error_log" CustomLog "logs/www.aaa.com.access_log" common <Directory "/var/www/html"> Require all granted //編輯虛擬主機配置條目 </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "/var/www/html/kgc/" ServerName www.kgc.com ErrorLog "logs/www.kgc.com.error_log" CustomLog "logs/www.kgc.com.access_log" common <Directory "/var/www/html"> Require all granted </Directory> </VirtualHost> ~ :wq //保存退出 [root@localhost extra]# cd /var/www/html/ //進入http服務網頁站點 [root@localhost html]# mkdir aaa kgc //建立目錄文件 [root@localhost html]# cd aaa/ //進入目錄 [root@localhost aaa]# vim index.html //編輯默認主頁 <h1>this is aaa web</h1> //編寫內容 ~ :wq //保存退出 [root@localhost aaa]# ls //查看目錄 index.html [root@localhost aaa]# cd ../kgc/ //返回上一層並進入kgc目錄 [root@localhost kgc]# vim index.html //編輯默認網頁 <h1>this is kgc web</h1> //編輯內容 ~ :wq //保存退出 [root@localhost kgc]# ls //查看目錄 index.html [root@localhost kgc]# vim /etc/httpd/conf/httpd.conf //進入編輯http服務主配置文件 ...//省略部份內容... # prevent Apache from glomming onto all bound IP addresses. # Listen 192.168.144.137:80 //打開ipv4監聽地址,並更改成本機IP地址 #Listen 80 //關閉ipv6的監聽地址 ...//省略部份內容... # Load config files in the "/etc/httpd/conf.d" directory, if any. IncludeOptional conf.d/*.conf Include conf/extra/vhost.conf //在最後一行添加包含子配置文件目錄條目 :wq //保存退出 [root@localhost kgc]# systemctl start httpd //開啓http服務 [root@localhost kgc]# netstat -ntap | grep 80 //查看80端口是否開啓 tcp6 0 0 :::80 :::* LISTEN 2450/httpd
打開一臺win10客戶機,更改DNS服務器地址,打開網頁,測試基於不一樣域名構建虛擬主機是否成功html
此處實驗就直接在上面的實驗中更改配置,不從新操做web
[root@localhost named]# vim /etc/httpd/conf/extra/vhost.conf ...//省略部份內容... <VirtualHost *:80> DocumentRoot "/var/www/html/kgc/" ServerName www.kgc.com ErrorLog "logs/www.kgc.com.error_log" CustomLog "logs/www.kgc.com.access_log" common <Directory "/var/www/html"> Require all granted </Directory> </VirtualHost> <VirtualHost *:8080> DocumentRoot "/var/www/html/kgc02/" ServerName www.kgc.com ErrorLog "logs/www.kgc02.com.error_log" CustomLog "logs/www.kgc02.com.access_log" common <Directory "/var/www/html"> //複製上面的配置文件條目,並更改監聽端口爲8080,並將站點目錄、日誌文件名更改成kgc02 Require all granted </Directory> </VirtualHost> :wq //保存退出 [root@localhost named]# cd /var/www/html/ [root@localhost html]# mkdir kgc02 [root@localhost html]# cd kgc02/ [root@localhost kgc02]# vim index.html <h1>this is kgc02 web</h1> ~ :wq [root@localhost kgc02]# vim /etc/httpd/conf/httpd.conf ...//省略部份內容... # prevent Apache from glomming onto all bound IP addresses. # Listen 192.168.144.137:80 Listen 192.168.144.137:8080 #Listen 80 ...//省略部份內容... :wq [root@localhost kgc02]# systemctl restart httpd
在win10客戶端驗證基於端口的虛擬主機配置vim
在這裏先給Linux服務器虛擬機添加一張網卡,獲取另外一個IP地址安全
在虛擬機中獲取IP地址服務器
[root@localhost ~]# ifconfig //查看網卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.144.137 netmask 255.255.255.0 broadcast 192.168.144.255 inet6 fe80::a85a:c203:e2e:3f3c prefixlen 64 scopeid 0x20<link> inet6 fe80::ad78:663f:1f02:22e4 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:72:65:cb txqueuelen 1000 (Ethernet) RX packets 14117 bytes 10290025 (9.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6337 bytes 767788 (749.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.144.143 netmask 255.255.255.0 broadcast 192.168.144.255 //成功獲取IP地址 inet6 fe80::d65e:47b1:916d:de6c prefixlen 64 scopeid 0x20<link> ether 00:0c:29:72:65:d5 txqueuelen 1000 (Ethernet) RX packets 115 bytes 20495 (20.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 79 bytes 17837 (17.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ...//省略部份內容... [root@localhost ~]# vim /etc/httpd/conf/extra/vhost.conf //進入編輯http服務子配置文件 <VirtualHost 192.168.144.137:80> //更改成固定IP地址 DocumentRoot "/var/www/html/aaa/" ServerName www.aaa.com ErrorLog "logs/www.aaa.com.error_log" CustomLog "logs/www.aaa.com.access_log" common <Directory "/var/www/html"> Require all granted </Directory> </VirtualHost> <VirtualHost 192.168.144.143:80> //複製上面配置條,更改IP地址 DocumentRoot "/var/www/html/aaa02/" //更改站點文件 ServerName www.naaa.com //更改域名 ErrorLog "logs/www.aaa02.com.error_log" //更改錯誤日誌文件名 CustomLog "logs/www.aaa02.com.access_log" common //更改日誌文件名 <Directory "/var/www/html"> Require all granted </Directory> </VirtualHost> :wq //保存退出 [root@localhost ~]# vim /etc/named.rfc1912.zones //編輯DNS區域配置文件 ...//省略部份內容... zone "aaa.com" IN { type master; file "aaa.com.zone"; allow-update { none; }; }; zone "naaa.com" IN { type master; //添加新的區域配置文件 file "naaa.com.zone"; allow-update { none; }; }; ...//省略部份內容... :wq //保存退出 [root@localhost ~]# cd /var/named/ //進入區域數據文件存放目錄 [root@localhost named]# cp -p aaa.com.zone naaa.com.zone //複製區域數據文件 [root@localhost named]# vim naaa.com.zone //進入編輯新的區域數據文件 $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.144.143 //更改IP地址 ~ :wq //保存退出 [root@localhost named]# cd /var/www/html/ //進入http服務站點 [root@localhost html]# mkdir aaa02 //建立新的站點目錄 [root@localhost html]# cd aaa02/ //進入目錄 [root@localhost aaa02]# vim index.html //編輯默認主頁文件 <h1>this is 143 aaa02 web</h1> //編輯網頁內容 :wq //保存退出 [root@localhost aaa02]# vim /etc/httpd/conf/httpd.conf //編輯http服務主配置文件 ...//省略部份內容... # prevent Apache from glomming onto all bound IP addresses. # Listen 192.168.144.137:80 Listen 192.168.144.143:80 //編輯監聽新的IP地址 #Listen 80 ...//省略部份內容... :wq //保存退出 [root@localhost aaa02]# systemctl restart httpd //重啓http服務 [root@localhost aaa02]# systemctl restart named //重啓DNS服務
在win10客戶機中驗證基於IP地址創建的虛擬主機tcp