建立httpd服務,通過httpd-2.2和httpd2.4實現:
1、提供基於名稱的虛擬主機www1、www2,有單獨的錯誤日誌和訪問日誌
2、通過www1的/server-status的狀態信息,且允許tom用戶訪問
3、www2不允許192.168.0.0/24網絡中的任意主機訪問
4、爲第二個虛擬主機提供https服務
一、在CentOS-6上通過httpd-2.2實現以上要求
1、安裝httpd-2.2的rpm包,
[root@localhost wordpress]# yum -y install httpd
[root@localhost wordpress]# rpm -qa |grep httpd
httpd-tools-2.2.15-45.el6.centos.x86_64
httpd-2.2.15-45.el6.centos.x86_64
2、修改httpd的配置文件,新建兩個虛擬主機
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
ServerName www1.qq.com:80 找到此行,把前面的#號取消
NameVirtualHost 172.16.18.5:80 把這前面的#號取消,開啓虛擬主機
創建第一個虛擬主機配置文件
[root@localhost conf.d]# vim vhost1.conf
創建第二個虛擬主機配置文件
[root@localhost conf.d]# vim vhost2.conf
檢查配置文件,並啓動httpd服務
[root@localhost conf.d]# httpd -t
Syntax OK
[root@localhost conf.d]# service httpd restart
停止 httpd: [確定]
正在啓動 httpd: [確定]
3、創建html資源文件
[root@localhost www1]# vim index.html
<h1>www1.site</h1>
[root@localhost www2]# vim index.html
<h1>www2.site</h1>
創建訪問server-status的賬戶 tom
[root@localhost logs]# htpasswd -c -m /etc/httpd/conf/.htpasswd tom
4、修改hosts文件並進行測試
訪問www1虛擬主機,能夠訪問頁面
訪問www2虛擬主機,能夠訪問頁面
通過www1訪問/server-status,需要身份驗證
輸入用戶名tom和密碼,能夠訪問狀態信息
Apache Server Status for www1.qq.com
Server Version: Apache/2.2.15 (Unix) DAV/2
Server Built: Jul 24 2015 11:52:28
Current Time: Monday, 20-Jun-2016 01:23:56 CST
Restart Time: Monday, 20-Jun-2016 01:20:59 CST
Parent Server Generation: 0
Server uptime: 2 minutes 57 seconds
1 requests currently being processed, 7 idle workers
_W______........................................................ ................................................................ ................................................................ ................................................................
Scoreboard Key:
"_
" Waiting for Connection, "S
" Starting up, "R
" Reading Request,
"W
" Sending Reply, "K
" Keepalive (read), "D
" DNS Lookup,
"C
" Closing connection, "L
" Logging, "G
" Gracefully finishing,
"I
" Idle cleanup of worker, ".
" Open slot with no current process
查看日誌文件,也已經自動生成
[root@localhost logs]# ls
access_log error_log www1_access www1_error www2_access www2_error
通過以上配置和測試,已經滿足前三個要求
5、爲第二個虛擬主機提供https服務
搭建私有CA
(1)創建CA所需要的文件
[root@localhost CA]# touch index.txt
[root@localhost CA]# echo 01 > serial
(2)生成key密鑰
# (umask 066;openssl genrsa -out private/cakey.pem 2048)
(3)自簽證書
# openssl req -new -x509 -key private/cakey.pem -days 3650 -out cacert.pem
(4)爲www2.qq.com虛擬主機申請證書
# (umask 066;openssl genrsa -out httpd.key 2048)
# openssl req -new -key httpd.key -days 365 -out httpd.crl
# scp httpd.crl 172.16.18.9:/tmp
(5)私有CA爲虛擬主機www2.qq.com頒發證書
# openssl ca -in httpd.crl -out httpd.crt
# scp httpd.crt 172.16.18.5:/etc/httpd/ssl
(6)在httpd主機上安裝ssl模塊,並配置文件
# yum install mod_ssl -y
# cd /etc/httpd/conf.d/
# vim ssl.conf
(7)檢查配置文件、重啓服務並進行測試
文件配置OK,服務也可以正常啓動
通過測試,https能夠正常訪問虛擬主機www2.qq.com
二、在CentOS-7通過httpd-2.4實現上述要求
1、安裝httpd-2.4rpm包
# yum -y install httpd
# rpm -q httpd
httpd-2.4.6-40.el7.centos.x86_64
2、啓動httpd服務
# systemctl start httpd.service
# ss -tnl |grep 80
LISTEN 0 128 *:80
3、創建虛擬主機www1和www2
# vim /etc/httpd/conf.d/vhost1.conf
# vim /etc/httpd/conf.d/vhost2.conf
# mkdir -pv /data/web/{www1,www2} 創建資源目錄
# vim /data/web/www1/index.html 創建www1資源文件
<h1>www1.qq.com<h1>
# vim /data/web/www2/index.html 創建www2資源文件
<h1>www2.qq.com<h1>
# htpasswd -c -m /etc/httpd/conf/.htpasswd tom 創建用戶名
3、檢查配置文件,重新加載配置文件並測試
# httpd -t 檢查配置文件
# systemctl reload httpd.service
測試www1.qq.com能否訪問資源
測試www2.qq.com能否訪問資源
測試www1主機能否訪問server-status
需要身份驗證,輸入用戶名和密碼,能夠訪問
測試www2能否訪問server-status
測試172.16.18.9能否訪問www2.qq.com
通過測試172.16.18.9能夠訪問www1主機,訪問www2主機被拒絕
通過以上測試,已經完成了前三個要求
5、爲www2主機提供https服務
(1)爲www2主機申請證書
# (umask 066;openssl genrsa -out httpd.key 2048) 創建key密鑰文件
# openssl req -new -key httpd.key -days 365 -out httpd.crl
# scp httpd.crl 172.16.18.9:/tmp
CA爲httpd-2.4的www2頒發證書
# openssl ca -in httpd.crl -out /etc/pki/CA/certs/http.crt
# scp http.crt 172.16.18.3:/etc/httpd/ssl
(2)在CentOS-7上安裝ssl
# yum install mod_ssl -y
修改ssl配置文件
# vim /etc/httpd/conf.d/ssl.conf
檢查配置文件,重啓httpd服務
# httpd -t
# systemctl restart httpd.service
(3)測試https服務能否訪問www2
通過測試,爲www2提供https服務成功。
)