1、建立httpd服务器(基于编译的方式进行),要求:
提供两个基于名称的虚拟主机:
(a)www1.itab.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1.err,访问日志为/var/log/httpd/www1.access;
(b)www2.itab.com,页面文件目录为/web/vhosts/www2;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/www2.access;
(c)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名;
(d)通过www1.itab.com/server-status输出httpd工作状态相关信息,且只允许提供帐号密码才能访问(status:status);
(e) www1不允许192.168.1.0/24网络中的主机访问;
2、为上面的的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点;
(1)要求使用证书认证,证书中要求使用的国家(CN)、州(HA)、城市(ZZ)和组织(iTab);
(2)设置部门为Ops,主机名为www2.itab.com,邮件为[email protected];
现在分别使用httpd-2.2和httpd-2.4搭建符合以上要求的HTTP/HTTPS服务。
httpd-2.2
(1) 使用yum安装httpd服务程序。
[root@web ~]# yum -y install httpd
(2) 要想使用虚拟主机,必须先注释掉中心主机的文档根路径(DocRoot)。
[root@web ~]# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
(3) 在/etc/httpd/conf.d目录下创建vhost.conf文件,专门用于配置虚拟主机。
NameVirtualHost 192.168.10.128:80
<VirtualHost 192.168.10.128:80> # 第一台虚拟主机配置
ServerName www1.itab.com # 第一台虚拟主机的主机名
DocumentRoot "/web/vhosts/www1" # 第一台虚拟主机的DocRoot
ErrorLog logs/www1.err # 错误日志路径
CustomLog logs/www1.access combined # 访问日志路径
<Directory "/web/vhosts/www1"> # 基于IP地址做访问控制
Order allow,deny
Deny from 192.168.1 # 不允许192.168.1.0/24网络中的主机访问
</Directory>
<Location /server-status> # 第一台虚拟主机开启server-status工作状态输出功能
SetHandler server-status
AuthType Basic # 基于账号密码做控制
AuthName "Enter your username and password." # 认证提示
AuthUserFile "/etc/httpd/.htpasswd" # 存放账号密码的文件路径
Require user status # 只允许status用户登录
</Location>
</VirtualHost>
<VirtualHost "192.168.10.128:80"> # 第二台虚拟主机配置
ServerName www2.itab.com # 第二台虚拟主机的主机名
DocumentRoot "/web/vhosts/www2" # 第二台虚拟主机的DocRoot
ErrorLog logs/www2.err # 错误日志路径
CustomLog logs/www2.access combined # 访问日志路径
</VirtualHost>
(4) 创建虚拟主机的文档根目录,并为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名。
[root@web ~]# mkdir -pv /web/vhosts/{www1,www2}
mkdir: created directory `/web'
mkdir: created directory `/web/vhosts'
mkdir: created directory `/web/vhosts/www1'
mkdir: created directory `/web/vhosts/www2'
[root@web ~]# cat /web/vhosts/www1/index.html
<h1> www1.itab.com </h1>
[root@web ~]# cat /web/vhosts/www2/index.html
<h1> www2.itab.com </h1>
(5) 设置站点主页面。
[root@web ~]# vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.html
(6) 创建访问status页面的账号(status)和密码(status)。
[root@web ~]# htpasswd -cm /etc/httpd/.htpasswd status
New password:
Re-type new password:
Adding password for user status
(7) 检查语法错误,如果没有错误,则启动http服务。
[root@web ~]# httpd -t
Syntax OK
[root@web ~]# service httpd start
Starting httpd: [ OK ]
(8) 查看httpd是否已经监听在80端口。
[root@web ~]# ss -tnl | grep 80
LISTEN 0 128 :::80 :::*
(9) 测试。
9.1 测试status页面。
使用192.168.10.140的主机测试,如下。
[root@test1 ~]# elinks www1.itab.com/server-status
打开页面如下。
9.2 测试两个虚拟主机的页面。
同样使用192.168.10.140这台主机来测试,如下。
[root@test1 ~]# elinks www1.itab.com
访问www1.itab.com时,打开页面如下。
[root@test1 ~]# elinks www2.itab.com
访问www2.itab.com时,打开页面如下。
对于192.168.1.0/24网段的主机,则无法访问www1.itab.com,其打开页面如下。
(10) 现在为上面的的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点。
为了方便,这里通过建立私有CA来提供服务器证书。CA主机和Webf服务器主机如下:
CA主机:192.168.10.140
Web服务器主机:192.168.10.128
10.1 在CA主机(192.168.10.128上)建立私有CA
① CA生成私钥。
[root@test1 ~]# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
② 生成自签证书。
[root@test1 ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:iTab
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:ca.itab.com
Email Address []:[email protected]
③ 为CA提供所需的目录及文件。
[root@test1 ~]# mkdir -pv /etc/pki/CA/{certs,crl,newcerts}
[root@test1 ~]# touch /etc/pki/CA/{serial,index.txt}
[root@test1 ~]# echo 01 > /etc/pki/CA/serial
10.2 在Web服务器主机(192.168.10.128)上创建证书签署请求。
① Web服务器主机生成私钥。
[root@web ~]# (umask 077; openssl genrsa -out /etc/httpd/ssl/httpd.key 2048)
② Web服务器生成证书签署请求。
[root@web ~]# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:iTab
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:www2.itab.com
Email Address []:[email protected]
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
③ 通过可靠方式将证书签署请求发送给CA主机。这里为了演示方便,所以直接通过scp命令传送。
[root@web ~]# scp /etc/httpd/ssl/httpd.csr [email protected]:/tmp/
10.3 在CA主机(192.168.10.140)上签署证书请求。
① CA主机为Web服务器签证。
[root@test1 ~]# openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: May 21 13:50:38 2017 GMT
Not After : May 21 13:50:38 2018 GMT
Subject:
countryName = CN
stateOrProvinceName = HA
organizationName = iTab
organizationalUnitName = Ops
commonName = www2.itab.com
emailAddress = [email protected]
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
C9:0A:4A:B8:2C:67:8A:1C:68:D5:1E:F6:40:57:21:BD:48:75:D5:75
X509v3 Authority Key Identifier:
keyid:81:F9:59:3E:F7:7C:8C:A4:1A:6B:7F:6F:9E:4F:8B:A6:18:A5:20:30
Certificate is to be certified until May 21 13:50:38 2018 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
② CA主机将已签署的证书通过可靠方式传送给该Webf服务器。这里同样使用scp命令。
[root@test1 ~]# scp /etc/pki/CA/certs/httpd.crt [email protected]:/etc/httpd/ssl/
(11) Web服务器在已经得到CA签署的证书了,现在开始配置HTTPS服务,首先安装mod_ssl模块。
[root@web ~]# yum install mod_ssl
查看ssl_module是否已经加载。
[root@web ~]# httpd -M | grep ssl_module
ssl_module (shared) # ssl模块已经加载
Syntax OK
(12) 修改/etc/httpd/conf.d/ssl.conf配置文件。
[root@web ~]# vim /etc/httpd/conf.d/ssl.conf
# 修改以下配置项
<VirtualHost 192.168.10.128:443>
DocumentRoot "/web/vhosts/www2"
ServerName www2.itab.com
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
检查语法错误。
[root@web ~]# httpd -t
Syntax OK
重载服务。
[root@web ~]# service httpd reload
查看是否监听在443端口。
[root@web ~]# ss -tnl | grep 443
LISTEN 0 128 :::443 :::*
(13) 使用Web服务器本主机测试。
测试完毕,使用httpd-2.2搭建HTTP/HTTPS服务成功。接下来使用httpd-2.4搭建同样的HTTP/HTTPS服务。
httpd-2.4
httpd-2.4跟httpd-2.2不同在于任意目录下的页面只有显式授权才能被访问,因此,在配置虚拟主机访问控制时,如下所示。
[root@test1 ~]# cat /etc/httpd/conf.d/vhosts.conf
<VirtualHost 192.168.10.140:80>
ServerName www1.itab.com
DocumentRoot "/web/vhosts/www1"
ErrorLog logs/www1.err
CustomLog logs/www1.access combined
<Location /server-status>
SetHandler server-status
AuthType Basic
AuthName "Enter your username and password"
AuthUserFile "/etc/httpd/.htpasswd"
Require user status
</Location>
<Directory "/web/vhosts/www1">
<RequireAll> # 显示授权
Require all granted
Require not ip 192.168.10.128
</RequireAll>
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.140:80>
ServerName www2.itab.com
DocumentRoot "/web/vhosts/www2"
ErrorLog logs/www2.err
CustomLog logs/www2.access combined
<Directory "/web/vhosts/www2">
<RequireAll> # 显示授权
Require all granted
Require not ip 192.168.10.128
</RequireAll>
</Directory>
</VirtualHost>
还有在配置HTTPS时,也是需要显式授权才能被访问到,如下。
[root@test1 ~]# vim /etc/httpd/conf.d/ssl.conf
<VirtualHost 192.168.10.140:443>
DocumentRoot "/web/vhosts/www2"
ServerName www2.itab.com
<Directory "/web/vhosts/www2"> # 显式授权
Require all granted
</Directory>
SSLCertificateFile /etc/httpd/ssl/httpd2.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
</VirtualHost>