x509证书一般会用到三类文,key,csr,crt。
Key 是私用密钥openssl格,通常是rsa算法。
Csr 是证书请求文件,用于申请证书。在制作csr文件的时,必须使用自己的私钥来签署申,还可以设定一个密钥。
crt是CA认证后的证书文,(windows下面的,其实是crt),签署人用自己的key给你签署的凭证。
1.key的生成
openssl genrsa -des3 -out server.key 2048
这样是生成rsa私钥,des3算法,openssl格式,2048位强度。server.key是密钥文件名。为了生成这样的密钥,需要一个至少四位的密码。可以通过以下方法生成没有密码的key:
openssl rsa -in server.key -out server.key
server.key就是没有密码的版本了。
2. 生成CA的crt
openssl req -new -x509 -key server.key -out ca.crt -days 1095
生成的ca.crt文件是用来签署下面的server.csr文件。
3. csr的生成方法
openssl req -new -key server.key -out server.csr
需要依次输入国家,地区,组织,email。最重要的是有一个common name,可以写你的名字或者域名。如果为了https申请,这个必须和域名吻合,否则会引发浏览器警报。生成的csr文件交给CA签名后形成服务端自己的证书。
4. crt生成方法
CSR文件必须有CA的签名才可形成证书。
openssl x509 -req -days 1095 -in server.csr -CA ca.crt -CAkey server.key -CAcreateserial -out server.crt
输入key的密钥后,完成证书生成。-CA选项指明用于被签名的csr证书,-CAkey选项指明用于签名的密钥,-CAserial指明序列号文件,而-CAcreateserial指明文件不存在时自动生成。
最后生成了私用密钥:server.key和自己认证的SSL证书:server.crt
证书合并:
cat server.key server.crt > server.pem
参数说明:
-x509 显示证书和签名工具
-days 证书的有效期
-sha1 证书加密算法
-newkey rsa:1024 创建一个新key,1024表示公钥长度为1024bits
5.指定证书路径
[root@www ~]# vi /etc/httpd/conf.d/ssl.conf
change as below:
# General setup for the virtual host, inherited from global configuration
DocumentRoot “/var/www/html”
ServerName www.hello.site:443
# Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate. If
# the certificate is encrypted, then you will be prompted for a
# pass phrase. Note that a kill -HUP will prompt again. A new
# certificate can be generated using the genkey(1) command.
SSLCertificateFile /etc/pki/tls/mycert/server.crt
# Server Private Key:
# If the key is not combined with the certificate, use this
# directive to point at the key file. Keep in mind that if
# you’ve both a RSA and a DSA private key you can configure
# both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateKeyFile /etc/pki/tls/mycert/server.key
or
请把 dvroot.cer 或 ovroot.cer、server.cer和 server.key 这三个文件保存到同一个目录下,例如放到/etc/ssl/crt/目录下。
用文本编辑器打开 httpd.conf 并更新以下内容
<VirtualHost xxx.xxx.xxx.xxx:443>
DocumentRoot “/var/www/html”
ServerName cn.globalsign.com
SSLEngine on
SSLCertificateFile /etc/ssl/crt/server.crt //公钥文件(GlobalSign颁发)
SSLCertificateKeyFile /etc/ssl/crt/server.key . //私钥文件
SSLCertificateChainFile /etc/ssl/crt/dvroot.crt //中级证书
</VirtualHost>
按照以上的步骤配置完成后,重新启动Apache(如果有设置server.key私钥密码,这时会提示输入) 后就可以使用 https://www.domain.com 来访问了。