nginx生成自签名证书
2025-05-06 19:10 WilliamZheng 阅读(230) 评论(0) 收藏 举报步骤 1: 创建证书存放目录
mkdir -p /etc/nginx/ssl
cd /etc/nginx/ssl
步骤 2: 生成私钥和自签名证书
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout ssl.key \
-out ssl.crt \
-subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/CN=testssl.io"
参数说明:
-x509: 生成自签名证书(而非证书请求)。
-nodes: 不加密私钥(避免 Nginx 启动时需要密码)。
-days 365: 证书有效期 1 年。
-newkey rsa:2048: 生成 2048 位 RSA 私钥。
-keyout: 输出私钥文件名。
-out: 输出证书文件名。
-subj: 证书信息(按需修改):
C: 国家代码(如 CN 表示中国)
ST: 省份
L: 城市
O: 组织名称
CN: 必须与你的域名一致(此处为 testssl.io)
步骤 3: 设置文件权限
chmod 600 ssl.key # 私钥设为仅所有者可读写
chmod 644 ssl.crt # 证书设为可读
步骤 4: 验证证书信息
openssl x509 -in ssl.crt -noout -text
检查关键字段:
Subject: CN = testssl.io
Validity: 有效期是否合理
Public-Key: 是否为 RSA (2048 bit)
步骤 5: 检查证书与私钥是否匹配
# 获取证书的 MD5 哈希
openssl x509 -noout -modulus -in ssl.crt | openssl md5
# 获取私钥的 MD5 哈希
openssl rsa -noout -modulus -in ssl.key | openssl md5
如果两个哈希值 相同,则证书和私钥匹配。
WilliamZheng©版权所有 转载请注明出处! 觉得对您有帮助请点个赞哟~ 运维架构师群:833329925
浙公网安备 33010602011771号