admin 發表於 2018-2-9 19:59:45

啟動 Windows 版 Apache 的 HTTPS (SSL) 功能

步驟大概分為下列:

[*]建立憑證
[*]修改 Apache 的 httpd.conf 檔
[*]修改 Apache 的 httpd-ssl.conf 檔


1. 建立憑證
首先一開始即是需要建立憑證,大概需要下面幾個指令


[*]openssl genrsa -des3 -out server.key 2048
[*]openssl req -new -key server.key -out server.csr
[*]openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
[*]openssl rsa -in server.key.org -out server.key

而 Windows 版 Apache 裡面通常附有 OpenSSL,以我的環境來說,在 Windows 7 下安裝好 Apache 之後,
OpenSSL 預設路徑即為 Apache 安裝路徑底下的 bin 資料夾中的 openssl.exe,
要做的就是透過內附的 OpenSSL 來產生憑證。

另外由於 Apache 內附的 OpenSSL,其 config 檔案是另外放在 Apache 裡 conf 資料夾下的 openssl.cnf,
因此在執行上面的指令時假如有出現找不到 config 檔案的話,需要在指令裡多加參數 -config "Apache 安裝路徑\conf\openssl.cnf"

當然如果先將命令提示字元切換至 Apache 安裝路徑後再執行指令的話,就可以避免每次要載入 config 檔案都要打那一長串的 "Apache 安裝路徑"

接著就是開啟 Windows 內建的命令提示字元,並切換到 Apache 的安裝路徑下


然後依序輸入指令

openssl genrsa -des3 -out server.key 2048


openssl req -new -key server.key -out server.csr


輸入憑證相關資訊 , 如:Country Name=TW , State Name=Taiwan , Organization Name=neidi , Common Name=neidi.homeip.net (Hostname),其他可以不用填


openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt


openssl rsa -in server.key.org -out server.key
此步驟為移除私密金鑰中的密碼,而從私密金鑰中摘除密碼的原因是:Apache win32 版本不支援 Passphrase 的功能。
故先將原本的 server.key 改名為 server.key.org,再執行這個指令


最後將上面產生的 server.key 跟 server.crt ,就直接擺到 "Apache 安裝路徑\conf" 即可(Apache預設的憑證放置位置)

2. 修改 Apache 的 httpd.conf 檔
將 httpd.conf 中 "LoadModule ssl_module modules/mod_ssl.so" 前面的註解取消
接著將 "Include conf/extra/httpd-ssl.conf" 的註解也取消掉

3.修改 Apache 的 httpd-ssl.conf 檔
將 "Apache 安裝路徑\conf\extra\httpd-ssl.conf 檔 "中的相關設定輸入

<VirtualHost *:443>

DocumentRoot "C:/Apache2.2/htdocs"
ServerName neidi.homeip.net:443
ServerAdmin
ErrorLog "logs/neidi.homeip.net_https-error.log"
TransferLog "logs/neidi.homeip.net_https-access.log"

SSLEngine on

SSLProtocol all -SSLv2

SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5

SSLCertificateFile "C:/Apache2.2/conf/server.crt"

SSLCertificateKeyFile "C:/Apache2.2/conf/server.key"

<FilesMatch "\.(cgi|shtml|phtml|php)[      DISCUZ_CODE_0      ]quot;>
SSLOptions +StdEnvVars
</FilesMatch>
<Directory "C:/Apache2.2/cgi-bin">
SSLOptions +StdEnvVars
</Directory>

BrowserMatch "MSIE " \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0

CustomLog "C:/Apache2.2/logs/ssl_request.log" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

</VirtualHost>
最後重新啟動 Apache 即可搞定囉!

ps,在 windows 環境底下設定 apache ( 32 位元 ) SSL 憑證讓網址變成 https

此環境為 apache、mysql、php 獨立安裝的 windows 環境
1.首先切換到Apache的bin目錄
 這裡的apache的目錄在C:\FMS\apache\bin

2.使用OpenSSL建立『server.key』私鑰檔案,並輸入自訂短密碼
openssl genrsa -out server.key 2048

這邊請不要加上 -des or -des3 來使用密碼保護
因為有密碼保護的 key,會讓 apache 在啟動時詢問密碼,這樣 linux 會停住
而 windows 則因為不支援密碼輸入,所以 apache 會開不起來

3.使用私鑰去建立『server.csr』憑證請求檔
openssl req -new -key server.key -out server.csr -config ..\conf\openssl.cnf

4.在建立憑證請求檔時會詢問相關資訊
Country Name (2 letter code) :所在國家簡寫TW
State or Province Name (full name) :所在國家Taiwan
Locality Name (eg, city) []:所在城市
Organization Name (eg, company) :公司名稱
Organizational Unit Name (eg, section) []:組織名稱
Common Name (e.g. server FQDN or YOUR name) []:網址名稱
Email Address []:聯絡人信箱

5.使用憑證請求檔及私鑰檔去建立3650天的X.509格式憑證的CRT憑證檔
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

6.以上步驟完成後會得到共三個檔案『server.key』、『server.csr』、『server.crt』請將三個檔案移到『\conf』目錄下。

7.打開『httpd.conf』檔案找到以下句並取消註解
LoadModule ssl_module modules/mod_ssl.so

8.因為預設建立的憑證在 windows 32 位元底下會發生錯誤(若安裝的apache 為 64 位元版則不用理會這段命令),所以需要輸入下列指令並重新將 server.key 複製到 http\conf 資料夾底下
ren server.key server.key.org
openssl rsa -in server.key.org -out server.key

9.在 httpd.conf 文件中新增下列設定
NameVirtualHost *:80
<VirtualHost *:80>
    ServerName 網址
    DocumentRoot "C:/你的web/www"
</VirtualHost>
NameVirtualHost *:443
<VirtualHost *:443>
    ServerName 網址
    DocumentRoot "C:/你的web/www"
    SSLEngine on
    SSLCertificateFile "C:\你的web\apache\conf\server.crt"
    SSLCertificateKeyFile "C:\你的web\apache\conf\server.key"   
    SSLCertificateChainFile "C:\你的web\apache\conf\server.crt"
</VirtualHost>

10.利用 httpd 檢測語法指令檢查設定參數
httpd -t

11.關閉 windows 防火牆,開啟瀏覽器 https://{網址名稱}
頁: [1]
查看完整版本: 啟動 Windows 版 Apache 的 HTTPS (SSL) 功能