















1. 安装windows OpenSSL工具
下载地址:适用于 Windows 的 Win32/Win64 OpenSSL 安装程序 - Shining Light Productions
点击截图下载:

点击安装:


配置系统环境变量:

2. 执行生成命令

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.key -out localhost.crt
或者
openssl genpkey -algorithm RSA -out localhost.key
openssl req -new -key localhost.key -out csr.csr
openssl x509 -req -days 365 -in csr.csr -signkey localhost.key -out localhost.crt
参数说明:
在提示符下,输入证书信息
刚才命令启动位置找到证书:

执行如下命令检测私钥与证书是否匹配,如果两个结果一致,则表明私钥和证书是匹配的:
openssl x509 -noout -modulus -in localhost.crt | openssl md5 openssl rsa -noout -modulus -in localhost.key | openssl md5

或者:
生成RootCA.pem, RootCA.key 以及 RootCA.crt,RootCA是一个用例的名称,实际使用中可以把它为你自己要的名字。执行如下命令:
openssl req -x509 -nodes -new -sha256 -days 1024 -newkey rsa:2048 -keyout RootCA.key -out RootCA.pem -subj "/C=US/CN=Example-Root-CA" openssl x509 -outform pem -in RootCA.pem -out RootCA.crt




假设有两个本地机器域名num1.local和num2.local,这两个域名使用hosts文件将其指向127.0.0.1。
创建domains.ext列出所有的本地域名


authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment subjectAltName = @alt_names [alt_names] DNS.1 = localhost DNS.2 = num1.local DNS.3 = num2.local
生成localhost.key, localhost.csr以及localhost.crt文件:
openssl req -new -nodes -newkey rsa:2048 -keyout localhost-2.key -out localhost-2.csr -subj "/C=CN/ST=SiChuan/L=ChengDu/O=Example-Certificates/CN=localhost-2.local" openssl x509 -req -sha256 -days 1024 -in localhost-2.csr -CA RootCA.pem -CAkey RootCA.key -CAcreateserial -extfile domains.ext -out localhost-2.crt




3. 使用证书
在Nginx的nginx.conf中配置如下,注意红色字体,默认HTTPS使用的是443端口,以及配置证书以及秘钥,启动Nginx后访问https://localhost即可生效:
server {
listen 443 ssl;
server_name localhost;
root "D:/phpstudy_pro/WWW";
ssl_certificate D:/phpstudy_pro/Extensions/Nginx1.21.0/conf/ssl/localhost.crt;
ssl_certificate_key D:/phpstudy_pro/Extensions/Nginx1.21.0/conf/ssl/localhost.key;
}



注:也可使用localhost-2.crt跟localhost-2.key
4. 信任证书,信任本地CA
站点加载有关自签名证书时会有警告。为了获得绿色锁,必须将新的本地CA添加到受信任的根证书颁发机构。
Windows 10: Chrome, IE11 以及 Edge
Windows 10是能识别.crt文件,右键RootCA.crt文件,然后执行安装,就会弹出导入证书的窗口。


这样Chrome,IE11以及Edge就会显示绿色锁。-好像无用
Window10:Firefox
对于Firefox,有两种方法来让浏览器信任我们自签名的证书:
1、在浏览器的地址栏,打开about:config,把security.enterprise_roots.enabled 设置为true。
2、在浏览器导入证书:在地址栏输入about:preferences#privacy > 证书(Certificats) > 导入(Import) > RootCA.pem > 确定网站
打开证书管理器:
按 Win + R,输入 certmgr.msc,然后按回车。
导入 CA 证书:
在左侧导航栏中,展开 “受信任的根证书颁发机构”。
右键点击 “证书”,选择 “所有任务” -> “导入”。
按照向导选择本地 CA 的根证书文件(例如 ca.crt),然后完成导入。
验证:
在 “受信任的根证书颁发机构” -> “证书” 中,检查是否已成功添加。
使用 certutil 导入证书:
打开命令提示符(以管理员身份运行),然后运行以下命令:certutil -addstore -f "Root" ca.crt
验证:
运行以下命令列出受信任的根证书:certutil -store Root




此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。