系統(tǒng)運(yùn)維
實(shí)驗(yàn)需求:安裝Apache與dns服務(wù)
給虛擬機(jī)增加兩張網(wǎng)卡
[root@localhost ~]# yum install bind httpd -y
增加網(wǎng)卡
2、為虛擬主機(jī)準(zhǔn)備網(wǎng)頁文檔[root@localhost ~]# vim /etc/named.conf //配置DNS主配置文件
[root@localhost ~]# vim /etc/named.rfc1912.zones //配置DNS區(qū)域配置文件
[root@localhost ~]# cd /var/named/
[root@ named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone //配置DNS區(qū)域數(shù)據(jù)配置文件
[root@localhost named]# cp -p kgc.com.zone accp.com.zone
//這里的DNS區(qū)域數(shù)據(jù)配置文件無需再修改
[root@localhost named]# systemctl start named //啟動(dòng)DNS服務(wù)
[root@localhost named]# systemctl stop firewalld.service //關(guān)閉防火墻
[root@localhost named]# setenforce 0
3、添加虛擬主機(jī)配置[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.htmlthis is accp web
//輸入網(wǎng)頁內(nèi)容
[root@localhost accp]# cd ../kgc/
[root@localhost kgc]# vim index.htmthis is kgc web
//輸入網(wǎng)頁內(nèi)容
[root@localhost ~]# cd /etc/httpd/
[root@localhost httpd]# cd conf
[root@localhost conf]# vim httpd.conf
找到監(jiān)聽端口注釋ipv6端口,開啟ipv4端口,并將監(jiān)聽地址修改為本機(jī)地址。
Include conf/extra/vhost.conf //按大寫字母G到末行,按o在下行插入此內(nèi)容
4、在客戶機(jī)分別訪問虛擬Web主機(jī)[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
DocumentRoot "/var/www/html/kgc/" //站點(diǎn)目錄
ServerName www.kgc.com //域名
ErrorLog "logs/www.kgc.com.error_log" //錯(cuò)誤日志
CustomLog "logs/www.kgc.com.access_log" common //訪問日志
Require all granted //訪問權(quán)限為允許所有
DocumentRoot "/var/www/html/accp/"
ServerName www.accp.com
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
Require all granted
[root@localhost conf]# systemctl start httpd //啟動(dòng)服務(wù)
在測(cè)試機(jī)win10分別輸入網(wǎng)址:www.kgc.com www.accp.com
構(gòu)建虛擬主機(jī)一 基于端口訪問 1、添加虛擬主機(jī)配置2、為虛擬主機(jī)準(zhǔn)備網(wǎng)頁文檔[root@localhost extra]# vim vhost.conf
//在虛擬主機(jī)配置文件添加以下內(nèi)容
DocumentRoot "/var/www/html/kgc02/"
ServerName www.kgc.com
ErrorLog "logs/www.kgc02.com.error_log"
CustomLog "logs/www.kgc02.com.access_log" common
Require all granted
3、進(jìn)入Apache配置文件添加端口[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.htmlthis is kgc02 web
//輸入網(wǎng)頁內(nèi)容
4、在客戶機(jī)分別訪問虛擬Web主機(jī)vim /etc/httpd/conf/httpd.conf
輸入/Listen查找到配置文件中監(jiān)聽端口位置
輸入:
Listen 192.168.131.129:80
Listen 192.168.131.129:8080
[root@localhost extra]# systemctl restart httpd //重啟服務(wù)
在測(cè)試機(jī)win10輸入www.kgc.com:8080 進(jìn)行測(cè)試
在測(cè)試機(jī)win10輸入www.kgc.com 則默認(rèn)端口號(hào)為80
2、為虛擬主機(jī)準(zhǔn)備網(wǎng)頁文檔[root@localhost kgc02]# cd /etc/httpd/conf/extra/
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf //進(jìn)入虛擬主機(jī)配置添加以下內(nèi)容
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
Require all granted//把本行 改為第二塊網(wǎng)卡的IP地址
DocumentRoot "/var/www/html/accp02/"
ErrorLog "logs/www.accp02.com.error_log"
CustomLog "logs/www.accp02.com.access_log" common
Require all granted
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir accp02
[root@localhost html]# cd accp02/
[root@localhost accp02]# vim index.htmlthis is accp02 web
//輸入網(wǎng)頁內(nèi)容
[root@localhost extra]# systemctl restart httpd //重啟服務(wù)3、在客戶機(jī)分別訪問虛擬Web主機(jī)在測(cè)試機(jī)win10分別輸入:192.168.131.129 /192.168.131.130進(jìn)行測(cè)試
但在實(shí)際情況中,我們?cè)L問網(wǎng)站一般都是使用域名,所以我們還需要為新增的IP地址提供域名解析服務(wù)。
修改DNS區(qū)域配置文件
vim /etc/named.rfc1912.zones //在DNS區(qū)域配置文件添加新的內(nèi)容
修改DNS數(shù)據(jù)配置文件
[root@localhost extra]# cd /var/named/
[root@localhost named]# cp -p accp.com.zone naccp.com.zone //復(fù)制區(qū)域數(shù)據(jù)配置文件模板
[root@localhost named]# vim naccp.com.zone //進(jìn)入?yún)^(qū)域數(shù)據(jù)配置文件進(jìn)行修改
[root@localhost extra]# systemctl restart httpd //重啟服務(wù)
在測(cè)試機(jī)win10分別輸入:www.kgc.com /www.naccp.com 進(jìn)行測(cè)試
構(gòu)建虛擬主機(jī)一 基于權(quán)限在虛擬主機(jī)配置中添加黑白名單
[root@localhost named]# cd /etc/httpd/conf/extra
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf
DocumentRoot "/var/www/html/accp/"
ServerName www.accp.com
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
Require not ip 192.168.131.128 //添加測(cè)試機(jī)IP地址為不允許訪問accp.com
Require all granted
[root@localhost extra]# systemctl restart httpd //重啟服務(wù)
驗(yàn)證黑白名單
使用測(cè)試機(jī)win10分別訪問:www.kgc.com / www.naccp.com
naccp.com可以正常訪問,而kgc.com則無法訪問,直接跳轉(zhuǎn)到Apache默認(rèn)網(wǎng)頁
密碼驗(yàn)證訪問Apache網(wǎng)頁
[root@localhost conf]# cd /etc/httpd/conf
[root@localhost conf]# htpasswd -c /etc/httpd/conf/pwd tese01 //創(chuàng)建訪問用戶
New password: //設(shè)置訪密碼
Re-type new password: //重復(fù)輸入密碼
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 //進(jìn)入虛擬主機(jī)配置文件配置以下內(nèi)容
DocumentRoot "/var/www/html/accp02/"
ServerName www.naccp.com
ErrorLog "logs/www.accp02.com.error_log"
CustomLog "logs/www.accp02.com.access_log" common
AuthName "DocumentRoot"
AuthType Basic
AuthUserFile /etc/httpd/conf/pwd
Require valid-user
[root@localhost extra]# systemctl restart httpd //重啟服務(wù)
使用測(cè)試機(jī)win10輸入:www.naccp.com 進(jìn)行測(cè)試
此時(shí)我們?nèi)粼傧朐L問naccp.com就需要輸入密碼進(jìn)行驗(yàn)證了