Apache/httpd(13)之给httpd服务整上https

Apache/httpd(13)之给httpd服务整上https

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:

首先,请参考【OpenSSL(2)之PKI简述及建立私有CA并签发证书】在 httpd 主机上申请证书,这里我使用的 CA 主机为 10.0.1.201,httpd 服务主机地址为 10.0.1.200
这里我已经在 httpd 主机上已经申请好了证书,如下:

$ ls /etc/httpd/ssl/
httpd.crt  httpd.csr  httpd.key

并且 CA 主机上的证书文件如下:

$ ls /etc/pki/CA/cacert.pem 
/etc/pki/CA/cacert.pem

做好上述准备,那我们就开始设定 httpd 服务了。


1、检查 httpd 是否装载了 ssl 模块:

$ httpd -M | grep ssl

2、如果未装载 ssl 模块,可执行下面命令安装为 httpd 提供 ssl 支持的程序包:

$ yum install mod_ssl -y
$ httpd -M | grep ssl   
 ssl_module (shared)

3、安装完 mod_ssl 模块后会在 /etc/httpd/conf.d 下生成 ssl.conf 文件,编辑这个文件:

$ cd /etc/httpd/conf.d && cp ssl.conf{,.bak}
$ vim ssl.conf
# 指定工作目录
DocumentRoot "/code/httpd"
# 指定使用 https 的域名
ServerName www.zze.com
# 监听 443 端口
<VirtualHost *:443>
# 指定证书文件
SSLCertificateFile /etc/httpd/ssl/httpd.crt
# 指定与证书文件匹配的私钥
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key

4、创建对应工作目录并编写测试 HTML 文件:

$ mkdir /code/httpd -p
$ echo '<h1>TEST HTTPS WITH HTTPD~~~</h1>' > /code/httpd/index.html
$ chown -R apache.apache /code/httpd

5、重启 httpd 服务并检查 443 端口是否处于监听状态:

$ systemctl restart httpd
$ ss -tanl | grep :443
LISTEN     0      128         :::443                     :::*

6、在另一台主机上(我这里是 10.0.1.202)测试使用 openssl 客户端工具测试基于 https 访问相应主机:

# openssl s_client [-connect host:port] [-cert filename] [-CApath directory] [-CAfile filename]
#	[-connect host:port]:指定连接的主机加端口;
#	[-cert filename]:指定客户端的证书文件(如果服务端需要验证客户端身份);
#	[-CApath directory]:指明 CA 证书的存放目录;
#	[-CAfile filename]:指明 CA 证书文件;
# 这里为尽量模拟实际环境将 CA 证书放在客户端,我将 CA 证书复制到了 10.0.1.202 主机上的 /root 目录,以 10.0.1.202 作为客户端并在客户端执行下面命令:
$ openssl s_client -connect 10.0.1.200:443 -CAfile /root/cacert.pem
CONNECTED(00000003)
depth=1 C = CN, ST = Guangdong, L = Shenzhen, O = zze, OU = Ops, CN = ca.zze.xyz, emailAddress = zhangzhongen326@gmail.com
verify return:1
depth=0 C = CN, ST = Guangdong, O = zze, OU = Ops, CN = www.zze.com, emailAddress = 632404164@qq.com
verify return:1
---
Certificate chain
 0 s:/C=CN/ST=Guangdong/O=zze/OU=Ops/CN=www.zze.com/emailAddress=632404164@qq.com
   i:/C=CN/ST=Guangdong/L=Shenzhen/O=zze/OU=Ops/CN=ca.zze.xyz/emailAddress=zhangzhongen326@gmail.com
---
Server certificate
-----BEGIN CERTIFICATE-----
MIID+jCCAuKgAwIBAgIBATANBgkqhkiG9w0BAQUFADCBjzELMAkGA1UEBhMCQ04x
EjAQBgNVBAgMCUd1YW5nZG9uZzERMA8GA1UEBwwIU2hlbnpoZW4xDDAKBgNVBAoM
A3p6ZTEMMAoGA1UECwwDT3BzMRMwEQYDVQQDDApjYS56emUueHl6MSgwJgYJKoZI
hvcNAQkBFhl6aGFuZ3pob25nZW4zMjZAZ21haWwuY29tMB4XDTIwMDMxMjA5MDk0
N1oXDTIxMDMxMjA5MDk0N1owdDELMAkGA1UEBhMCQ04xEjAQBgNVBAgMCUd1YW5n
ZG9uZzEMMAoGA1UECgwDenplMQwwCgYDVQQLDANPcHMxFDASBgNVBAMMC3d3dy56
emUuY29tMR8wHQYJKoZIhvcNAQkBFhA2MzI0MDQxNjRAcXEuY29tMIIBIjANBgkq
hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuYdlhD58BoKbU+OvhGXiisQELBtXwpzP
HhjNfZn0Y6Mb/zgOacaXrxlIKY0I+k2g12NZFFocPElU7MN3QoCBBpBRUe1IlcbK
F94YFoRKipA9RLfIfe1HJ1bvCmwyQ8o4BhDsV4O344iseh7nVD5nSfSadfbJ5sMz
mB1O4sgsVd7qbNYBavcHEB1hYc+divPaTHCRLgZbvi5p8cYV+7ftW71q9l/YXLvF
LjzEno6SzGdgfqLsEfy04XHIw4G5bSur8r1AlkDZN5pZHl15KOltxqX+W5W77fie
aZ+nsXMCXw1GiueJ5LsWohqi1PpFVd1/N8AZ9cknAmrPCVAGWEZN4QIDAQABo3sw
eTAJBgNVHRMEAjAAMCwGCWCGSAGG+EIBDQQfFh1PcGVuU1NMIEdlbmVyYXRlZCBD
ZXJ0aWZpY2F0ZTAdBgNVHQ4EFgQUNOcr5u/53UYOG2fog6uvch6lFl4wHwYDVR0j
BBgwFoAUYHa+4tpXibbnkbp5i79zdGub66YwDQYJKoZIhvcNAQEFBQADggEBAOVE
OJCtjYe1fEgCTA6DadaqkaxFzZ33r3MnRufhOwhGDHfKZ0QJEuDlHDH2pZopztM7
UfFRju254HAr5mYocqSSbbt8VMskDZjHUgww88fofgqTMHSozsJlgTPmkntp57Wy
Sq8c94yBcHCOQKA+C77XnBzR1rqs8X8LaU4X3/nIWoYtKDCtIflVcS0wGBJ73wJj
uBfOq3qEZwpbHLqDaiear5HQL4rSTrnZRzAjj/X9MdbXf5P76H5mwzhPIY9inxw/
1AB4t3IqOowShWak+qe56M4+d1jjqMK4Vckv1u/n78X+lz+YJJhuAUrtGP+0FyHC
enuVkdlWsBE/mXL78GE=
-----END CERTIFICATE-----
subject=/C=CN/ST=Guangdong/O=zze/OU=Ops/CN=www.zze.com/emailAddress=632404164@qq.com
issuer=/C=CN/ST=Guangdong/L=Shenzhen/O=zze/OU=Ops/CN=ca.zze.xyz/emailAddress=zhangzhongen326@gmail.com
---
No client certificate CA names sent
Peer signing digest: SHA512
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 1713 bytes and written 415 bytes
---
New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES256-GCM-SHA384
    Session-ID: 20DD619F3955996E2CCF12C12BB7E5B3EE43DD1E4A8DEE116EDC54AD0E2A001D
    Session-ID-ctx: 
    Master-Key: 443600470E1BF2AB8CDA7C8178018A3436F81415A3919B5731B5DE0FE518020D70A225D33FC8D634DF05207880CD4CD7
    Key-Arg   : None
    Krb5 Principal: None
    PSK identity: None
    PSK identity hint: None
    TLS session ticket lifetime hint: 300 (seconds)
    TLS session ticket:
    0000 - 12 88 f9 27 03 ea 5c 21-fe 88 ff 70 6d 93 98 c3   ...'..\!...pm...
    0010 - d3 3c ce af ae 55 22 0b-24 b7 f9 42 83 7d 52 8e   .<...U".$..B.}R.
    0020 - c6 af 9c 2a 90 45 0e 0b-9b 54 80 0e 3d 8c 3c 63   ...*.E...T..=.<c
    0030 - 0b 12 5f 00 41 75 dc 8c-9a 11 9d 26 c6 87 8f 53   .._.Au.....&...S
    0040 - f6 c6 26 6c ed 44 9b 89-67 bb 97 3e d8 98 e3 e6   ..&l.D..g..>....
    0050 - 28 a4 71 0b b6 ba 1e 0f-c4 07 65 15 59 36 fe 46   (.q.......e.Y6.F
    0060 - d5 5d 83 a2 45 4f e3 8e-0c 8e f1 b3 4f ef a3 5c   .]..EO......O..\
    0070 - 43 96 6e 75 cd 22 e2 d3-9d a2 f5 1c 0b 8d d7 7f   C.nu."..........
    0080 - 40 be 35 03 e2 04 fd 4c-aa fd 2d 15 96 53 42 56   @.5....L..-..SBV
    0090 - e3 94 72 80 d1 3a 92 b8-cd 5c 24 51 5b 8d 96 8e   ..r..:...\$Q[...
    00a0 - c7 4f b7 f5 b4 5f fe 81-4f aa 00 1d 5e 59 4f f8   .O..._..O...^YO.
    00b0 - fc 62 1b a9 45 a5 f1 fd-ef 8a de 01 5e 1f 9c b6   .b..E.......^...

    Start Time: 1584006257
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---

出现上述信息则说明此时我们通过 openssl s_client 可以正常使用 https 与 httpd 服务建立连接。

7、那我们测试使用宿主机的浏览器访问一下(需先修改宿主机 hosts 映射以保证能够通过域名的方式访问到 httpd 服务):
image.png

8、可以看到目前 httpd 服务向我们发送的证书是不受信任的,因为我们宿主机本地并没有对应的 CA 证书能验证该证书,那我们就要在宿主机安装 CA 证书啦,安装证书有如下步骤:

  • 把 CA 主机中的 /etc/pki/CA/cacert.pem 复制到宿主机一份,并改名为 cacert.crt,如果是宿主机是 Windows 会发现它自己就带上 ICON。
    image
  • 打开 IE 浏览器,到如下位置导入证书:
    image.png

9、直接使用 IE 浏览器访问 https://www.zze.com
image.png

可以看到可以正常访问了,经测试如果是其它浏览器可能依旧是不信任的状态哦,这是由于各浏览器对自建证书的校验机制可能有所不同,我们这里着重于了解 https 搭建流程,这个就不要较真啦~~~

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://www.zze.xyz/archives/apachehttpd13.html

Buy me a cup of coffee ☕.