close

Git over HTTPS on Windows

from http://huan-lin.blogspot.tw/2011/05/git-over-https-on-windows.html

在<Git Server on Windows 安裝手記> 中,我整理了在 Windows 環境上安裝 Apache HTTP Server 和 Git for Windows 的操作步驟。這篇則是要讓 Git(其實主要是 Apache)支援 HTTPS 加密協定。也就是說,連同這篇的設定,你的 Git 伺服器就能夠同時支援 HTTP 和 HTTPS 啦!

操作步驟如下:

Step 1: 修改  Apache 組態

用文字編輯器開啟 Apache 的組態檔 httpd.conf,分別找到底下這兩行設定,並將前面的 '#' 字元刪除(也就是取消註解)。

#LoadModule ssl_module modules/mod_ssl.so
#Include conf/extra/httpd-ssl.conf


第二行表示要加入 conf\extra\httpd-ssl.conf,故接著要開啟這個組態檔,確認其中包含以下設定:

....
Listen 443
....
SSLMutex default
....
<virtualhost _default_:443="">
DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
ServerName localhost:443
ServerAdmin 你的電子郵件信箱
....
SSLEngine on
....
SSLCertificateFile "C:/Program Files/Apache Software Foundation/Apache2.2/conf/server.crt"
SSLCertificateKeyFile "C:/Program Files/Apache Software Foundation/Apache2.2/conf/server.key"
....
</virtualhost>


注意最後兩行的 server.crt 和 server.key。下個步驟就是要產生這兩個檔案。

Step 2: 建立憑證和金鑰

開啟命令列視窗,將現行目錄切換至 Apache 的安裝目錄下的 bin。然後依序輸入以下指令(你的 Apache 必須是內建 OpenSSL 的版本):

openssl req -config ..\conf\openssl.cnf -new -out server.csr

openssl rsa -in privkey.pem -out server.key

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


第一道指令會要求你輸入一些基本資料,如國家代碼、城市、電子郵件信箱等等。參考下圖:


最後一道指令的 "-days 3650" 參數,是指定這個伺服器憑證的有效期限為十年。
若上述指令都執行成功,應該會在 bin 目錄下產生三個檔案,分別是 server.csr、server.key、和 server.crt。

將 server key 與 server.crt 搬移至 Apache 的 conf 目錄下,然後重啟 Apache 服務。

Step 3: 修改 Git 參數

開啟 Git Bash,輸入以下指令:

$ git config --global http.sslverify false


這個指令要在用戶端電腦執行。也就是說,每個要透過 https 協定存取版本庫的用戶端電腦都要先下這個指令。少了這個步驟,在執行 git clone 命令來取出版本庫時,很可能會出現錯誤訊息:

error: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify faile
d while accessing https://localhost/Projects/test/info/refs

fatal: HTTP request failed


或出現另一種錯誤:

error: error setting certificate verify locations:
  CAfile: \bin\curl-ca-bundle.crt
  CApath: none
 while accessing https://localhost/Projects/test/info/refs


我試過網路上找到的幾種解法,最後發現還是把 http.sslverify 參數設為 false 這個方法最為簡單有效。

測試看看

現在你的 Git 伺服器應該可以同時支援 HTTP 和 HTTPS 了。你可以開啟 Git Bash,然後用 git clone 測試看看,例如:

$git clone http://michael@localhost:8080/Projects/Test


或使用加密協定:

$git clone https://michael@localhost/Projects/Test


直接指定帳號密碼:

$git clone https://michael:密碼@localhost/Projects/Test



最後,別忘了防火牆要開放 TCP 443 port 喔!



參考資料

arrow
arrow
    全站熱搜

    zer931 發表在 痞客邦 留言(0) 人氣()