SSL/TLS 基礎概念
- SSL (Secure Socket Layer):已棄用,存在安全漏洞
- TLS (Transport Layer Security):現代標準,TLS 1.2/1.3 強制要求
- 證書鏈:根 CA → 中間 CA → 服務器證書(葉子證書)
- 私鑰:服務器持有,絕不外洩,用於解密/簽名
步驟一:安裝支持 SSL 的 Apache
# 下載 OpenSSL 版本
https://httpd.apache.org/download.cgi
# 文件名含 openssl: httpd-2.4.x-win64-VS16-openssl-1.1.1.msi
# 安裝時選擇典型安裝,確保勾選 OpenSSL 模塊
步驟二:啟用 SSL 模塊與配置文件
# conf/httpd.conf
LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so # 會話緩存必需
Include conf/extra/httpd-ssl.conf
步驟三:生成私鑰與證書請求 (CSR)
# 1. 生成 RSA 私鑰 (2048 位最低,推薦 4096)
openssl genrsa -out server.key 4096
# 2. 生成 CSR (證書簽名請求)
openssl req -new -key server.key -out server.csr -config conf/openssl.cnf \
-subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/OU=IT/CN=www.example.com/emailAddress=admin@example.com"
# 關鍵字段:
# CN (Common Name) = 完整域名 (FQDN),必須與訪問域名完全一致
# SAN (Subject Alternative Name) 支持多域名,需在 openssl.cnf 配置
步驟四:獲取可信證書 (三種途徑)
| 途徑 | 適用場景 | 成本 | 信任度 |
| Let's Encrypt (Certbot/win-acme) | 公開網站、自動化續期 | 免費 | 全球信任 |
| 商業 CA (DigiCert, Sectigo, GlobalSign) | 企業級、EV/OV 證書、保險 | 年費制 | 全球信任 |
| 企業內部 CA (AD CS / OpenSSL 自建) | 內網、測試、IoT 設備 | 自建成本 | 需分發根證書 |
Let's Encrypt 自動化 (推薦)
# Windows 使用 win-acme
wget https://github.com/win-acme/win-acme/releases/download/v2.1.25/win-acme.v2.1.25.x64.pluggable.zip
unzip -d wacs
wacs.exe --target manual --host www.example.com --validation selfhosting --store pemfiles --pemfilespath C:\Apache24\conf\ssl
步驟五:自簽證書 (僅測試/內網)
# 1. 生成 CA 私鑰
openssl genrsa -out ca.key 4096
# 2. 生成 CA 自簽證書 (有效期 10 年)
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -config conf/openssl.cnf \
-subj "/C=CN/ST=Beijing/L=Beijing/O=MyCA/OU=IT/CN=MyRootCA"
# 3. CA 簽發服務器證書
# 創建 CA 目錄結構
mkdir -p demoCA/newcerts
touch demoCA/index.txt
echo 01 > demoCA/serial
# 簽發
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key -config conf/openssl.cnf -days 825
# 4. 客戶端需導入 ca.crt 至「受信任的根證書頒發機構」
步驟六:配置 httpd-ssl.conf
# conf/extra/httpd-ssl.conf
Listen 443
DocumentRoot "D:/Apache24/htdocs"
ServerName www.example.com:443
ServerAdmin admin@example.com
# 證書與私鑰路徑
SSLCertificateFile "conf/ssl/server.crt"
SSLCertificateKeyFile "conf/ssl/server.key"
# SSLCertificateChainFile "conf/ssl/ca_bundle.crt" # 中間證書 (商業證書通常需要)
# 安全配置 (現代兼容性配置)
SSLProtocol -all +TLSv1.2 +TLSv1.3
SSLCipherSuite HIGH:!aNULL:!MD5:!3DES
SSLHonorCipherOrder on
SSLCompression off
SSLSessionTickets off
# HSTS (謹慎啟用,預加載需提交名單)
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
# OCSP Stapling
SSLUseStapling on
SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
步驟七:HTTP 強制跳轉 HTTPS
# conf/extra/httpd-vhosts.conf 或 httpd.conf
ServerName www.example.com
Redirect permanent / https://www.example.com/
驗證與測試
# 配置測試
httpd -t
# 重啟服務
httpd -k restart
# 本地測試
curl -Ivk https://localhost/
openssl s_client -connect localhost:443 -servername www.example.com
# 在線檢測工具
# SSL Labs: https://www.ssllabs.com/ssltest/
# Mozilla Observatory: https://observatory.mozilla.org/
避坑指南
| 問題 | 原因 | 解決 |
| 瀏覽器警告「不安全」 | 自簽證書/過期/域名不匹配/缺少中間證書 | 使用可信 CA、檢查 CN/SAN、補全證書鏈 |
| SSL 協商失敗 | 協議/加密套件不匹配 | 啟用 TLS 1.2/1.3、配置現代加密套件 |
| 啟動報錯 "Init: SSLPassPhraseDialog" | 私鑰有密碼保護 | openssl rsa -in server.key -out server_nopass.key 移除密碼 |
| 虛擬主機 SSL 不生效 | SNI 未支持/配置錯誤 | 確保 NameVirtualHost *:443、每個 VH 獨立證書或多域證書 |