自签名证书生成指南

使用 OpenSSL 生成 CA 根证书、服务器证书和客户端证书的完整教程

⚠️ 安全警告:自签名证书仅适用于开发测试和内部服务。生产环境请使用受信任 CA 签发的证书(如 Let's Encrypt)。

第一步:生成 CA 根证书

CA(证书颁发机构)根证书用于签发服务器和客户端证书,是证书信任链的基础。

生成 CA 私钥
openssl genrsa -out ca.key 4096
生成 CA 根证书(有效期10年)
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt \
  -subj '/C=CN/ST=Beijing/L=Beijing/O=MyOrg/OU=Dev/CN=My CA'

第二步:生成服务器证书

服务器证书用于 HTTPS 服务端,由 CA 根证书签发。

生成服务器私钥
openssl genrsa -out server.key 2048
创建 SAN 配置文件(server.cnf)
[req]
default_bits = 2048
prompt = no
default_md = sha256
distinguished_name = dn
req_extensions = v3_req

[dn]
C = CN
ST = Beijing
L = Beijing
O = MyOrg
OU = Dev
CN = example.com

[v3_req]
subjectAltName = @alt_names

[alt_names]
DNS.1 = example.com
DNS.2 = *.example.com
DNS.3 = localhost
IP.1 = 127.0.0.1
IP.2 = ::1
生成服务器证书签名请求(CSR)
openssl req -new -key server.key -out server.csr -config server.cnf
使用 CA 签发服务器证书(有效期1年)
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key \
  -CAcreateserial -out server.crt -days 365 \
  -extensions v3_req -extfile server.cnf

第三步:生成客户端证书

客户端证书用于双向 TLS(mTLS)认证,验证客户端身份。

生成客户端私钥
openssl genrsa -out client.key 2048
生成客户端 CSR
openssl req -new -key client.key -out client.csr \
  -subj '/C=CN/ST=Beijing/L=Beijing/O=MyOrg/OU=Dev/CN=Client'
使用 CA 签发客户端证书
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key \
  -CAcreateserial -out client.crt -days 365

证书信任链说明

CA 根证书
ca.crt / ca.key
自签名,10年有效期
└── 签发 ↓
服务器证书
server.crt / server.key
HTTPS 服务端使用
客户端证书
client.crt / client.key
mTLS 客户端认证

场景一:本地开发(localhost)

为 localhost 开发环境生成证书,浏览器会提示不安全,需要手动信任 CA。

localhost SAN 配置
[req]
default_bits = 2048
prompt = no
default_md = sha256
distinguished_name = dn
req_extensions = v3_req

[dn]
C = CN
ST = Beijing
L = Beijing
O = DevOrg
OU = Dev
CN = localhost

[v3_req]
subjectAltName = @alt_names

[alt_names]
DNS.1 = localhost
DNS.2 = *.localhost
IP.1 = 127.0.0.1
IP.2 = ::1

信任 CA 证书:

  • macOS: 双击 ca.crt → 钥匙串访问 → 始终信任
  • Windows: 双击 ca.crt → 安装到"受信任的根证书颁发机构"
  • Chrome: 设置 → 隐私安全 → 安全 → 管理证书 → 导入

场景二:内网服务

为内网 IP 或域名生成证书,适用于企业内部 API 和管理后台。

内网服务 SAN 配置示例
[alt_names]
DNS.1 = api.internal.company.com
DNS.2 = admin.internal.company.com
DNS.3 = *.internal.company.com
IP.1 = 192.168.1.100
IP.2 = 10.0.0.50

Nginx 配置

Nginx HTTPS 配置
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate     /path/to/server.crt;
    ssl_certificate_key /path/to/server.key;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    # 可选:双向 TLS (mTLS)
    # ssl_client_certificate /path/to/ca.crt;
    # ssl_verify_client on;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

# HTTP 跳转 HTTPS
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

Apache 配置

Apache HTTPS 配置
<VirtualHost *:443>
    ServerName example.com

    SSLEngine on
    SSLCertificateFile      /path/to/server.crt
    SSLCertificateKeyFile   /path/to/server.key
    SSLCertificateChainFile /path/to/ca.crt

    # 可选:双向 TLS (mTLS)
    # SSLCACertificateFile  /path/to/ca.crt
    # SSLVerifyClient require

    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>

证书查看与续期

查看证书信息
openssl x509 -in server.crt -text -noout
查看证书有效期
openssl x509 -in server.crt -noout -dates
验证证书链
openssl verify -CAfile ca.crt server.crt
续期服务器证书(重新签发)
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key \
  -CAcreateserial -out server.crt -days 365 \
  -extensions v3_req -extfile server.cnf
转换为 PFX/PKCS12 格式(Windows/IIS)
openssl pkcs12 -export -out server.pfx -inkey server.key \
  -in server.crt -certfile ca.crt

自签名证书使用指南

适用场景

  • 本地开发 - localhost HTTPS 开发调试
  • 内网服务 - 企业内部 API 和管理后台
  • 测试环境 - CI/CD 流水线和自动化测试
  • 学习实验 - 学习 TLS/SSL 原理和证书体系

注意事项

  • 妥善保管私钥文件,不要提交到代码仓库
  • 生产环境使用 Let's Encrypt 等受信任 CA
  • 定期检查证书有效期并及时续期
  • 浏览器默认不信任自签名证书,需手动添加

常见问题

相关工具