使用mkcert工具来创建本地可信的SSL证书。mkcert是一个简单的工具,用于制作本地信任的开发证书。它不需要配置。

 步骤:

 1. 安装mkcert

 2. 创建本地CA(Certificate Authority)

 3. 生成域名证书(比如localhost, 127.0.0.1, 或者自定义域名)

 4. 在开发服务器中配置使用这些证书

 详细步骤:

 1. 安装mkcert

   根据你的操作系统,安装方法不同。

   - 在macOS上,可以使用Homebrew:

        brew install mkcert

        brew install nss   # 如果你使用Firefox,需要这一步

   - 在Windows上,可以使用Chocolatey:

        choco install mkcert

     或者使用Scoop:

        scoop bucket add extras

        scoop install mkcert

   - 在Linux上,需要先安装certutil,然后下载预编译的二进制文件或者从源码构建。

     例如,在Debian/Ubuntu上:

        sudo apt install libnss3-tools

        然后从GitHub releases下载或者使用包管理器(如果有的话)

   或者,也可以使用go安装(需要安装Go):

        go install filippo.io/mkcert@latest

 2. 安装本地CA

    运行以下命令,将mkcert的CA添加到系统的信任存储中:

        mkcert -install

    这样,系统(和浏览器)就会信任由这个CA签发的证书。

 3. 生成证书

    假设我们要为localhost和127.0.0.1生成证书(这是最常见的本地开发场景):

        mkcert localhost 127.0.0.1 ::1

    你也可以为其他域名生成,比如:

        mkcert example.test

    这个命令会生成两个文件:`localhost+1.pem`(证书)和`localhost+1-key.pem`(私钥)。文件名的数字可能会根据你指定的域名数量变化。

    如果你想指定证书输出的位置和名称,可以使用`-cert-file`和`-key-file`参数:

        mkcert -cert-file cert.pem -key-file key.pem localhost 127.0.0.1

 4. 在开发服务器中使用证书

    根据你使用的服务器,配置方法不同。下面举几个例子:

    - Node.js (使用https模块和express):

        const https = require('https');

        const fs = require('fs');

        const express = require('express');

        const app = express();

        const options = {

          key: fs.readFileSync('path/to/key.pem'),

          cert: fs.readFileSync('path/to/cert.pem')

        };

        https.createServer(options, app).listen(443);

    - 使用webpack-dev-server(在webpack.config.js中):

        module.exports = {

          // ...其他配置

          devServer: {

            https: {

              key: fs.readFileSync('path/to/key.pem'),

              cert: fs.readFileSync('path/to/cert.pem'),

              ca: fs.readFileSync('path/to/mkcert生成的CA证书,一般不需要指定,因为已经信任了CA') // 通常不需要

            }

          }

        };

    - 使用Python Flask(使用adhoc模式也可以,但这里用具体文件):

        from flask import Flask

        app = Flask(__name__)

        if __name__ == '__main__':

            app.run(ssl_context=('path/to/cert.pem', 'path/to/key.pem'))

    - 使用ASP.NET Core(在Program.cs中):

        public static IHostBuilder CreateHostBuilder(string[] args) =>

            Host.CreateDefaultBuilder(args)

                .ConfigureWebHostDefaults(webBuilder =>

                {

                    webBuilder.UseStartup<Startup>();

                    webBuilder.ConfigureKestrel(options =>

                    {

                        options.Listen(IPAddress.Any, 5001, listenOptions =>

                        {

                            listenOptions.UseHttps("path/to/cert.pem", "path/to/key.pem");

                        });

                    });

                });

 注意:生成的证书是绑定到你指定的域名的,确保你访问的地址是证书中包含的域名(比如https://localhost),这样浏览器就不会报证书错误。

 5. 其他注意事项

    - 如果你移动了证书文件,记得更新服务器配置中的路径。

    - 当不再需要时,可以通过删除CA来取消信任(但通常不需要,除非有安全考虑)。

    - mkcert生成的证书只在本地开发使用,不要用于生产环境。

 通过以上步骤,你就可以在本地开发环境中使用HTTPS了。

使用 mkcert 创建本地可信 SSL 证书的步骤

1. 安装 mkcert

macOS (Homebrew):

bash

brew install mkcert

brew install nss  # 支持 Firefox

Windows (Chocolatey):

bash

choco install mkcert

Linux (Debian/Ubuntu):

bash

sudo apt install libnss3-tools

curl -sLO https://github.com/FiloSottile/mkcert/releases/download/v1.4.4/mkcert-v1.4.4-linux-amd64

chmod +x mkcert-v1.4.4-linux-amd64

sudo mv mkcert-v1.4.4-linux-amd64 /usr/local/bin/mkcert

2. 创建并信任本地 CA

bash

mkcert -install

此操作会将 mkcert 的根证书添加到系统信任库(浏览器/操作系统自动信任)。

3. 为域名生成证书

bash

基本用法(生成证书 + 私钥)

mkcert example.test "*.example.test" localhost 127.0.0.1 ::1

自定义输出路径/文件名

mkcert -cert-file ./certs/cert.pem -key-file ./certs/key.pem example.com

参数说明:

example.test:自定义本地域名

*.example.test:通配符子域名

localhost / 127.0.0.1 / ::1:本地环回地址

生成的文件:<域名>+<数字>-key.pem(私钥)和 <域名>+<数字>.pem(证书)

4. 在开发服务器中使用证书

Node.js (Express):

javascript

const https = require('https');

const fs = require('fs');

const app = require('./app'); // Express 应用

const options = {

  key: fs.readFileSync('localhost+2-key.pem'),

  cert: fs.readFileSync('localhost+2.pem')

};

https.createServer(options, app).listen(443);

Python (Flask):

python

from flask import Flask

app = Flask(__name__)

if __name__ == "__main__":

    app.run(ssl_context=('cert.pem', 'key.pem'), port=443)

Nginx 配置示例:

nginx

server {

    listen 443 ssl;

    server_name localhost;


    ssl_certificate /path/to/localhost+2.pem;

    ssl_certificate_key /path/to/localhost+2-key.pem;


    location / {

        proxy_pass http://localhost:3000;

    }

}

5. 浏览器访问

直接访问 https://localhost 或 https://example.test(需配置本地 hosts)

浏览器显示 安全锁标志 ✅

常见问题解决

浏览器不信任证书?

确保已运行 mkcert -install(重启浏览器)。

检查证书是否包含当前域名(如 localhost)。

移动设备测试?

导出 CA 根证书:

bash

mkcert -CAROOT  # 查看 CA 路径

将 rootCA.pem 发送到设备并手动信任(设置 → 安全 → 安装证书)。

删除所有证书/取消信任?

卸载 CA:

bash

mkcert -uninstall

删除生成的证书文件。

优点总结

✅ 零配置:自动创建可信证书。

✅ 跨平台:支持 macOS/Linux/Windows。

✅ 通配符域名:支持 *.example.test 格式。

✅ 无域名限制:可绑定任意本地域名/IP。

注意事项:生产环境请使用 Let's Encrypt 等正式 CA,mkcert 仅用于本地开发。