localhost 127.0.0.1 是开发人员常用的两个本地主机地址,如果想要给他们俩签发浏览器安全证书,要涉及一些神秘的命令行以及专业知识。而在 macOS 与 Linux 上使用 mkcert 则可以 0 配置为他们俩签发并安装证书,并且还能将证书拿给其他系统使用。
真证书签发机构不太可能为你签发 Localhost 与 127.0.0.1 的证书,所以只能自己给自己签。
用 mkcert 非常简单,先安装。
安装 mkcert
官方提供编译好的版本,可以直接下载[fanctdl filename=’mkcert’ filesize=’4.76MB’ href=’https://github.com/FiloSottile/mkcert/releases’ filedown=’mkcert’][/fanctdl]使用
macOS 下也可以使用 brew 安装:brew install mkcert
brew install nss # if you use Firefox
Linux 需要安装依赖:sudo apt install libnss3-tools
或者
sudo yum install nss-tools
运行 mkcert
$ mkcert -install
Created a new local CA at "/Users/NAME/Library/Application Support/mkcert"
The local CA is now installed in the system trust store!
The local CA is now installed in the Firefox trust store (requires restart)!
$ mkcert example.com '*.example.org' myapp.dev localhos# 127.0.0.1 ::1//把#改成t
Using the local CA at "/Users/filippo/Library/Application Support/mkcert"
Created a new certificate valid for the following names
- "example.com"
- "*.example.org"
- "myapp.dev"
- "localhos#"//把#改成t
- "127.0.0.1"
- "::1"
The certificate is at "./example.com+5.pem" and the key at "./example.com+5-key.pem"
可以使用 mkcert -CAROOT 来查看 CA 证书,注意不要分享你的 CA 证书。
而 mkcert 还介绍来如何在 iOS、Android 上使用证书的步骤,可以前往 GitHub 了解,以及,下一步会支持 Windows,嗯期待一下。