创建本地开发环境ssl证书
程序开发时,避免不了使用https加密通信,可以通过 openssl 工具来生成 ssl 证书,对于不懂的开发来说,openssl 工具是太难使用。有没有一个好用又简单的工具,可以试一试这个用Go语言写的命令行工具:mkcert,非常简单易用。
mkcert介绍:
mkcert
是一个用于生成本地自签名 SSL 证书的开源工具,项目基于 Golang
开发,可跨平台使用,不需要配置,支持多域名以及自动信任 CA。
github项目地址
https://github.com/FiloSottile/mkcert
安装
-
MacOS
brew install mkcert brew install nss # Firefox 浏览器支持
-
Linux
首先安装
certutil
sudo apt install libnss3-tools
或者
sudo yum install nss-tools
或者
sudo pacman -S nss
或者
sudo zypper install mozilla-nss-tools
然后可以使用 Linuxbrew 进行安装
brew install mkcert
或从源代码构建(需要Go 1.13+)
git clone https://github.com/FiloSottile/mkcert
cd mkcert
go build -ldflags "-X main.Version=$(git describe --tags)"
-
Windows
choco install mkcert
查看版本
mkcert -version
创建本地CA,将CA加入本地可信CA,如下图
mkcert - install
Using the local CA at "/Users/username/Library/Application Support/mkcert" ✨
Sudo password:
The local CA is now installed in the system trust store! ⚡️
生成证书
mkcert localhost
会在当前目录生成localhost.pem
和localhost-key.pem
两个文件
复制localhost.pem
,并将文件名改为localhost.crt
,用于本地安装
cp localhost.pem localhost.crt
安装证书,点击localhost.crt安装证书
将localhost.pem
和localhost-key.pem
两个文件放在nginx目录下
cd /usr/local/etc/nginx
mkdir ssl
cd ~
cp localhost.pem /usr/local/etc/nginx/ssl/localhost.pem
cp localhost-key.pem /usr/local/etc/nginx/ssl/localhost-key.pem
配置 Nginx
ssl_certificate /usr/local/etc/nginx/ssl/localhost.pem;
ssl_certificate_key /usr/local/etc/nginx/ssl/localhost-key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
重启nginx
su root
nginx -t && nginx -s reload
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
验证成功
生成多域名证书
mkcert example.com "*.example.com" example.test localhost 127.0.0.1 ::1
注意事项
一定要在客户端生成ssl证书!一定要在客户端生成ssl证书!一定要在客户端生成ssl证书!
之前在linux下生成的ssl证书,然后拷贝到windows主机上安装,浏览器一直提示证书颁发者无效 生成证书时不要sudo到root用户,不然会提示【此证书的签发者无效】报错
参考资料,本文主要参考 https://zhuanlan.zhihu.com/p/100743934