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

Apache 虛擬主機 + SSL 一站式配置實戰:從編譯安裝到多域名 HTTPS

編輯:Linux 教學

本文記錄 Apache HTTP Server 從源碼編譯啟用 SSL 模塊、配置基於域名的虛擬主機、生成自簽證書、驗證多站點 HTTPS 訪問的完整流程,適用於開發測試環境快速搭建。

1. 源碼編譯安裝並啟用 SSL

# 下載解壓後
./configure --prefix=/usr/local/apache2 --enable-ssl --enable-so --enable-rewrite
make
make install

--enable-ssl 編入 mod_ssl,--enable-so 支援動態模塊(DSO),--enable-rewrite 支援 URL 重寫。

2. 啟用虛擬主機配置文件

編輯 /usr/local/apache2/conf/httpd.conf,取消註釋:

Include conf/extra/httpd-vhosts.conf

3. 配置虛擬主機

編輯 /usr/local/apache2/conf/extra/httpd-vhosts.conf:

<VirtualHost *:80>
    DocumentRoot "/usr/local/w1"
    ServerName www.shnu1.cn
    <Directory "/usr/local/w1">
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/usr/local/w2"
    ServerName www.shnu2.cn
    <Directory "/usr/local/w2">
        Require all granted
    </Directory>
</VirtualHost>

4. 本地測試:修改 hosts 文件

# Windows: C:\Windows\System32\drivers\etc\hosts
# Linux: /etc/hosts
10.10.134.26 www.w1.com
10.10.134.26 www.w2.com

在對應目錄建立測試頁面:w1.htm、w2.htm。

5. 重啟驗證 HTTP

/usr/local/apache2/bin/apachectl restart
# 瀏覽器訪問 https://www.w1.com、https://www.w2.com

6. 生成自簽 SSL 證書

cd /usr/local/apache2/conf
openssl req -new -x509 -nodes -days 365 -out server.crt -keyout server.key
# 依提示填寫:Country, State, City, Org, OU, Common Name (域名), Email

7. 啟用 HTTPS 虛擬主機

取消 httpd.conf 中註釋:

Include conf/extra/httpd-ssl.conf

編輯 httpd-ssl.conf,配置 443 端口虛擬主機並指向證書:

<VirtualHost *:443>
    DocumentRoot "/usr/local/w1"
    ServerName www.w1.com
    SSLEngine on
    SSLCertificateFile "/usr/local/apache2/conf/server.crt"
    SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"
    <Directory "/usr/local/w1">
        Require all granted
    </Directory>
</VirtualHost>

8. 現代化改良建議(生產環境必讀)

項目舊做法現代最佳實踐
證書來源自簽Let's Encrypt (Certbot) 免費受信證書,自動續期
SSL 協議預設全部SSLProtocol -ALL +TLSv1.2 +TLSv1.3
加密套件預設SSLCipherSuite HIGH:!aNULL:!MD5:!3DES
HSTS無Header always set Strict-Transport-Security "max-age=31536000"
OCSP Stapling無SSLUseStapling on + SSLStaplingCache
配置管理手工編輯Ansible/Terraform + 模板化,版本控制

驗證清單:apachectl configtest 語法檢查 → curl -I https://www.w1.com 返回 200 → 瀏覽器綠鎖圖標 → openssl s_client -connect www.w1.com:443 檢查證書鏈。

Copyright © Windows教程網 All Rights Reserved