實驗需求:安裝Apache與DNS服務
給虛擬機增長兩張網卡
[root@localhost ~]# yum install bind httpd -y
增長網卡
html
[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 0web
[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
<h1>this is accp web</h1> //輸入網頁內容
[root@localhost accp]# cd ../kgc/
[root@localhost kgc]# vim index.htm
<h1>this is kgc web</h1> //輸入網頁內容
[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在下行插入此內容vim
[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>ide<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>
ui
[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
<h1>this is kgc02 web</h1> //輸入網頁內容this
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 //重啓服務3d
在測試機win10輸入www.kgc.com:8080 進行測試
在測試機win10輸入www.kgc.com 則默認端口號爲80
rest
[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
<h1>this is accp02 web</h1> //輸入網頁內容
[root@localhost extra]# systemctl restart httpd //重啓服務三、在客戶機分別訪問虛擬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就須要輸入密碼進行驗證了