跳到主要内容
SSL 证书付费服务

wdCP 环境 HTTPS 与 SSL 证书配置全流程(Nginx / Apache 双模式实战)

wdCP 环境 HTTPS 与 SSL 证书配置全流程(Nginx / Apache 双模式实战) — wdCP 环境 HTTPS 与 SSL 证书配置全流程(Nginx / Apache 双模式实战)

一、问题背景:HTTP 被判“不安全”的根因与 wdCP 的特殊性

原文“2017 年起 HTTP 将被列为不安全访问”的时间点需要更正:真正落地的是 2018 年 7 月发布的 Chrome 68,它开始在地址栏把纯 HTTP 页面标记为“不安全(Not secure)”,随后 Firefox、Edge、Safari 全部跟进。到今天,HTTP 站点不仅被搜索引擎降权,还会直接丧失一批现代 Web 能力。

根因有三层。第一是传输层明文:HTTP 报文在公网以明文传输,运营商、公共 Wi-Fi、透明代理等任意中间节点都能读取和篡改 Cookie、表单与支付回调。第二是浏览器的安全上下文(Secure Context)限制:Service Worker、地理位置、摄像头/麦克风、剪贴板、WebCrypto 等 API 只在 HTTPS 下可用;HTTP/2 在主流浏览器中也只通过 TLS 提供(依赖 ALPN 协商)。第三是生态强制:微信小程序、公众号网页授权、支付宝异步回调、Apple 的 App Transport Security 都要求 HTTPS 且证书链完整。

证书有效期规则同样变了:CA/B 论坛规定单张 TLS 证书有效期上限 398 天(2020 年 9 月起被 Chrome/Apple 强制执行),Let’s Encrypt 免费证书固定 90 天;已通过的分阶段缩短计划要求 2026 年 3 月起降至 200 天、2027 年 100 天、2029 年 47 天。“一年一配、到期再买”的运维方式必须换成自动续期。

wdCP 的特殊性在于它有三种运行组合:纯 Nginx、Nginx+Apache(Nginx 监听 80 并反代到 Apache 的 88 端口处理 PHP)、纯 Apache。TLS 必须配在真正“终止”连接的那一层——Nginx+Apache 模式下证书只配 Nginx,Apache 端既不必要也不能重复配置。站点 vhost 分别位于 /www/wdlinux/nginx/conf/vhost/ 与 /www/wdlinux/apache/conf/vhost/,在面板里修改站点会重写这些文件,手工改动必须绕开面板的覆盖逻辑。

二、前置准备:环境确认、端口放行与备份

先确认版本与运行组合,避免把证书配错层:

cat /www/wdlinux/wdcp/version.txt
ps aux | grep -E 'nginx|httpd' | grep -v grep
/www/wdlinux/nginx/sbin/nginx -V 2>&1 | tr ' ' '\n' | grep -E 'ssl|http_v2'
ss -lntp | grep -E ':80|:443|:88'

域名解析必须与证书 SAN 完全一致,并特别检查 AAAA 记录:如果域名解析了 IPv6 但服务器没有在 443 上监听 IPv6,部分移动网络客户端会直接连接失败:

dig +short A www.example.com
dig +short AAAA www.example.com

放行 443 端口(firewalld 与 iptables 二选一,云主机还需在安全组放行):

firewall-cmd --permanent --add-service=https && firewall-cmd --reload
iptables -I INPUT -p tcp --dport 443 -j ACCEPT && service iptables save

建立证书目录、备份 vhost,并校准系统时间——证书校验依赖本机时间,时间漂移会产生“证书尚未生效/已过期”的假故障:

mkdir -p /www/wdlinux/nginx/conf/ssl /www/wdlinux/apache/conf/ssl
cp -a /www/wdlinux/nginx/conf/vhost /root/nginx-vhost.bak.$(date +%F)
cp -a /www/wdlinux/apache/conf/vhost /root/apache-vhost.bak.$(date +%F)
timedatectl set-timezone Asia/Shanghai
ntpdate ntp.aliyun.com || chronyc makestep

权限要点:私钥务必 chmod 600、属主 root,绝不能放在网站根目录下,否则 https://站点/xxx.key 可被任何人直接下载。

三、证书申请:CSR 生成、商业证书与 Let’s Encrypt 自动化

商业证书需要先产生私钥与 CSR(证书签名请求),CSR 只包含公钥和主体信息,私钥永远不出服务器:

cd /www/wdlinux/nginx/conf/ssl
openssl genrsa -out example.com.key 2048
openssl req -new -sha256 -key example.com.key -out example.com.csr \
  -subj "/C=CN/ST=Guangdong/L=Shenzhen/O=Example Tech/CN=example.com"
openssl req -in example.com.csr -noout -subject -text | head -20

把 CSR 提交给 CA 后通常拿到三样东西:站点证书 example.com.crt、中间证书 ca-bundle.crt、可选根证书。务必按“站点证书在前、中间证书在后”合并成 fullchain,因为 Android 与微信内置 WebView 不会自动补链,缺链就会报证书不受信任:

cat example.com.crt ca-bundle.crt > example.com.fullchain.crt
openssl verify -CAfile ca-bundle.crt example.com.fullchain.crt

免费方案用 Let’s Encrypt。wdCP 多部署在 CentOS 7,先装 EPEL 源下的 certbot,用 webroot 模式签发,-w 必须指向该站点真实的 DocumentRoot:

yum install -y epel-release && yum install -y certbot
certbot certonly --webroot -w /www/web/example_com/public_html \
  -d example.com -d www.example.com \
  --agree-tos -m admin@example.com --no-eff-email

通配符证书必须走 DNS-01 挑战:certbot certonly –manual –preferred-challenges dns -d “*.example.com” -d example.com。签发结果位于 /etc/letsencrypt/live/example.com/,其中 fullchain.pem 已含中间证书、privkey.pem 为私钥,配置里直接指向这两个路径即可,无需手工合并。

证书类型 典型有效期 价格区间 浏览器信任 适用场景
Let’s Encrypt DV 90 天(自动续期) 免费 全信任 中小站点、CI 集成
商业 DV 单域名 397 天左右 百元级 全信任 企业官网、测试环境
商业 DV 通配符 397 天左右 数百至千元 全信任 多子域站群
OV 企业型 397 天左右 千元级 全信任 电商、金融
EV 扩展验证 397 天左右 数千元 全信任(Chrome 77 起已不再显示绿色公司名) 品牌背书场景

四、Nginx 模式:完整 server 块与 80→443 跳转

先确认 Nginx 编译了 SSL 模块,无输出说明需要重新编译加 –with-http_ssl_module,或直接替换为官方 nginx 1.24 包并保留原 conf:

/www/wdlinux/nginx/sbin/nginx -V 2>&1 | tr ' ' '\n' | grep with-http_ssl_module

编辑 /www/wdlinux/nginx/conf/vhost/example.com.conf,把原 80 段改成 301 跳转,再新增 443 段。面板生成的 root、index、location 段要原样复制进 443 段,只替换 listen:

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate     /www/wdlinux/nginx/conf/ssl/example.com.fullchain.crt;
    ssl_certificate_key /www/wdlinux/nginx/conf/ssl/example.com.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES128-SHA256;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /www/wdlinux/nginx/conf/ssl/ca-bundle.crt;

    add_header Strict-Transport-Security "max-age=31536000" always;

    root /www/web/example_com/public_html;
    index index.php index.html;

    location ~ \.php$ {
        proxy_pass http://127.0.0.1:88;
        include /www/wdlinux/nginx/conf/proxy.conf;
    }
}

三个要点:其一,Nginx 1.25.1 起 listen 443 ssl http2 的写法已废弃,改用独立的 http2 on;,wdCP 自带旧版用 listen 443 ssl; 即可;其二,ssl_stapling 必须配合 ssl_trusted_certificate 指向中间证书,否则 OCSP 装订不生效;其三,Nginx+Apache 模式下 proxy_pass 仍指向 Apache 的 88 端口,Apache 侧继续跑 HTTP,不需要再配证书。

/www/wdlinux/nginx/sbin/nginx -t
service nginxd restart
tail -20 /www/wdlinux/nginx/logs/error.log

五、Apache 模式:mod_ssl 加载与 VirtualHost 配置

先确认 mod_ssl 已加载,Apache 2.4 还需要 socache_shmcb_module,否则启动即报错:

grep -E 'mod_ssl|socache_shmcb' /www/wdlinux/apache/conf/httpd.conf
ls /www/wdlinux/apache/modules/mod_ssl.so

若被注释,去掉 # 并确保存在 Listen 443:

sed -i 's/^#LoadModule ssl_module/LoadModule ssl_module/' /www/wdlinux/apache/conf/httpd.conf
sed -i 's/^#LoadModule socache_shmcb_module/LoadModule socache_shmcb_module/' /www/wdlinux/apache/conf/httpd.conf
grep -q '^Listen 443' /www/wdlinux/apache/conf/httpd.conf || echo 'Listen 443' >> /www/wdlinux/apache/conf/httpd.conf

新建 /www/wdlinux/apache/conf/vhost/example.com-ssl.conf:

<VirtualHost *:443>
    DocumentRoot "/www/web/example_com/public_html"
    ServerName www.example.com
    ServerAlias example.com

    SSLEngine on
    SSLProtocol -all +TLSv1.2 +TLSv1.3
    SSLCipherSuite HIGH:!aNULL:!MD5:!3DES
    SSLHonorCipherOrder on
    SSLCertificateFile      /www/wdlinux/apache/conf/ssl/example.com.crt
    SSLCertificateKeyFile   /www/wdlinux/apache/conf/ssl/example.com.key
    SSLCertificateChainFile /www/wdlinux/apache/conf/ssl/ca-bundle.crt

    Header always set Strict-Transport-Security "max-age=31536000"
    ErrorLog  /www/wdlinux/apache/logs/example.com-ssl-error_log
    CustomLog /www/wdlinux/apache/logs/example.com-ssl-access_log combined

    <Directory "/www/web/example_com/public_html">
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

80 端口的跳转写在原 vhost 中:Redirect permanent / https://www.example.com/,或用 RewriteEngine On 配合 RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]。注意 Apache 2.4.8 之后 SSLCertificateChainFile 已废弃,可把中间证书并入 SSLCertificateFile(fullchain),两者不可同时配置,否则会重复发送中间证书。最后 apachectl -t 校验后 service httpd restart。

六、wdCP 面板一键部署与四种方案对比

wdCP v3 提供图形化入口:登录 http://IP:8080 → 网站管理 → 站点列表 → 对应站点“修改” → 开启 SSL,粘贴证书与私钥内容。面板实际做的事情就是把证书写入 /www/wdlinux/nginx/conf/ssl/ 并在 vhost 中追加 443 段后重启服务。有三个坑必须注意:第一,面板“修改站点”提交时会用表单内容整体重写 vhost,手工追加的 HSTS、OCSP 等指令可能被覆盖,建议把自定义内容拆到独立 include 文件;第二,部分旧版本面板只接受 PEM 且不支持上传,需要先把站点证书与中间证书合并;第三,面板不会自动续期 Let’s Encrypt 证书,必须自己加 cron。

方案 证书配置位置 是否需改 Apache 优点 风险与限制
纯 Nginx /www/wdlinux/nginx/conf/vhost/*.conf 否 性能最好、配置直观 需 Nginx 编译 ssl 模块
Nginx+Apache(wdCP 默认) 仅 Nginx 443 段,反代到 Apache:88 否 复用现有 PHP 配置,改动最小 双层日志,排查链路较长
纯 Apache /www/wdlinux/apache/conf/vhost/*-ssl.conf 是 兼容 .htaccess 与老扩展 需手动加载 mod_ssl,性能略低
面板一键 面板自动写入上述路径 否 零命令行,适合批量站点 会覆盖手工配置,不自动续期

七、常见报错排查:从 emerg 到浏览器错误码

1)nginx: [emerg] unknown directive “ssl”:Nginx 未编译 –with-http_ssl_module。用 /www/wdlinux/nginx/sbin/nginx -V 2>&1 | grep -c with-http_ssl_module 验证,输出 0 即未编译,需重编译或换包。

2)nginx: [emerg] SSL_CTX_use_PrivateKey_file(…) failed (SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch):私钥与证书不配对。用以下命令比对,两个 md5 必须一致:

openssl x509 -noout -modulus -in example.com.crt | openssl md5
openssl rsa -noout -modulus -in example.com.key | openssl md5

3)curl: (60) SSL certificate problem: unable to get local issuer certificate:未发送中间证书。合并 fullchain 或给 Apache 加 SSLCertificateChainFile。验证:openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | grep -c ‘BEGIN CERTIFICATE’,正常应大于等于 2。

4)浏览器 NET::ERR_CERT_COMMON_NAME_INVALID:证书 SAN 不含当前访问域名(例如只签了 example.com 却访问 www.example.com)。重新签发时用 -d 把所有域名写进 SAN。

5)Apache: Invalid command ‘SSLEngine’, perhaps misspelled or defined by a module not included in the server configuration:mod_ssl 未加载,检查 LoadModule ssl_module modules/mod_ssl.so 与 modules 目录下的 mod_ssl.so 是否存在。

6)nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use):443 被占用,常见于 Apache 与 Nginx 同时监听 443。用 ss -lntp | grep ‘:443’ 定位进程。

7)SSL_ERROR_RX_RECORD_TOO_LONG:用 https:// 访问了 80 端口,或反代链路把明文送进了 TLS 端口,核对跳转规则与 upstream 端口。

8)微信/安卓报“证书无效”但 Chrome 正常:典型缺中间证书,Android 内置 WebView 不自动补链。

9)控制台 Mixed Content: The page at ‘https://…’ was loaded over HTTPS, but requested an insecure element ‘http://…’:混合内容。改模板里的硬编码为 // 或 https,数据库存量数据批量替换,临时可用 add_header Content-Security-Policy “upgrade-insecure-requests” always; 让浏览器自动升级。

10)AH01909: RSA certificate configured for www.example.com:443 does not include an ID which matches the server name:ServerName 与证书不匹配,或 vhost 未被命中。

八、效果验证与长期加固

先做本机验证,确认状态码、跳转与 HSTS 响应头:

curl -I https://www.example.com
curl -sI https://www.example.com | grep -i strict-transport-security
curl -I http://www.example.com
curl -vI https://www.example.com 2>&1 | grep -E 'SSL connection|TLS'

再验证证书链、有效期与旧协议是否已关闭(TLS 1.1 已被 RFC 8996 废弃,第二条应返回协议错误):

openssl s_client -connect www.example.com:443 -servername www.example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -issuer -dates
echo | openssl s_client -connect www.example.com:443 -servername www.example.com -tls1_1 2>&1 | grep -E 'Protocol|alert'

Let’s Encrypt 证书只有 90 天,续期必须自动化。–deploy-hook 只在真正续期成功时执行,可避免每次空跑都重启服务:

certbot renew --dry-run
crontab -e
0 3 * * * /usr/bin/certbot renew --quiet --deploy-hook "service nginxd reload"

外部检测建议用 SSL Labs,目标 A 级及以上,重点看 Certificate Chain 是否显示 Chain issues: None、Protocols 是否只剩 TLS 1.2/1.3;国内站点可再用 myssl.com 做微信兼容性检测。握手失败的定位入口是 /www/wdlinux/nginx/logs/error.log 与 /www/wdlinux/apache/logs/*-ssl-error_log,其中会留下 SSL_do_handshake 相关记录。

最后提醒:HSTS 一旦下发,浏览器会在 max-age 内强制走 HTTPS,上线前务必确认所有子域都支持 HTTPS,否则先用较小的 max-age(如 300 秒)灰度,再逐步加长并考虑提交 preload。

常见问题

一台 wdCP 服务器上多个站点,可以共用一个证书吗?

可以,前提是用多域名(SAN)证书或通配符证书,把域名全部写进同一张证书的 SAN 列表,然后每个站点的 vhost 都指向同一对 crt/key。注意通配符 *.example.com 只覆盖一级子域,不覆盖 a.b.example.com;不同顶级域必须额外加入 SAN。

配置完成后用 http 访问仍然返回 200,没有跳转到 https?

多数情况是只改了一个 vhost:Nginx+Apache 模式下 80 段在 Nginx 的 vhost 里,面板重写过站点会覆盖跳转规则。用 /www/wdlinux/nginx/sbin/nginx -T | grep -n ‘server_name’ 检查是否存在别的 server 块以 default_server 抢占了 80 请求。

Let’s Encrypt 证书 90 天到期后网站打不开怎么办?

先执行 certbot renew –force-renewal 手动续期恢复服务,再排查 cron 是否生效、certbot 版本是否支持 ACME v2(0.x 老版本需升级)。若当初用 –manual DNS 方式签发,无法自动续期,需换成带 DNS 插件的客户端或改用商业证书。

为什么证书有效期不是一年了?

CA/B 论坛规定 TLS 证书最长 398 天,2020 年 9 月起被 Chrome、Apple 强制执行,因此商业证书实际签发多为 397 天;Let’s Encrypt 固定 90 天。后续还将分阶段缩短至 200 天(2026 年 3 月)、100 天(2027 年)、47 天(2029 年),自动化续期是唯一可持续方案。

开启 HTTPS 后部分图片、JS 加载失败,控制台报 Mixed Content?

这是混合内容问题。把模板中的 http:// 硬编码改为 // 或 https://,数据库里的存量链接用 SQL 批量替换;临时方案是在 Nginx 加 add_header Content-Security-Policy “upgrade-insecure-requests” always;,让浏览器自动把不安全请求升级为 HTTPS。

wdCP 面板后台 8080 端口要不要也上 HTTPS?

建议上,但不要与站点共用证书路径。面板由 wdapache 独立进程提供服务,配置在 /www/wdlinux/wdapache/conf/httpd.conf,改前务必备份;一旦配错会导致面板无法登录,只能通过 SSH 手工回滚配置并重启 wdapache 服务。

内容审核

长期从事 SSL/TLS 证书部署与 HTTPS 运维,负责本站证书选型、安装配置与故障排查内容的编写与审核。