Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Apache SSL 證書安裝配置完整指南:從文件部署到 VirtualHost 啟用 HTTPS

編輯:關於伺服器

在 Apache HTTP Server 上部署 SSL/TLS 證書,實現 HTTPS 加密訪問。涵蓋證書文件部署、VirtualHost 配置、配置測試、服務重啟、故障排查、PCI 合規加固全流程。

1. 證書文件準備與部署

  • 從 CA 服務商下載:
    • 服務器證書:your_domain.crt (或 .pem)
    • 中間 CA 證書鏈:DigiCertCA.crt (或 ca-bundle.crt)
  • 私鑰文件:your_domain.key (CSR 生成時產生,嚴禁洩露)
  • 統一存放路徑 (建議):/etc/ssl/certs/ (證書)、/etc/ssl/private/ (私鑰)
  • 權限設置:chmod 600 /etc/ssl/private/your_domain.key、chown root:root 所有文件

2. 定位 Apache 配置文件

發行版/安裝方式主配置文件VirtualHost 典型位置
RHEL/CentOS/Fedora (yum/httpd)/etc/httpd/conf/httpd.conf/etc/httpd/conf.d/ssl.conf 或 vhost 文件
Debian/Ubuntu (apt/apache2)/etc/apache2/apache2.conf/etc/apache2/sites-available/default-ssl.conf
源碼編譯默認/usr/local/apache2/conf/httpd.confhttpd.conf 末尾或 extra/httpd-ssl.conf

搜索關鍵字 <VirtualHost 定位現有站點配置塊。

3. 配置 VirtualHost 啟用 SSL

場景 A:同時支持 HTTP (80) 與 HTTPS (443)

複製現有 HTTP VirtualHost 為新塊,修改端口為 443 並添加 SSL 指令:

<VirtualHost *:80>
    ServerName www.yourdomain.com
    DocumentRoot /var/www/html
    # 可選:重定向所有 HTTP 到 HTTPS
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerName www.yourdomain.com
    DocumentRoot /var/www/html
    
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/your_domain.crt
    SSLCertificateKeyFile /etc/ssl/private/your_domain.key
    SSLCertificateChainFile /etc/ssl/certs/DigiCertCA.crt
    
    # 安全加固 (推薦)
    SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite HIGH:!aNULL:!MD5:!3DES
    SSLHonorCipherOrder on
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</VirtualHost>

場景 B:僅支持 HTTPS (強制加密)

直接修改現有 VirtualHost 端口為 443 並添加上述 SSL 指令,刪除 80 端口配置。

關鍵指令對照表

指令指向文件說明
SSLCertificateFile服務器證書 (your_domain.crt)必須
SSLCertificateKeyFile私鑰 (your_domain.key)必須
SSLCertificateChainFile中間 CA 鏈 (DigiCertCA.crt)推薦 (避免瀏覽器信任鏈不全)
SSLCACertificateFile客戶端 CA 證書 (雙向認證時)可選

4. 配置測試與服務重啟

# 語法檢查 (關鍵步驟,防止配置錯誤導致服務無法啟動)
apachectl configtest
# 或
httpd -t
# 或 (Debian/Ubuntu)
apache2ctl configtest

# 重啟服務
apachectl restart
# 或
systemctl restart httpd
# 或
service apache2 restart
舊版本提示:Apache 2.2 早期版本使用 apachectl startssl 啟用 SSL,現代版本 (2.4+) 已廢棄,統一使用 apachectl start/restart,SSL 由配置文件 SSLEngine on 控制。

5. 故障排查與驗證工具

  • SSL Labs 測試:https://www.ssllabs.com/ssltest/ (全面評級、信任鏈、協議/套件支持、模擬客戶端兼容性)
  • 命令行驗證:
    openssl s_client -connect www.yourdomain.com:443 -servername www.yourdomain.com
    curl -Iv https://www.yourdomain.com
  • 常見錯誤:
    • SSLCertificateFile: file does not exist → 路徑/權限錯誤
    • Private key does not match certificate → 證書與私鑰不匹配 (重新生成 CSR/私鑰)
    • unable to get local issuer certificate → 缺少 SSLCertificateChainFile 或鏈不全

6. PCI DSS 合規加固 (禁用 SSLv2/弱加密套件)

在 VirtualHost 或全局 ssl.conf 添加:

SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite HIGH:!aNULL:!MD5:!3DES:!RC4:!DH:!ECDH
SSLHonorCipherOrder on
SSLCompression off
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
Header always set X-Content-Type-Options nosniff
Header always set X-Frame-Options DENY

7. 證書遷移與跨平台部署

  • PFX/P12 (Windows IIS) ↔ PEM (Apache/Nginx) 轉換:
    # PFX → PEM (私鑰+證書+鏈)
    openssl pkcs12 -in cert.pfx -out cert.pem -nodes
    # 提取私鑰
    openssl pkcs12 -in cert.pfx -nocerts -out key.pem -nodes
    # 提取證書
    openssl pkcs12 -in cert.pfx -clcerts -nokeys -out cert.crt
  • 詳細參考:OpenSSL 導出指南
現代最佳實踐:使用 Let's Encrypt 免費證書 + Certbot 自動續期 (certbot --apache),啟用 HTTP/2 (Protocols h2 http/1.1)、OCSP Stapling (SSLUseStapling on)、HSTS Preload 列表提交。
Copyright © Windows教程網 All Rights Reserved