一、环境介绍
操作系统:
Red Hat Enterprise Linux Server release 7.1 (Maipo)
openssl 版本:
openssl-1.0.1e-51.el7_2.5.x86_64
二、生成自签名证书过程
# 建立工作目录
mkdir /opt/ssl_san && cd /opt/ssl_san
mkdir ca cert && cd ca
# 生成CA私钥
openssl genrsa -out ca.key 2048
# 生成CA证书
openssl req -new -x509 -days 36500 -key ca.key -out ca.crt -subj "/C=CN/ST=Shanghai/L=Shanghai/O=killingwolf/OU=killingwolf/emailAddress=killingwolf@qq.com"
# 生成nginx私钥
cd ../cert/
openssl genrsa -out nginx.key 2048
# 生成san证书配置文件
cat >san.cfg<<EOF
[ req ]
default_bits = 2048
default_keyfile = nginx.key # name of the keyfile
distinguished_name = req_distinguished_name
req_extensions = req_ext
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = CN
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = Shanghai
localityName = Locality Name (eg, city)
localityName_default = Shanghai
organizationName = Organization Name (eg, company)
organizationName_default = killingwolf
commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_max = 64
organizationalUnitName = Organizational Unit Name (eg, section)
organizationalUnitName_default = killingwolf
commonName = Common Name (eg, YOUR name)
commonName_default = www.killingwolf.com
commonName_max = 64
emailAddress = Email Address
emailAddress_max = 64
emailAddress_default = killingwolf@qq.com
[ req_ext ]
subjectAltName = @alt_names
[alt_names]
DNS.1 = killingwolf.com
DNS.2 = *.killingwolf.com
DNS.3 = *.ben.com
EOF
# 生成请求(csr)文件,如果要购买权威机构证书的话,将csr文件提交给权威机构即可。
openssl req -new -key nginx.key -out nginx.csr -config san.cfg -subj "/C=CN/ST=Shanghai/L=Shanghai/O=killingwolf/OU=killingwolf/CN=ww.killingwolf.com"
# 检查生成的请求文件。
openssl req -in nginx.csr -text -noout | grep DNS
# 用自己的CA证书签名
openssl x509 -req -in nginx.csr -CA ../ca/ca.crt -CAkey ../ca/ca.key -CAcreateserial -out nginx.crt -extensions req_ext -extfile san.cfg
# 检查签名的证书
openssl x509 -in nginx.crt -text -noout | grep DNS
三、如何使用生成的证书?
3.1 将自己的ca.crt文件导入到浏览器受信任证书列表里。
3.2 将nginx.crt, nginx.key配置到nginx/apache等服务器上。