环境准备与工具安装
生成自签名证书需安装OpenSSL工具,Windows用户可从官网下载安装包,Linux系统通过包管理器执行安装命令:
- Ubuntu/Debian:
sudo apt install openssl
- CentOS/RHEL:
sudo yum install openssl
生成证书核心步骤
通过三阶段命令实现证书生成,建议在服务器终端逐条执行:
- 生成RSA私钥:
openssl genpkey -algorithm RSA -out server.key
- 创建证书请求(CSR):
openssl req -new -key server.key -out server.csr
- 自签名证书生成:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
注:建议在创建CSR时指定Common Name
为虚拟域名,并通过-extfile
参数添加subjectAltName
扩展字段。
证书部署与验证
将生成的server.key
和server.crt
文件配置到Web服务器:
- Nginx示例配置:
ssl_certificate /path/server.crt;
ssl_certificate_key /path/server.key; - 浏览器访问时需手动信任证书,Chrome可通过
chrome://flags/#allow-insecure-localhost
启用本地调试
自签名证书适用于内部测试与开发环境,通过OpenSSL工具链可在5分钟内完成虚拟域名的HTTPS配置。需注意生产环境应使用受信CA颁发的证书以确保安全性。