实验环境:
CentOS 7
httpd-2.4.6-40.el7.centos.x86_64
需要
openssl,mod_ssl
安装(直接yum了):
[root@localhost pki]# yum install httpd openssl mod_ssl -y
安装完之后可以访问一下本机,测试httpd服务器
我的是 http://192.168.1.102/index.html
接下来修改/etc/httpd/conf.d/ssl.conf
在<VirtualHost _default_:443>下找到
# General setup for the virtual host, inherited from global configuration
DocumentRoot "/var/www/html"
ServerName www.example.com:443
</VirtualHost>
将上面两行取消注释,保存。
重启httpd服务即可。
就可以使用 https://192.168.1.102/index.html访问了。
当然,初次访问会提示不受信任的证书,这是因为没有CA签发证书。
实验环境解决办法,创建私有CA,并给httpd签发证书或选择添加信任,继续访问。
签发证书步骤如下:
(1) 生成私钥;
# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 4096) |
(2) 生成自签证书;
# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365 |
-new:生成新证书签署请求;
-x509:生成自签格式证书,专用于创建私有CA时;
-key:生成请求时用到的私有文件路径;
-out:生成的请求文件路径;如果自签操作将直接生成签署过的证书;
-days:证书的有效时长,单位是day;
(3) 为CA提供所需的目录及文件;
# mkdir -pv /etc/pki/CA/{certs,crl,newcerts} # touch /etc/pki/CA/{serial,index.txt} # echo 01 > /etc/pki/CA/serial |
此主机即是一台CA了。
(4)用到证书的主机(httpd主机)生成私钥;
# mkdir /etc/httpd/ssl # cd /etc/httpd/ssl # (umask 077; openssl genrsa -out /etc/httpd/ssl/httpd.key 2048) |
(5) 生成证书签署请求
# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr -days 365 |
(6) 在CA主机上签署证书;
# openssl ca -in /etc/httpd/ssl/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(0x2) Validity Not Before: Apr 10 15:45:54 2016 GMT Not After : Apr 10 15:45:54 2017 GMT Subject: countryName = CN stateOrProvinceName = beijing organizationName = ops organizationalUnitName = ops commonName = ops.com emailAddress = admin@ops.com X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 08:A3:DD:98:D3:E0:42:58:5E:B7:24:43:6C:3D:B1:D8:02:34:16:46 X509v3 Authority Key Identifier: keyid:75:63:44:2C:46:80:2F:84:CE:EF:C6:F1:F2:E7:75:2E:EF:17:37:C2 Certificate is to be certified until Apr 10 15:45:54 2017 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 |
(7)查看证书中的信息:
# openssl x509 -in /etc/pki/CA/certs/httpd.crt -noout -serial -subject
注:由于为实验环境,还需将此证书导入到浏览器中方可。
另还需注意本地hosts文件。
本文出自 “我的学习笔记” 博客,请务必保留此出处http://zhaoyongtao.blog.51cto.com/10955972/1770206