您好,登录后才能下订单哦!
实验需求:安装Apache与DNS服务
给虚拟机增加两张网卡
[root@localhost ~]# yum install bind httpd -y
增加网卡
[root@localhost ~]# vim /etc/named.conf //配置DNS主配置文件
[root@localhost ~]# vim /etc/named.rfc1912.zones //配置DNS区域配置文件
[root@localhost ~]# cd /var/named/
[root@ named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone //配置DNS区域数据配置文件
[root@localhost named]# cp -p kgc.com.zone accp.com.zone
//这里的DNS区域数据配置文件无需再修改
[root@localhost named]# systemctl start named //启动DNS服务
[root@localhost named]# systemctl stop firewalld.service //关闭防火墙
[root@localhost named]# setenforce 0
[root@localhost httpd]# cd /var/www/html
[root@localhost html]# ls
[root@localhost html]# mkdir accp kgc
[root@localhost html]# ls
accp kgc
[root@localhost html]# cd accp/
[root@localhost accp]# vim index.html
<h2>this is accp web</h2> //输入网页内容
[root@localhost accp]# cd ../kgc/
[root@localhost kgc]# vim index.htm
<h2>this is kgc web</h2> //输入网页内容
[root@localhost ~]# cd /etc/httpd/
[root@localhost httpd]# cd conf
[root@localhost conf]# vim httpd.conf
找到监听端口注释ipv6端口,开启ipv4端口,并将监听地址修改为本机地址。
Include conf/extra/vhost.conf //按大写字母G到末行,按o在下行插入此内容
[root@localhost named]# cd /etc/httpd/
[root@localhost httpd]# cd conf
[root@localhost conf]# mkdir extra
[root@localhost conf]# ls
extra httpd.conf magic
[root@localhost conf]# cd extra/
[root@localhost extra]# vim 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 *:80>
DocumentRoot "/var/www/html/accp/"
ServerName www.accp.com
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@localhost conf]# systemctl start httpd //启动服务
在测试机win10分别输入网址:www.kgc.com www.accp.com
[root@localhost extra]# vim vhost.conf
<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/">
Require all granted
</Directory>
</VirtualHost>
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# ls
accp kgc
[root@localhost html]# mkdir kgc02
[root@localhost html]# ls
accp kgc kgc02
[root@localhost html]# cd kgc02/
[root@localhost kgc02]# vim index.html
<h2>this is kgc02 web</h2> //输入网页内容
vim /etc/httpd/conf/httpd.conf
输入/Listen查找到配置文件中监听端口位置
输入:
Listen 192.168.131.129:80
Listen 192.168.131.129:8080
[root@localhost extra]# systemctl restart httpd //重启服务
在测试机win10输入www.kgc.com:8080 进行测试
在测试机win10输入www.kgc.com 则默认端口号为80
[root@localhost kgc02]# cd /etc/httpd/conf/extra/
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf //进入虚拟主机配置添加以下内容
VirtualHost 192.168.56.131:80> //基于ip访问,因此需要把改为本地地址
DocumentRoot "/var/www/html/accp/"
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.56.134:80> //把本行改为第二块网卡的IP地址
DocumentRoot "/var/www/html/accp02/"
ErrorLog "logs/www.accp02.com.error_log"
CustomLog "logs/www.accp02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir accp02
[root@localhost html]# cd accp02/
[root@localhost accp02]# vim index.html
<h2>this is accp02 web</h2> //输入网页内容
[root@localhost extra]# systemctl restart httpd //重启服务3、在客户机分别访问虚拟Web主机
在测试机win10分别输入:192.168.131.129 /192.168.131.130进行测试
但在实际情况中,我们访问网站一般都是使用域名,所以我们还需要为新增的IP地址提供域名解析服务。
修改DNS区域配置文件
vim /etc/named.rfc1912.zones //在DNS区域配置文件添加新的内容
修改DNS数据配置文件
[root@localhost extra]# cd /var/named/
[root@localhost named]# cp -p accp.com.zone naccp.com.zone //复制区域数据配置文件模板
[root@localhost named]# vim naccp.com.zone //进入区域数据配置文件进行修改
[root@localhost extra]# systemctl restart httpd //重启服务
在测试机win10分别输入:www.kgc.com /www.naccp.com 进行测试
在虚拟主机配置中添加黑白名单
[root@localhost named]# cd /etc/httpd/conf/extra
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf
<VirtualHost 192.168.131.129:80>
DocumentRoot "/var/www/html/accp/"
ServerName www.accp.com
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
<Directory "/var/www/html/">
<RequireAll>
Require not ip 192.168.131.128 //添加测试机IP地址为不允许访问accp.com
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
[root@localhost extra]# systemctl restart httpd //重启服务
验证黑白名单
使用测试机win10分别访问:www.kgc.com / www.naccp.com
naccp.com可以正常访问,而kgc.com则无法访问,直接跳转到Apache默认网页
密码验证访问Apache网页
[root@localhost conf]# cd /etc/httpd/conf
[root@localhost conf]# htpasswd -c /etc/httpd/conf/pwd tese01 //创建访问用户
New password: //设置访密码
Re-type new password: //重复输入密码
Adding password for user tese01
[root@localhost conf]# ls
extra httpd.conf magic pwd
[root@localhost conf]# cat pwd
tese01:$apr1$.HU1/Ltw$Isp7l.Qhe9Zlh81OU8e7M1 //密码密文
[root@localhost conf]# cd extra/
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf //进入虚拟主机配置文件配置以下内容
<VirtualHost 192.168.131.130:80>
DocumentRoot "/var/www/html/accp02/"
ServerName www.naccp.com
ErrorLog "logs/www.accp02.com.error_log"
CustomLog "logs/www.accp02.com.access_log" common
<Directory "/var/www/html/">
AuthName "DocumentRoot"
AuthType Basic
AuthUserFile /etc/httpd/conf/pwd
Require valid-user
</Directory>
</VirtualHost>
[root@localhost extra]# systemctl restart httpd //重启服务
使用测试机win10输入:www.naccp.com 进行测试
此时我们若再想访问naccp.com就需要输入密码进行验证了
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。