萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 網路安全 >  >> 病毒防護

Apache + mod_ssl 快速部署 HTTPS:從原始碼編譯到憑證簽署全流程

為什麼需要 HTTPS?

HTTP 明文傳輸帳密、Cookie、敏感資料,極易遭中間人竊聽竄改。SSL/TLS(HTTPS)提供加密傳輸、伺服器身分驗證、完整性保護,現為網站標配。本文示範從源碼編譯 Apache 1.3.x + mod_ssl + OpenSSL 的經典流程(現代部署建議直接使用發行版套件 apt install apache2 libapache2-mod-ssl 或 dnf install httpd mod_ssl)。

軟體版本對應表(歷史參考)

元件版本說明
Apache1.3.24舊版穩定分支
mod_ssl2.8.8-1.3.24必須與 Apache 版本嚴格對應
OpenSSL0.9.6c建議升級至 1.1.1/3.0 長期支援版

步驟 1:安裝 OpenSSL

cd /usr/local/src
tar xzf openssl-0.9.6c.tar.gz
cd openssl-0.9.6c
./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl
make && make test && make install

步驟 2:將 mod_ssl 修補進 Apache 原始碼樹

cd /usr/local/src
tar xzf mod_ssl-2.8.8-1.3.24.tar.gz
cd mod_ssl-2.8.8-1.3.24
./configure --with-apache=/usr/local/src/apache_1.3.24 --with-ssl=/usr/local/ssl

步驟 3:以 DSO 模式編譯 Apache

cd /usr/local/src/apache_1.3.24
./configure \
  --prefix=/usr/local/apache \
  --enable-rule=SHARED_CORE \
  --enable-module=ssl \
  --enable-shared=ssl
make

步驟 4:產生自我簽署憑證(測試用)

cd /usr/local/src/apache_1.3.24
make certificate TYPE=custom
# 互動式輸入兩組資訊:
# 第一組:建立臨時 CA(Common Name 可隨意)
# 第二組:正式伺服器憑證(Common Name = 伺服器 FQDN,如 www.example.com)
生產環境必須向受信 CA(Let's Encrypt、DigiCert、內部 PKI)申請正式憑證,瀏覽器才會信任。

步驟 5:安裝並啟動

make install
/usr/local/apache/bin/apachectl stop
/usr/local/apache/bin/apachectl startssl

驗證

  • https://yourdomain.com → 80 埠 HTTP
  • https://yourdomain.com → 443 埠 HTTPS,瀏覽器顯示鎖頭圖示
  • openssl s_client -connect yourdomain.com:443 -servername yourdomain.com 檢視憑證鏈

現代化部署建議(Apache 2.4 + Let's Encrypt)

# Ubuntu/Debian
apt update && apt install -y apache2 certbot python3-certbot-apache
a2enmod ssl rewrite headers
certbot --apache -d example.com -d www.example.com
# 自動續期已納入 systemd timer

SSL/TLS 強化配置片段(/etc/apache2/mods-available/ssl.conf)

SSLProtocol             all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite          HIGH:!aNULL:!MD5:!3DES
SSLHonorCipherOrder     on
SSLCompression          off
SSLSessionTickets       off
# HSTS(謹慎啟用,確保所有子域皆支援 HTTPS)
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
# OCSP Stapling
SSLUseStapling          on
SSLStaplingResponderTimeout 5
SSLStaplingReturnResponderErrors off

結語

從源碼編譯 mod_ssl 能深入理解 Apache SSL 模組載入機制,但生產環境請優先使用發行版套件與自動化憑證工具(Certbot/acme.sh),確保持續獲得安全更新與憑證續期。核心原則:強協議、強加密套件、HSTS、OCSP Stapling、定期掃描(SSL Labs/Qualys)。

copyright © 萬盛學電腦網 all rights reserved