建立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服务成功。
)