构建HTTPS安全网站

构建HTTPS安全网站

问题

本案例基于上一章的CA服务器,要求为Web服务器提供加密通信支持,主要完成以下任务操作:
1)使用openssl为网站服务器创建CSR证书签发申请
2)将CSR申请提交给CA服务器签署,下载签发好的数字证书文件
3)配置实现强制跳转的HTTPS网站服务

方案

采用两台RHEL6虚拟机,其中svr5作为CA服务器,而www作为测试用的网站服务器。另外准备一台pc120作为访问网站的Windows测试机,如图所示。
在这里插入图片描述

步骤

实现此案例需要按照如下步骤进行。
步骤一:使用openssl为网站服务器创建CSR证书签发申请
1) 在www服务器上,先使用openssl创建私钥文件
由于此例中的私钥主要用于Web自动加密通信,为了方便起见,可以不设置私钥口令,否则在启动httpd服务时需要验证私钥口令。

[root@www ~]# cd /etc/pki/tls/private/ pc
[root@www private]# openssl genrsa 2048 > www.key  	//建立无口令保护的私钥文件
Generating RSA private key, 2048 bit long modulus
..........+++
........+++
e is 65537 (0x10001)
[root@www private]# chmod 600 www.key

2)基于新建的密钥信息创建CSR证书签发请求
注意根据提示设置的国家、省、市、组织信息要与CA根证书的设置保持一致。

[root@www private]# \
> openssl req -new -key www.key > ~/www.csr  		//建立CSR证书请求文件
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) []:Beijing
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:Tarena Technology Ltd
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:www.tarena.com
Email Address []:webmaster@tarena.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@www private]#

步骤二:将CSR申请提交给CA服务器签署,下载签发好的数字证书文件
1) 在CA服务器svr5上,获得www服务器的CSR证书签发请求
可以采用SCP等方式拷贝过来,正式签发之前,可以先确认证书请求的信息内容。

[root@svr5 ~]# scp 192.168.4.120:/root/www.csr ./
root@192.168.4.120's password:
www.csr                                       100% 1058     1.0KB/s   00:00 
[root@svr5 ~]# openssl req -in www.csr -noout –text  		//查看证书请求
Certificate Request:
    Data:
        Version: 0 (0x0)
        Subject: C=CN, ST=Beijing, L=Beijing, O=Tarena Technology Ltd, CN=www.tarena.com/emailAddress=webmaster@tarena.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                .. ..
        Attributes:
            a0:00
Signature Algorithm: sha1WithRSAEncryption
            .. ..

2)在CA服务器svr5上,签署并发布证书文件
正式签署www服务器的CSR请求,生成证书文件。然后将证书文件发放给www服务器,此例中仍通过httpd服务提供下载。

[root@svr5 ~]# cd /etc/pki/CA/certs/
[root@svr5 certs]# openssl ca -in ~/www.csr > www.crt  	//签署证书
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/my-ca.key:  		//验证私钥口令
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 6 (0x6)
        Validity
            Not Before: Aug 19 06:48:14 2013 GMT
            Not After : Aug 19 06:48:14 2014 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Beijing
            organizationName          = Tarena Technology Ltd
            commonName                = www.tarena.com
            emailAddress              = webmaster@tarena.com
            .. ..
Certificate is to be certified until Aug 19 06:48:14 2014 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
[root@svr5 certs]# cp www.crt /var/www/html/certs/  	//复制到Web下载目录

3)在www服务器上,下载CA服务器签发好的证书文件

[root@www private]# cd /etc/pki/tls/certs/
[root@www certs]# wget http://192.168.4.5/certs/www.crt
.. ..
2015-05-17 14:55:59 (270 MB/s) - 已保存 “www.crt” [4631/4631])

步骤三:配置实现强制跳转的HTTPS网站服务
1)在www服务器上,确认准备的私钥、证书文件位置

[root@www ~]# ls -lh /etc/pki/tls/private/www.key
-rw-------. 1 root root 1.7K 8月  19 14:13 /etc/pki/tls/private/www.key
[root@www ~]# ls -lh /etc/pki/tls/certs/www.crt
-rw-r--r--. 1 root root 4.6K 8月  19 14:51 /etc/pki/tls/certs/www.crt

2) 调整httpd服务配置,以便启用SSL引擎
此例中以RHEL6自带的httpd网站服务为例,需要安装mod_ssl模块包。然后修改/etc/httpd/conf.d/ssl.conf文件,添加启用SSL的设置,以及将http://访问自动重定向(跳转)为https://访问的设置。

[root@www ~]# yum -y install httpd mod_ssl
.. ..
[root@www ~]# vim /etc/httpd/conf.d/ssl.conf
LoadModule ssl_module modules/mod_ssl.so
Listen 443
.. ..
<VirtualHost  _default _:443>
    SSLEngine on
    .. ..
    SSLCertificateFile "/etc/pki/tls/certs/www.crt"
    SSLCertificateKeyFile "/etc/pki/tls/private/www.key“
</VirtualHost>
.. ..
RewriteEngine on  									//启用地址重写引擎
RewriteCond %{SERVER_PORT} !^443$  					//重写条件为非443端口的访问
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]  	//将URL重写为https://开头
[root@www ~]# vim /etc/httpd/conf/httpd.conf
.. ..  										//确认ssl.conf能够被主配置所调用
Include conf.d/*.conf
[root@www ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:                                           [确定]
[root@www ~]# netstat -anpt | grep httpd
tcp        0      0 :::80           :::*        LISTEN      31645/httpd
tcp        0      0 :::443          :::*        LISTEN      31645/httpd

3)访问HTTPS站点进行测试
在测试机pc120上,可以从浏览器直接访问https://www.tarena.com,或者访问http://www.tarena.com自动跳转为https://www.tarena.com。首次访问时会出现安全提示,单击“确定”即可,如图所示。

在这里插入图片描述
另外,由于这个网站的证书是企业自建CA颁发的,而并不是由互联网中合法、可信的CA机构所颁发,因此会出现关于证书问题的安全警报,如图所示,单击“是”即可。

在这里插入图片描述

成功接收证书、信任证书以后,就可以看到网页内容了;而且,从地址栏里可以看到访问的协议为HTTPS,如图所示,说明已经支持加密通信了。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值