如何在 IIS 上搭建 mercurial server


mercurial server

對於代碼管理工具,更多的人可能更熟悉 Git 一些(Git 太火了)。其實另外一款分布式代碼管理工具也被廣泛的使用,它就是 mercurial。多人協作時,最好能夠通過創建一個 mercurial server 對用戶進行權限認證,同時也會方便持續集成。

關於創建 mercurial server 的步驟,mercurial 官方的 wiki 有說明,網上也有很多朋友分享了自己的創建過程。但筆者在創建的過程中還是頗費了一番周折才最終成功,所以也在此分享一下自己的經驗,希望對朋友們有所幫助。

一、環境及軟件安裝

筆者使用的操作系統為 Server2012R2 x64 Standard 中文版。

首先,在安裝其他工具前,需要先安裝 IIS。安裝 IIS 時需要注意,一定要把 CGI 和 ISAPI 這兩個選項都勾選上。

然后,安裝 Python,使用默認設置安裝 python 2.7.x。

最后,安裝 mercurial server,請從這里在這里下載 mercurial server 的安裝包並安裝,安裝完成后檢查 C:\Python27\Lib\site-packages\mercurial 目錄是否被正確安裝!

注意,python 和 sercurial server 必須保持相同的架構,不要一個安裝 x86 另一個安裝 x64。

二、設置 IIS 服務器支持 python 模塊

在 IIS 管理器中選擇 IIS server,雙擊”ISAPI 和 CGI 限制”,添加一項新的擴展:

1

喜歡使用命令行的同學也可以通過一行命令直接搞定:

C:\Windows\system32\inetsrv\appcmd set config /section:isapiCgiRestriction /+"[path='C:\Python27\python.exe -u %22%s%22',description='Python',allowed='True']"

三、創建網站

在 IIS 中創建一個新的網站,端口綁定81(80端口已被默認網站占用)。在網站的根目錄中添加 web.config 文件,web.config 文件的內容為:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="Python" path="*.cgi" verb="*" modules="CgiModule" scriptProcessor="C:\Python27\python.exe -u &quot;%s&quot;" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
  </system.webServer>
</configuration>

需要注意文件中 python.exe 的路徑,請根據自己機器上的安裝目錄進行配置。

網站的基本設置已經完成,下面寫個簡單的測試文件檢查一下網站能否正常工作。

在網站的根目錄下創建 test.cgi 文件,文件內容如下:

print 'Status: 200 OK'
print 'Content-Type: text/html'
print
print '<html><body><h1>Hello world!</h1></body></html>'

在瀏覽器中訪問 http://localhost:81/test.cgi,如果看不到”Hello world!”,請檢查前面的步驟是否執行正確。

四、配置 mercurial server

1. 在網站的根目錄下添加 hgweb.config 文件,內容如下:

[collections]
C:\repos = C:\repos

[web]
push_ssl = false
allow_push = *

2. 在網站的根目錄下添加 hgweb.cgi 文件,內容如下:

#!/usr/bin/env python
# Path to repo or hgweb config to serve (see 'hg help hgweb')
config = "xxxxx\hgweb.config"

from mercurial import demandimport; demandimport.enable()
from mercurial.hgweb import hgweb, wsgicgi
application = hgweb(config)
wsgicgi.launch(application)

注意,請按實際情況配置 config 的路徑。

這就 OK 了,讓我們在 c:\repos 目錄下初始化一個庫然后訪問 http://localhost:81/hgweb.cgi 看看:

2

五、配置 URL 重定向

每次都要在 URL 中輸入 /hgweb.cgi,一來不方便,二來總感覺怪怪的。能不能輸入 http://localhost:81 就可以正常訪問呢?當然可以,只需添加一個重定向的配置就可以了。

首先,需要下載並安裝IIS的插件:http://www.iis.net/downloads/microsoft/url-rewrite

然后,在 web.config 文件中添加 rewrite 元素,新的 web.config 文件內容為:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="Python" path="*.cgi" verb="*" modules="CgiModule" scriptProcessor="C:\Python27\python.exe -u &quot;%s&quot;" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="rewrite to hgwebdir" patternSyntax="Wildcard">
          <match url="*" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="hgweb.cgi/{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

好了,現在訪問下 http://localhost:81 試試,和訪問 http://localhost:81/hgweb.cgi 是一樣的。

六、設置匿名訪問權限

默認情況下我們已經可以使用匿名權限從服務器克隆庫並進行操作了,但是當你執行 hg push 命令時會收到一個 HTTP Error 502: Bad Gateway 的錯誤。出現這個錯誤,是因為匿名用戶沒有修改服務器上文件的權限,所以我們需要給匿名身份驗證設置一個有修改文件權限的用戶。

3

現在就可以正常執行 push 操作了。

總結,相比其他工具的一鍵式安裝與配置,mercurial server 的安裝和配置稍顯復雜。我們這里只是配置了最簡單的匿名訪問,並不支持 ssl,不過這在局域網中基本也夠用了。

 

相關閱讀:

最全的Windows Azure學習教程匯總

Azure Blob Storage 基本用法 -- Azure Storage 之 Blob

Azure Queue Storage 基本用法 -- Azure Storage 之 Queue

Azure File Storage 基本用法 -- Azure Storage 之 File

Azure Table storage 基本用法 -- Azure Storage 之 Table

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM