怎么在 IIS 上搭建 mercurial server

怎么在 IIS 上搭建 mercurial server

如何在 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 限制”,添加一项新的扩展:

怎么在 IIS 上搭建 mercurial server

喜欢使用命令行的同学也可以通过一行命令直接搞定:

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 文件的内容为:

[html] view plain copy
 
 怎么在 IIS 上搭建 mercurial server怎么在 IIS 上搭建 mercurial server
  1.   
<?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 看看:

怎么在 IIS 上搭建 mercurial server

五、配置 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 的错误。出现这个错误,是因为匿名用户没有修改服务器上文件的权限,所以我们需要给匿名身份验证设置一个有修改文件权限的用户。

怎么在 IIS 上搭建 mercurial server

现在就可以正常执行 push 操作了。

 

总结,相比其他工具的一键式安装与配置,mercurial server 的安装和配置稍显复杂。我们这里只是配置了最简单的匿名访问,并不支持 ssl,不过这在局域网中基本也够用了。