废话不多讲,我们直入正题。
首先我假设你的系统已经安装了openssl。使用openssl version -a即可查看当前安装的版本。没有安装的请自行百度安装openssl教程。
1.创建目录并授予权限:

进入ca目录:
cd ca
2. 为服务器端和客户端准备公钥、私钥:
① 生成服务器端私钥:
openssl genrsa -out server.key 1024

② 生成服务器端公钥:
openssl rsa -in server.key -pubout -out server.pem

③ 生成客户端私钥:
openssl genrsa -out client.key 1024

④ 生成客户端公钥:
openssl rsa -in client.key -pubout -out client.pem

3.生成 CA 证书
① 生成 CA 私钥:
openssl genrsa -out ca.key 1024

② 生成请求文件:
openssl req -new -key ca.key -out ca.csr

注意,这里的 Organization Name (eg, company) [Internet Widgits Pty Ltd]: 后面生成客户端和服务器端证书的时候也需要填写,不要写成一样的!!!
可以随意写如:myca, myserver, myclient。
然后 Common Name (e.g. server FQDN or YOUR name) []: 这一项,是最后可以访问的域名,我这里为了方便测试,写成 localhost
③ 生成ca证书:
openssl x509 -req -in ca.csr -signkey ca.key -out ca.crt

4.生成服务器端证书和客户端证书
① 服务器端需要向 CA 机构申请签名证书,在申请签名证书之前依然是创建自己的 CSR 文件:
openssl req -new -key server.key -out server.csr

② 向自己的 CA 机构申请证书,签名过程需要 CA 的证书和私钥参与,最终颁发一个带有 CA 签名的证书:
openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt

③ 生成client端csr:
openssl req -new -key client.key -out client.csr

④ client 端得到 CA 签名生成client端证书:
openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in client.csr -out client.crt

我们的 ca 文件夹下已经有如下内容了:

以上为全部内容
补充,
nginx服务端配置的时候使用的key和pem格式的证书,所以上述中的pem格式的server证书不是最终结果
openssl x509 -in server.crt -out server-root.pem
网友评论