mkcert 极简安装及使用步骤

1. 安装 mkcert

macOS
brew install mkcert
Linux
sudo apt install libnss3-tools
curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64"
chmod +x mkcert-v*-linux-amd64
sudo mv mkcert-v*-linux-amd64 /usr/local/bin/mkcert
Windows

使用 Chocolatey 安装:

choco install mkcert

2. 初始化 mkcert

生成本地 CA 并添加到系统信任列表:

mkcert -install

3. 生成证书

localhost 生成证书:

mkcert localhost

生成的证书文件:

  • localhost.pem(证书)
  • localhost-key.pem(私钥)

4. 使用证书

Node.js 示例
const https = require('https');
const fs = require('fs');

const options = {
  key: fs.readFileSync('localhost-key.pem'),
  cert: fs.readFileSync('localhost.pem')
};

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('Hello, HTTPS!');
}).listen(443);

5. 访问 HTTPS 服务

打开浏览器访问:

https://localhost

浏览器不会提示证书错误,因为证书已被系统信任。

你可能感兴趣的:(Linux,ssl)