Thursday, March 10, 2011

Mercurial on IIS with basic Active Directories

Notes on how to install Mercurial on Windows Server 2008  with basic Active Directory authentication.

Credit to Jeremy Skinner and Ben Alabaster who wrote the original articles on this.
This is an updated combination of the 2 posts, using the latest compatible versions of Python and Mercurial, and one or 2 steps that wheren't included in either post.


  1. Download and install Mercurial (mercurial-1.8.0)
    1. Seems to require 32 bit even on 64 bit systems
    2. Select defaults
  2. Download and install Python (python-2.6.6)
    1. Seems to require 32 bit even on 64 bit systems
    2. Select defaults
    3. Add C:\Python26\ to system PATH
  3. Download and install MinGW (mingw-get-inst-20110211)
    1. Use pre-package repsoitory catalogues
    2. Install to C:\minGW
    3. Don't create start folder
    4. Select C++ compiler 
    5. Add C:\MinGW\bin to the system PATH
  4. Download Mercurial Python Library (http://mercurial.selenic.com/release/mercurial-1.8.tar.gz)
    1. Extract folder
    2. jump into the command line where you extracted the library to
    3. execute "python setup.py install build --compiler=mingw32"
  5. Install and setup IIS
    1. Server Manager > Roles > Add Roles > Install IIS
    2. Server Manager > Role Services > CGI (enable)
    3. Create hg directory
      1. mkdir c:\inetpub\wwwroot\hg
    4. IIS Manager > Sites > Default web Site > Refresh
    5. IIS Manager > Sites > Default web Site > Bindings > http port 40
    6. IIS Manager > Sites > Default web Site > hg > Handler Mappings > Add Script Map
      1. Request Path: *.cgi
      2. Executable: C:\Python26\python.exe
      3. Name: Python
      4. Yes on dialog box that appears
    7. IIS Manager > Sites > Default web Site  > START
  6. Setup the Mercurial Web
    1. mkdir c:\temp\hgClone
    2. cd c:\temp\hgClone
    3. hg clone http://selenic.com/report/hg
    4. copy hg\hgweb.cgi c:\inetpub\wwwroot\hg
    5. cd c:\
    6. rmdir /S /Q c:\temp\hgClone
    7. c:\>"c:\Program Files\Windows NT\Accessories\wordpad.exe" c:\inetpub\wwwroot\hg\hgweb.cgi
      1. Change the line "application = hgweb(config)" to "application = hgweb('c:\inetpub\wwwroot\hg\hgweb.config')"
    8. extract C:\Program Files\Mercurial\library.zip into c:\inetpub\wwwroot\hg\library\
    9. copy C:\Program Files\Mercurial\templates\ to c:\inetpub\wwwroot\hg\
  7. Initialize Repositories
    1. mkdir c:\repositories
    2. mkdir test 
    3. hg init test
  8. Configure
    1. add to C:\inetpub\wwwroot\hg\hgweb.config:
      1. [collections]
        C:\repositories = C:\repositories
  9. URL Rewriting
      1. download http://www.iis.net/expand/URLRewrite
    1. install
    2. IIS Manager > Sites > Default web Site > URL Rewrite > new Rule > blank Rule
      1. name: Rewrite to hgweb
      2. Match URL
        1. Requested URL: Matches the Pattern
        2. Using Wildcards
        3. Pattern: *
        4. ignore case: yes
      3. Conditions
        1. Logical Grouping: Match All
        2. Add Condtion
          1. Condition input: {REQUEST_FILENAME}
          2. Check if input string: Is Not a File
      4. Action
        1. Action Type: Rewrite
        2. Action Properties
          1. Rewrite URL: /hg/hgweb.cgi/{R:1}
          2. Append Query String: Yes
          3. Log rewritten URL: No
        1. Stop processing of subsequent Rules: No
  10. Authorisation Settings
    1. IIS Manager > Sites > Default web Site > Authentication
      1. Disable Everything
      2. Enable Basic
      3. Edit Basic
        1. default domain: yourdomain.com
    2. Edit c:\inetpub\wwwroot\hg\hgweb.config
      1. add line
        1. push_ssl = false
      2. add line 
        1. allow_push = <comma seperated list of developer domain names>

6 comments:

  1. I was getting this error

    HTTP Error 502.2 - Bad Gateway
    The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "".

    I fixed it by adding -u "%s" to the end of the script map for the python executable c:\Python26\python.exe -u "%s"

    ReplyDelete
  2. Thank you! This (almost) worked for me. Much like the other Anonymous commentor said, I made it work by changing the *.cgi handler to use: C:\Python26\python.exe -u "%s"

    Also, I had to set my Application Pool to allow for 32 bit applications.

    ReplyDelete
  3. I don't get it. Why do i have to create a repository on the ROOT of the C-drive? I don't see the repositories when I go to the hgweb.cgi file in my browser. I guess that the goal of implementing mercurial in iis7 is that they're remotley accessable.

    ReplyDelete
  4. Thanks for your post.

    Note: I was able to use the x64 versions Python 2.6.6, Hg 1.8.3, and URL Rewrite.

    So the 32-bit requirement may be a thing of the past for x64 systems.

    ReplyDelete
  5. I had to alter the rewrite match pattern from * to hg/* to have working pictures/graphs if I clicked a repository.

    ReplyDelete
  6. ...or else you can try HgLab ( http://hglabhq.com ), which has ActiveDirectory support out of the box.

    ReplyDelete