您好,登錄后才能下訂單哦!
[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 //編輯默認主頁
<h2>this is aaa web</h2> //編寫內容
~
:wq //保存退出
[root@localhost aaa]# ls //查看目錄
index.html
[root@localhost aaa]# cd ../kgc/ //返回上一層并進入kgc目錄
[root@localhost kgc]# vim index.html //編輯默認網頁
<h2>this is kgc web</h2> //編輯內容
~
: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服務器地址,打開網頁,測試基于不同域名構建虛擬主機是否成功
此處實驗就直接在上面的實驗中更改配置,不重新操作
[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
<h2>this is kgc02 web</h2>
~
: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客戶端驗證基于端口的虛擬主機配置
在這里先給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 //編輯默認主頁文件
<h2>this is 143 aaa02 web</h2> //編輯網頁內容
: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地址建立的虛擬主機
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。