文章目录
平常开发过程中需要服务enable https, 涉及到CA证书和服务端证书秘钥, 但是由于openssl命令平时使用不多,每一次都要去搜索一下怎么用,于是在此处记录一下快速生成tls证书相关文件的openssl命令,使用时方便查询
openssl指令快速生成证书
这里生成证书带san, 适合生成一套证书文件供所有服务使用,假设需要证书的网站二级域名是example.com
, 使用如下指令生成ca证书
openssl req -new -x509 -keyout ca.key -out ca.crt -days 3650 -subj '/C=CN/ST=Shanghai/L=Shanghai/O={your org}/OU={your org unit}/CN=example.com' \
-addext 'subjectAltName=DNS:example.com,DNS:*.example.com' -nodes
使用上述指令生成的ca.key
和 ca.crt
生成服务端证书service.crt
和私钥service.key
openssl req -new -keyout service.key -out service.csr \
-subj '/C=CN/ST=Shanghai/L=Shanghai/O={your org}/OU={your org uinit}/CN=example.com' -nodes \
&& openssl x509 -req -in service.csr -out service.crt -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650 -extfile \
<(printf "subjectAltName=DNS:example.com,DNS:*.example.com")
这样将service.crt
和service.key
配置给任何以*.example.com
作为域名的服务, 将ca.crt
配置给浏览器且设置为受信任的证书即可实现浏览器无警告
的以https协议访问服务器.