Development / Systems Integration

Mercurial on IIS 7 – x86 or x64

A guide on how to install and configure Mercurial (hg) with IIS on Windows 2008R2. For those folks that are looking to manage a central repositories for hg, and are mostly a Microsoft shop, it can be challenging to get something other than Team Foundation Server (TFS) approved, let alone deployed and maintained by your Systems team. This guide should make the installation process much more accessible.

Setup Python on Windows Server 2008 R2
Install Python for Windows Extensions (PyWin32)
Install and Build Mercurial
>Using the “pure build” instructions
>Using the alternate build instructions
Setup Mercurial HgWeb Host on IIS 7.x using ISAPI
Test the Mercurial HgWeb site


This guide was built using the articles written by Jeremy Skinner, Adam Boddington, Claus Conrad, FireGarden and Matt Hawley.

Update 2012-09-16: Thanks to Henrik Stuart (hstuart) for his feedback on this document. He’s done a lot of the development on the tools used here, and is very generous with his time. I’ve updated the section outlining the change to the System Path Environment Variable to include the Python scripts directory (e.g. C:\Python27\scripts), and removed the deprecated alterations to the hg.bat file.

I am solely responsible for any mistakes by omission or commission.

Setup Python on Windows Server 2008 R2

Choose a Python Version

  1. It is critical that the Python version (2.6 or 2.7) and architecture (x86 or x64) be consistent throughout the process
  2. The recommendation is to run Python 2.7 x64
  3. NOTE: Python 3.x is not supported by Mercurial as of this writing.

Download Python Installer

  1. Python 2.6.6
  2. Python 2.7.3

Run the Python Installer

  1. Accept the default installation path of C:\Python2x and accept all default options.



Add Python and Python\scripts to System Environment Path

  • The following steps are necessary in order to ensure python.exe, and the python scripts can run from anywhere.
  1. Open the System Control panel applet
  2. Click on Advanced Settings, then Environment Variables in popup
  3. Under system variables, click to edit “Path”
  4. Append the Python path to existing values, such as “;C:\Python27\;C:\Python27\scripts\;” or “;C:\Python26\;C:\Python26\scripts\;” – note that values are separated by semi-colons.

    Remote Desktop Manager [HgWeb Sample]_2012-09-11_22-22-35

  5. Test the Python executable is accessible in the environment by opening an Administrative Command Prompt and typing “python” – then Ctrl-Z to quit out of python if successful.

Install Python for Windows Extensions (PyWin32)

Choose a build for PyWin32 (links provided to build 217)

  • It is critical that the PyWin32 build match exactly the installed Python version (2.6 or 2.7) and the architecture (x86 or x64)

Download the PyWin32 Installer

  1. PyWin32 for Python 2.6
  2. PyWin32 for Python 2.7

Run PyWin32 Installer

  1. Installs into the Python install folder at C:\Python2x\Lib\site-packages\ directory.
  2. The package installer will find the previously installed Python installation.


Install and Build Mercurial Source

Download the Mercurial 2.3.1 Source release

  • Do NOT use the installers. The source allows for an exact pairing of the Mercurial build with the version of Python, as well as the paths to the Python libraries. This is a critical component of this recipe, and cannot be overstated.

    Mercurial SCM - Google Chrome_2012-09-11_16-44-26

  • Use 7zip to unzip the .tar.gz archive of the source. Place the source in a working directory for building – preferably one without spaces in the path (e.g. C:\Users\Administrator\Documents\Development\mercurial-2.3.1)

    Remote Desktop Manager [HgWeb Sample]_2012-09-11_22-34-28

Build and Install Mercurial from Source

>Using the “pure build” instructions.
  1. Open an Administrative Command Prompt and browse to the Mercurial source folder.
    • Run the following commands:

      python –pure build_py -c -d . build_ext -i build_mo –force
      python –pure install –force

      Remote Desktop Manager [HgWeb Sample]_2012-09-11_22-37-29

      • This will build and install Mercurial into the Python install folder at C:\Python2x\Lib\site-packages\mercurial
>Using the alternate build instructions
  1. Get Microsoft Visual C++ Express Edition 2008
    • Download and run vcsetup.exe, deselecting the Silverlight and SQL 2008 Express radio buttons – those components are not necessary for this process. - Remote Desktop Connection_2012-09-17_21-39-13
    • Registration is optional, but necessary if the compiler will be used past the 30 day evaluation period. Registration is free.
  2. Download and install the Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1. (“Microsoft Windows SDK for Windows 7 and .NET Framework 4” will NOT work for this process).
    • Download and run winsdk_web.exe
  3. Setup the 64 bit Build and Install Environment from an Administrative Command Prompt (keep the command prompt open – this is where the work will be done).
    • Copy the vcvars64.bat file to amd64\vcvarsamd64.bat with the following command:

      copy "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat" "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64\vcvarsamd64.bat"

    • Setup the SDK Environment for x64 (known as amd64 or win-amd64 in the Mercurial build internals) – include the double quotes:

      cmd.exe /E:ON /V:ON /K "C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\SetEnv.cmd" /xp /x64 /Release

    • Set the DISTUTILS_USE_SDK environment variable:

      set DISTUTILS_USE_SDK=true

  4. Setup the 32 bit Build and Install Environment from an Administrative Command Prompt (keep the command prompt open – this is where the work will be done).
    • Setup the SDK Environment by selecting the Visual Studio Command Prompt from the Microsoft Visual Studio 2008 Express Edition menu.
  5. The final step is to actually Build and Install Mercurial.
    • From the open Administrative Command Prompt, navigate to the Mercurial source folder, and run the following commands:

      python build
      python install


Setup Mercurial HgWeb Host on IIS 7.5 using ISAPI

Ensure IIS is installed, including CGI and Basic Authentication (Application Role, Web Server Services).

Remote Desktop Manager [HgWeb Sample]_2012-09-11_22-46-53

Download the ISAPI WSGI Handler Source

  • There is a known installer issue where setup fails missing the MSVCR71.dll, and the workaround using the “egg” is not as flexible and could cause issues with the latest version of Python, so building from source is required.
  • Use 7zip to unzip the archive of the source. Place the source in a working directory for building – preferably one without spaces in the path (e.g. C:\Users\Administrator\Documents\Development\isapi_wsgi-0.4.2)
  1. Build and Install ISAPI WSGI from Source
    • Run the following command from the Administrative Command Prompt in source directory:

      python install

      Remote Desktop Manager [HgWeb Sample]_2012-09-11_22-44-20

  2. will be installed in the Python install directory under C:\Python2x\Lib\site-packages\
  3. Create the folder that will be hosting the Mercurial Website (e.g. C:\inetpub\hg). Set permissions to this folder to mimic that of C:\inetpub\wwwroot.

    Remote Desktop Manager [HgWeb Sample]_2012-09-11_22-50-46

  4. Create the parent directory for hosting the Mercurial Repositories (e.g. C:\HgWeb\)
    • Set the permissions to the Mercurial Repositories directory to allow “Full Control” to the Programmers.
  5. Copy and edit the file from the Mercurial Source to the Mercurial Website Folder.
    • The file can be found in the contrib\win32 directory of the Mercurial Source (e.g. C:\Users\Administrator\Documents\Development\mercurial-2.3.1\contrib\win32).
    • Edit the file by right-clicking it and using a Python editor (notepad will NOT open the file properly due to unix line-endings). The file should have syntax highlighting within the IDLE or PythonWin editors.
      • Set the hgweb_config value to that of the website folder.

        hgweb_config = r’C:\inetpub\hg\hgweb.config’

      • Unless there is a parent directory (virtual/application directory), set the path_prefix to zero. For example, for http://server/hgweb/, path_prefix = 1, and http://server/, path_prefix = 0.

        path_prefix = 0

      • Save and close the file.

        Remote Desktop Manager [HgWeb Sample]_2012-09-11_22-53-45

  6. Create a new hgweb.config file in the website folder with the path to the parent directory of the Mercurial Repositories (e.g. C:\HgWeb\ contains the Mercurial Repositories). Also, set allow_push to all users. The server will therefore rely on the authentication token to allow or prevent the user from pushing code into the repository.

    / = C:\HgWeb\*

    allow_push = *

  7. Open an Administrative Command Prompt, and navigate to the Mercurial Website Directory (e.g. C:\inetpub\hg).
    • Execute the following command to compile a DLL in that directory called _hgwebdir_wsgi.dll.


      Remote Desktop Manager [HgWeb Sample]_2012-09-11_22-57-58

Configure the HgWeb ISAPI Website

  1. Create a new application pool in IIS Manager called “Mercurial-Repo”
    • Set the .NET Framework version to “No Managed Code”
    • If the architecture used during this process has been 32 bit, then change the “Enable 32-bit applications” to true.
  2. Create a new website in IIS Manager pointing it to the Mercurial Website Folder (e.g. C:\inetpub\hg). Set that website to use the “Mercurial-Repo” application pool.
    Remote Desktop Manager [HgWeb Sample]_2012-09-11_23-03-45Remote Desktop Manager [HgWeb Sample]_2012-09-11_23-05-29
  3. Open the Handler Mappings for the new website.
    Remote Desktop Manager [HgWeb Sample]_2012-09-11_23-09-36
  4. Add a new “Wildcard Script Map” with the Executable location pointing to the _hgwebdir_wsgi.dll in the Mercurial Website Folder (e.g. C:\inetpub\hg\_hgwebdir_wsgi.dll). Name it Mercuiral-ISAPI. Remote Desktop Manager [HgWeb Sample]_2012-09-11_23-10-32
    • Click OK, and click YES to allow the ISAPI extension.
  5. Open Authentication for the new website
    • Enable “Basic Authentication.”
    • Set the default domain and realm.
    • Disable “Anonymous Authentication”
  6. Install a self-signed SSL Certificate (or install a CA-provided certificate). This is important for assuring the clear-text password required by “Basic Authentication” is in-fact encrypted via the SSL/TLS channel between the client and the HgWeb Server.
    Remote Desktop Manager [HgWeb Sample]_2012-09-11_23-13-44
    • Set the Bindings for the Mercurial Website to use the certificate, and force HTTPS over port 443. Remote Desktop Manager [HgWeb Sample]_2012-09-11_23-14-33

Test the Mercurial HgWeb Site

Create test repository in the Mercurial Repository directory.

Navigate to https://localhost/.

  1. Authentication should be required.
  2. Once authenticated, the test repository should be displayed as a clickable link.

3 thoughts on “Mercurial on IIS 7 – x86 or x64

  1. Pingback: Error while installing Mercurial on IIS7 64bit: "DLL Load Failed: %1 is not a valid Win32 application"CopyQuery CopyQuery | Question & Answer Tool for your Technical Queries,CopyQuery, ejjuit, query, copyquery,, android doubt,

  2. Pingback: Error while installing Mercurial on IIS7 64bit: "DLL Load Failed: %1 is not a valid Win32 application" | BlogoSfera

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s