SSL 证书代安装完整指南:CentOS Nginx 与 Windows IIS 部署、配置与排错
一、问题背景与根因:证书买到了,为什么 HTTPS 还是装不上
很多用户在购买 DV/OV SSL 证书后,会收到一个压缩包,里面包含 .crt、.pem、.key、.pfx、.csr 等多种文件,但不知道哪个文件对应 Nginx、Apache 或 IIS。实际排障中,HTTPS 打不开通常不是证书“没生效”,而是四类根因:第一,证书链不完整,服务器只下发了叶子证书,浏览器报 NET::ERR_CERT_AUTHORITY_INVALID;第二,私钥与证书不匹配,Nginx 报 key values mismatch;第三,域名不匹配,访问 www.example.com 但证书只签发了 example.com;第四,443 端口未放行、TLS 协议版本过低,或页面仍引用 http:// 资源导致混合内容。
需要修正两个常见过时认知:自 2020 年 9 月 1 日起,公开信任的 TLS 证书最长有效期为 398 天,不能再签发多年期证书;Let’s Encrypt 证书有效期为 90 天,必须配置自动续期。CentOS 7 已于 2024 年 6 月 30 日 EOL,新部署建议使用 Rocky Linux 9、AlmaLinux 9 或 CentOS Stream 9;Windows 侧建议 Windows Server 2016 及以上,IIS 10 支持 SNI,多站点部署更稳妥。
代安装服务的边界要提前说清:客户提供已签发的域名证书、中间证书、私钥或 PFX 密码,以及服务器远程方式;技术方完成证书校验、链配置、443 绑定、HTTP 跳转、防火墙与验证。若页面源码存在 http:// 硬编码、绝对路径、CDN 回源未切 HTTPS,需要修改网站代码或第三方配置,应另行协商。快速判断命令:openssl x509 -in domain.crt -noout -subject -issuer -dates 查看主题、签发者和有效期;openssl rsa -in domain.key -noout -check 检查私钥完整性;curl -I https://example.com 看是否返回 200 与有效证书。
二、准备工作:识别证书文件并用 OpenSSL 校验私钥、证书链与 CSR
先给文件分类:domain.crt 是域名证书(叶子证书),intermediate.crt 是中间证书,root.crt 是根证书(通常不需要部署到服务器),domain.key 是私钥,cert.pfx 是 IIS 常用格式。Nginx/Apache 推荐使用 fullchain.pem,即“域名证书+中间证书”合并文件。执行以下命令检查证书域名与 SAN:
openssl x509 -in domain.crt -noout -subject -issuer -dates -ext subjectAltName
openssl rsa -in domain.key -noout -check
openssl x509 -noout -modulus -in domain.crt | openssl md5
openssl rsa -noout -modulus -in domain.key | openssl md5
两条 modulus 的 MD5 必须完全一致,否则私钥与证书不匹配。生成 fullchain:cat domain.crt intermediate.crt > fullchain.pem。验证链:openssl verify -CAfile chain.pem domain.crt,其中 chain.pem 应包含中间证书和根证书。检查 CSR 的 SAN:openssl req -in domain.csr -noout -subject -text | grep -A1 "Subject Alternative Name"。如果只有 .pfx,可转换为 PEM:openssl pkcs12 -in cert.pfx -nodes -out all.pem,再提取私钥:openssl pkcs12 -in cert.pfx -nocerts -nodes -out key.pem。
权限与目录也要规范。CentOS/Rocky 建议放 /etc/pki/tls/certs/fullchain.pem 和 /etc/pki/tls/private/domain.key,执行 chmod 600 domain.key、chown root:root domain.key。Windows IIS 直接导入 .pfx,导入时勾选“允许导出证书”便于备份。重点检查 SAN 是否同时包含 example.com 与 www.example.com;单域名 DV 证书通常只含一个域名,www 需要单独签发或使用通配符证书。证书有效期上限为 398 天,Let’s Encrypt 为 90 天,建议用 openssl x509 -enddate -noout -in domain.crt 记录到期日并设置续期提醒。
三、CentOS/RHEL 系 Nginx 部署完整步骤(命令可直接执行)
以 Rocky Linux 9 / AlmaLinux 9 为例,Nginx 1.20 以上版本。安装并启动:dnf install -y nginx openssl,systemctl enable --now nginx。创建目录并上传证书:mkdir -p /etc/nginx/ssl,把 fullchain.pem 和 privkey.pem 放入该目录,执行 chmod 600 /etc/nginx/ssl/privkey.pem。编辑 /etc/nginx/conf.d/example.conf,写入以下配置:
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000" always;
root /var/www/html;
index index.html;
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
执行 nginx -t 检查语法,通过后 systemctl reload nginx。防火墙放行:firewall-cmd --permanent --add-service=https,firewall-cmd --reload。SELinux 启用时,若证书放在非标准目录,执行 restorecon -Rv /etc/nginx/ssl 修正上下文。若使用 Let’s Encrypt,可安装 certbot:dnf install -y certbot python3-certbot-nginx,执行 certbot --nginx -d example.com -d www.example.com,自动续期由 systemd timer 完成,用 systemctl list-timers | grep certbot 确认。注意 Certbot 会修改 Nginx 配置,生产环境先备份 /etc/nginx。
四、Windows Server IIS 部署完整步骤(含 PFX 导入与绑定)
适用 Windows Server 2016/2019/2022,IIS 10 及以上。先确认已安装“IIS 管理控制台”和“万维网服务”。图形界面:打开 inetmgr,点击服务器名称,双击“服务器证书”,右侧“导入”,选择 cert.pfx,输入密码,勾选“允许导出证书”。PowerShell 管理员方式导入:
$pwd = ConvertTo-SecureString -String "PfxPassword" -AsPlainText -Force
Import-PfxCertificate -FilePath C:sslcert.pfx -CertStoreLocation Cert:LocalMachineMy -Password $pwd
Get-ChildItem Cert:LocalMachineMy | Where-Object {$_.Subject -like "*example.com*"} | Format-List Subject,Thumbprint,NotAfter
记下 Thumbprint,在 IIS 站点“绑定”中添加 https,端口 443,选择证书,多站点必须勾选“需要服务器名称指示”。命令行绑定示例:netsh http add sslcert hostnameport=example.com:443 certhash=THUMBPRINT appid={4dc3e181-e14b-4a21-b022-59fc669b0914} certstorename=MY,用 netsh http show sslcert 核对。HTTP 跳转 HTTPS 可安装 URL Rewrite 模块,添加规则匹配 ^http:// 并重写为 https://{HTTP_HOST}/{R:1},或建立 80 站点返回 301。
PFX 必须包含完整证书链;若只导入叶子证书,浏览器会报证书颁发机构无效。补链命令:certutil -addstore -f "CA" intermediate.crt。常见报错 0x80070520 多与私钥权限、证书存储或 SNI 绑定有关,可在 certlm.msc 中给 IIS_IUSRS 授予私钥读取权限,再重新绑定。验证:浏览器访问 https://example.com,查看锁标志;命令行执行 curl.exe -I https://example.com。IIS 绑定即时生效,无需重启服务器;修改应用池或证书存储后可执行 iisreset。
五、Apache httpd 配置示例:CentOS 与 Windows 通用
CentOS/Rocky 安装:dnf install -y httpd mod_ssl,配置文件在 /etc/httpd/conf.d/ssl.conf。Windows 使用 Apache24confextrahttpd-ssl.conf。核心配置如下,注意 Apache 2.4.8 之后推荐把 fullchain 直接放入 SSLCertificateFile,SSLCertificateChainFile 已废弃:
Listen 443
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot "/var/www/html"
SSLEngine on
SSLCertificateFile "/etc/httpd/ssl/fullchain.pem"
SSLCertificateKeyFile "/etc/httpd/ssl/domain.key"
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite HIGH:!aNULL:!MD5
Header always set Strict-Transport-Security "max-age=31536000"
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
检查与重载:CentOS 执行 apachectl configtest,通过后 systemctl reload httpd;Windows 执行 httpd -t,再 Restart-Service Apache2.4。私钥权限设为 600,CentOS 下确保 apache 用户可读,可将私钥属组设为 apache 并 chmod 640。若报 AH02565: Certificate and private key do not match,说明证书与私钥不匹配,用第二节 modulus 命令比对。若报 AH02217: ssl_stapling_init_cert,通常是 OCSP 配置缺失,可暂时关闭 SSLUseStapling。Apache 同样支持 Let’s Encrypt:certbot --apache -d example.com -d www.example.com。
六、部署方式与证书类型对比表格
不同服务器环境的证书格式、链配置和重载命令差异明显,先看下表再动手,能减少大量返工。Nginx 适合高并发反向代理,Apache 兼容传统 .htaccess 站点,IIS 适合 Windows/.NET 应用。证书类型方面,DV 只验证域名控制权,签发快;OV 验证组织信息;EV 展示组织名称但浏览器 UI 已弱化。公开信任证书最长 398 天,Let’s Encrypt 90 天,通配符证书覆盖同一主域下一级子域,多域名证书用 SAN 扩展。
- 代购 SSL 证书 · DigiCert / GeoTrust / RapidSSL 单域名与通配符
- HTTPS证书申请 · SSL证书安装部署 · 证书续费
- SSL/HTTPS 证书配置 · 证书安装 · 证书申请
| 项目 | Nginx(CentOS/Rocky) | Apache(CentOS/Windows) | IIS(Windows Server) |
|---|---|---|---|
| 证书格式 | fullchain.pem + privkey.pem | fullchain.pem + domain.key | .pfx(含私钥与链) |
| 配置路径 | /etc/nginx/conf.d/*.conf | /etc/httpd/conf.d/ssl.conf 或 httpd-ssl.conf | inetmgr 站点绑定 |
| 链配置 | ssl_certificate 指向 fullchain | SSLCertificateFile 指向 fullchain | 导入 PFX 时自动包含 |
| 语法检查 | nginx -t | apachectl configtest / httpd -t | netsh http show sslcert |
| 重载命令 | systemctl reload nginx | systemctl reload httpd | 绑定即时生效,必要时 iisreset |
| HTTP 跳转 | return 301 https://$host$request_uri | Redirect permanent / https://… | URL Rewrite 或 80 站点 301 |
| 常见错误 | key values mismatch | AH02565 证书私钥不匹配 | 0x80070520 绑定失败 |
选择建议:已有 Nginx 反向代理则优先在 Nginx 终止 TLS,后端只跑 HTTP;Apache 老站点注意关闭 SSLv3/TLS1.0/1.1;IIS 多站点务必启用 SNI,否则同一 IP 只能绑定一张证书。证书到期前 30 天完成续期,避免 398 天有效期到期造成业务中断。
七、常见错误排查:从真实报错到修复命令
1)浏览器报 NET::ERR_CERT_AUTHORITY_INVALID:证书链不完整。执行 openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 查看服务器下发的证书数量,只有一张说明缺中间证书。Nginx 改用 fullchain.pem,Apache 把中间证书合并到 SSLCertificateFile,IIS 用 certutil -addstore -f "CA" intermediate.crt 补链。
2)Nginx 报 [emerg] SSL_CTX_use_PrivateKey_file() failed (SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch):私钥与证书不匹配。用 openssl x509 -noout -modulus -in fullchain.pem | openssl md5 和 openssl rsa -noout -modulus -in privkey.pem | openssl md5 比对,不一致就找回正确私钥,不要强行重载。
3)命令行报 curl: (60) SSL certificate problem: unable to get local issuer certificate:客户端缺根证书或服务端链不全。先用 curl --cacert chain.pem -I https://example.com 验证链文件,再检查服务端是否下发中间证书。4)Apache 报 AH02565: Certificate and private key do not match:处理方式同上。5)IIS 报 0x80070520:检查 PFX 是否含私钥、IIS_IUSRS 是否有私钥读取权限、绑定是否勾选 SNI,执行 netsh http show sslcert 核对 certhash。
6)浏览器报 ERR_SSL_PROTOCOL_ERROR:检查 Nginx 是否写了 listen 443 ssl、防火墙是否放行 443、后端是否把 HTTPS 当 HTTP 转发。7)控制台报 Mixed Content: The page at 'https://...' was loaded over HTTPS, but requested an insecure resource 'http://...':页面存在 http:// 绝对路径或 http 接口。把资源改为相对路径、// 协议相对地址或 https://,并检查 CDN 回源协议。8)证书域名不匹配:用 openssl x509 -ext subjectAltName -noout -in domain.crt 确认 SAN,缺 www 就重新签发或 301 到证书覆盖的域名。
八、效果验证:命令行、浏览器与在线工具三重检查
部署完成后不要只看浏览器锁标志。先做命令行验证:openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates,确认 subject 含正确域名、issuer 是中间 CA、notAfter 在有效期内。再执行 openssl s_client -connect example.com:443 -servername example.com -verify_return_error </dev/null,返回 Verify return code: 0 (ok) 才算链完整。用 curl -I https://example.com 看 HTTP/2 200,用 curl -I http://example.com 看是否 301 到 HTTPS。
检查协议与加密套件:openssl s_client -connect example.com:443 -tls1_2 </dev/null 应成功,-tls1_0 应失败;nmap --script ssl-enum-ciphers -p 443 example.com 可列出支持的套件。检查 HSTS:curl -I https://example.com | grep -i strict-transport-security。浏览器侧打开 DevTools 的 Security 面板,确认证书有效、无混合内容;地址栏锁标志点开可看证书链。在线工具建议用 SSL Labs、Why No Padlock 交叉验证。若使用 Let’s Encrypt,执行 certbot renew --dry-run 模拟续期,确保 90 天到期前能自动更新。最后记录证书到期日,设置 30 天前提醒。
常见问题
SSL 证书最长有效期是多久?
自 2020 年 9 月 1 日起,公开信任的 TLS 证书最长有效期为 398 天;Let’s Encrypt 免费证书为 90 天。不能再用多年期证书,必须建立续期和替换流程,建议到期前 30 天完成续签。
代安装需要客户提供哪些材料?
需要域名证书、中间证书、私钥或 PFX 及密码、服务器远程方式、站点域名列表。私钥属于敏感信息,安装完成后应立即修改服务器密码,并确认私钥未通过聊天工具长期留存。
CentOS 7 还能部署新证书吗?
CentOS 7 已于 2024 年 6 月 30 日 EOL,官方不再提供安全更新。新项目建议 Rocky Linux 9、AlmaLinux 9 或 CentOS Stream 9;存量 CentOS 7 需切换 vault 源,并确保 OpenSSL 支持 TLS 1.2/1.3。
浏览器提示“不安全”但锁标志正常,是什么原因?
通常是混合内容:页面通过 HTTPS 加载,但图片、JS、CSS 或接口仍使用 http:// 绝对路径。打开 DevTools Console 搜索 Mixed Content,把资源改为相对路径、// 或 https://,并检查 CDN 回源协议。
一张证书能同时用于 example.com 和 www.example.com 吗?
取决于证书 SAN 列表。单域名 DV 证书通常只含一个域名,不含 www;需要签发时勾选 www,或使用多域名证书、通配符证书。部署前用 openssl x509 -ext subjectAltName 确认。
安装证书后需要重启服务器吗?
Nginx 执行 systemctl reload nginx,Apache 执行 systemctl reload httpd 即可,无需重启操作系统;IIS 绑定即时生效,修改证书存储或应用池后可执行 iisreset。重载前务必先做 nginx -t 或 apachectl configtest。