用 IIS 搭建 mercurial server mercurial server 环境及软件安装 设置 IIS 服务器支持 python 模块 创建网站 配置 mercurial server 配置 URL 重定向 设置匿名访问权限 总结

对于代码管理工具,更多的人可能对 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:Python27Libsite-packagesmercurial 目录是否被正确安装!

注意,python 和 sercurial server 必须保持相同的架构,不要一个安装 x86 另一个安装 x64。

设置 IIS 服务器支持 python 模块

在 IIS 管理器中选择 IIS server,双击”ISAPI 和 CGI 限制”,添加一项新的扩展:

用 IIS 搭建 mercurial server
mercurial server
环境及软件安装
设置 IIS 服务器支持 python 模块
创建网站
配置 mercurial server
配置 URL 重定向
设置匿名访问权限
总结

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

C:Windowssystem32inetsrvappcmd set config /section:isapiCgiRestriction /+"[path='C:Python27python.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:Python27python.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

在网站的根目录下添加 hgweb.config 文件,内容如下:

[collections]
C:
epos = C:
epos

[web]
push_ssl = false
allow_push = *

在网站的根目录下添加 hgweb.cgi 文件,内容如下:

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

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

注意,请按实际情况配置 config 的路径。
这就 OK 了,让我们在 c: epos 目录下初始化一个库然后访问 http://localhost:81/hgweb.cgi 看看:

用 IIS 搭建 mercurial server
mercurial server
环境及软件安装
设置 IIS 服务器支持 python 模块
创建网站
配置 mercurial server
配置 URL 重定向
设置匿名访问权限
总结

配置 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:Python27python.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
mercurial server
环境及软件安装
设置 IIS 服务器支持 python 模块
创建网站
配置 mercurial server
配置 URL 重定向
设置匿名访问权限
总结

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

总结

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