[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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。