Pages

Sunday, 19 January 2020

使用mkcert配置本地https

很多时候项目在本地也需要同样的使用https协议(而本地所使用的域名并没有注册,而是直接在hosts文件中指定的),所以在本地我们不能使用以上的方法,而是使用一个叫mkcert的工具来生成证书。

安装mkcert

Windows安装mkcert

如果你知道Chocolatey,你可以用choco来安装:
choco install mkcert
Bash
如果你没用过choco,那直接在这里下载最新的Windows版mkcert.
下载了就可以直接用,不需要安装(用法在下边)。

macOS安装mkcert

其中nss是为了保证火狐浏览器能识别证书
brew install mkcert nss
Bash

Linux安装mkcert

Linux上要先安装nss(Network Security Services):
Ubuntu/Debian上安装nss:
sudo apt install libnss3-tools
Bash
RHEL/CentOS上安装nss:
sudo yum install nss-tools
Bash
Manjaro/Arch Linux上安装nss:
sudo pacman -S nss
Bash
SUSE Linux上安装nss:
sudo zypper install mozilla-nss-tools
Bash

安装nss后,再安装mkcert,常规方法无法安装,比如Ubuntu你没法用sudo apt install mkcert来安装。
方法一:可以用Linuxbrew来安装(Linux安装和使用LinuxBrew
brew install mkcert
Bash
方法二:直接在这里下载Linux的可执行文件即可。

设置mkcert为本地CA

前面已经我们已经安装好了mkcert(其实就是把mkcert的可执行文件下载下来),现在开始使用。
CA是Certificate Authority的缩写,即证书颁发机构,我们要配置让浏览器信任mkcert颁发的证书。
设置命令:
mkcert -install
Bash
注意:
其中的mkcert,在Windows中,如果你是直接下载的,名字并不是mkcert,比如我下载的就是mkcert-v1.4.1-windows-amd64.exe,建议打开cmd窗口后,直接把下载的mkcert拖到窗口中,再输入-install回车即可。
在Linux中,如果你是直接下载的,它名字也不叫mkcert,比如我下载的就叫mkcert-v1.4.1-linux-amd64,你需要先给它添加可执行权限然后再使用:
sudo +x ./mkcert-v1.4.1-linux-amd64
Bash

生成HTTPS证书

注意事项

1、下边用到的mkcert命令,在Windows中,如果你是直接下载的,名字并不是mkcert,比如我下载的就是mkcert-v1.4.1-windows-amd64.exe,建议打开cmd窗口后,直接把下载的mkcert拖到窗口中,再输入-install回车即可。
2、在Linux中,如果你是直接下载的,它名字也不叫mkcert,比如我下载的就叫mkcert-v1.4.1-linux-amd64,你需要先给它添加可执行权限,然后再按下边的方法使用:
sudo +x ./mkcert-v1.4.1-linux-amd64
Bash
3、下边的命令生成的证书都是在当前目录下

生成证书

www.example.com生成证书:
mkcert www.example.com
Bash
以上命令会在当前目录下生成:
www.example.com.pem
www.example.com-key.pem

生成通配符证书*.example.com
mkcert "*.example.com"
Bash
以上命令会在当前目录下生成:
_wildcard.example.com.pem
_wildcard.example.com-key.pem

给本地环回地址(127.0.0.1)生成证书:
mkcert 127.0.0.1
Bash
以上命令会在当前目录下生成:
127.0.0.1.pem
127.0.0.1-key.pem

其实以上三个可以写在一起的:
mkcert www.example.com *.example.com 127.0.0.1
生成证书后,如何使用?其实就是在http服务器(如nginx、apache等)里使用语句指定证书的路径,再把端口换成443即可,重启nginx/apache,再重启浏览器,即可生效。

上机操作

Mac和Linux用户相对比较熟悉命令,我就不录屏操作了,按前面说的方法,命令去做就行.
----------------------------------------------

mkcert

A simple zero-config tool to make locally trusted development certificates with any names you'd like. 
mkcert is a simple tool for making locally-trusted development certificates. It requires no configuration.
$ mkcert -install
Created a new local CA at "/Users/filippo/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 browser restart)! 🦊

$ mkcert example.com "*.example.com" example.test localhost 127.0.0.1 ::1
Using the local CA at "/Users/filippo/Library/Application Support/mkcert" ✨

Created a new certificate valid for the following names 📜
 - "example.com"
 - "*.example.com"
 - "example.test"
 - "localhost"
 - "127.0.0.1"
 - "::1"

The certificate is at "./example.com+5.pem" and the key at "./example.com+5-key.pem" ✅
Chrome and Firefox screenshot
Using certificates from real certificate authorities (CAs) for development can be dangerous or impossible (for hosts like example.testlocalhost or 127.0.0.1), but self-signed certificates cause trust errors. Managing your own CA is the best solution, but usually involves arcane commands, specialized knowledge and manual steps.
mkcert automatically creates and installs a local CA in the system root store, and generates locally-trusted certificates. mkcert does not automatically configure servers to use the certificates, though, that's up to you.

Installation

Warning: the rootCA-key.pem file that mkcert automatically generates gives complete power to intercept secure requests from your machine. Do not share it.

macOS

On macOS, use Homebrew
brew install mkcert
brew install nss # if you use Firefox
or MacPorts.
sudo port selfupdate
sudo port install mkcert
sudo port install nss # if you use Firefox

Linux

On Linux, first install certutil.
sudo apt install libnss3-tools
    -or-
sudo yum install nss-tools
    -or-
sudo pacman -S nss
    -or-
sudo zypper install mozilla-nss-tools
Then you can install using Linuxbrew
brew install mkcert
or build from source (requires Go 1.13+)
git clone https://github.com/FiloSottile/mkcert && cd mkcert
go build -ldflags "-X main.Version=$(git describe --tags)"
For Arch Linux users, mkcert is available on the official Arch Linux repository.
sudo pacman -Syu mkcert

Windows

On Windows, use Chocolatey
choco install mkcert
or use Scoop
scoop bucket add extras
scoop install mkcert
or build from source (requires Go 1.10+), or use the pre-built binaries.
If you're running into permission problems try running mkcert as an Administrator.

Supported root stores

mkcert supports the following root stores:
  • macOS system store
  • Windows system store
  • Linux variants that provide either
    • update-ca-trust (Fedora, RHEL, CentOS) or
    • update-ca-certificates (Ubuntu, Debian, OpenSUSE, SLES) or
    • trust (Arch)
  • Firefox (macOS and Linux only)
  • Chrome and Chromium
  • Java (when JAVA_HOME is set)
To only install the local root CA into a subset of them, you can set the TRUST_STORES environment variable to a comma-separated list. Options are: "system", "java" and "nss" (includes Firefox).

Advanced topics

Advanced options

 -cert-file FILE, -key-file FILE, -p12-file FILE
     Customize the output paths.

 -client
     Generate a certificate for client authentication.

 -ecdsa
     Generate a certificate with an ECDSA key.

 -pkcs12
     Generate a ".p12" PKCS #12 file, also know as a ".pfx" file,
     containing certificate and key for legacy applications.

 -csr CSR
     Generate a certificate based on the supplied CSR. Conflicts with
     all other flags and arguments except -install and -cert-file.
Note: You must place these options before the domain names list.

Example

mkcert -key-file key.pem -cert-file cert.pem example.com *.example.com

S/MIME

mkcert automatically generates an S/MIME certificate if one of the supplied names is an email address.
mkcert filippo@example.com

Mobile devices

For the certificates to be trusted on mobile devices, you will have to install the root CA. It's the rootCA.pem file in the folder printed by mkcert -CAROOT.
On iOS, you can either use AirDrop, email the CA to yourself, or serve it from an HTTP server. After installing it, you must enable full trust in itNote: earlier versions of mkcert ran into an iOS bug, if you can't see the root in "Certificate Trust Settings" you might have to update mkcert and regenerate the root.
For Android, you will have to install the CA and then enable user roots in the development build of your app. See this StackOverflow answer.

Using the root with Node.js

Node does not use the system root store, so it won't accept mkcert certificates automatically. Instead, you will have to set the NODE_EXTRA_CA_CERTS environment variable.
export NODE_EXTRA_CA_CERTS="$(mkcert -CAROOT)/rootCA.pem"

Changing the location of the CA files

The CA certificate and its key are stored in an application data folder in the user home. You usually don't have to worry about it, as installation is automated, but the location is printed by mkcert -CAROOT.
If you want to manage separate CAs, you can use the environment variable $CAROOT to set the folder where mkcert will place and look for the local CA files.

Installing the CA on other systems

Installing in the trust store does not require the CA key, so you can export the CA certificate and use mkcert to install it in other machines.
  • Look for the rootCA.pem file in mkcert -CAROOT
  • copy it to a different machine
  • set $CAROOT to its directory
  • run mkcert -install
Remember that mkcert is meant for development purposes, not production, so it should not be used on end users' machines, and that you should not export or share rootCA-key.pem.