How to setup Mercurial 1.8 HgWeb Repository hosted on IIS 7.5 with Windows Server 2008 R2 (x86 | x64 bit) and Python 2.6 or Python 2.7 with ISAPI or CGI

After reading existing tutorials Setting up Mercurial server in IIS7 using ISAPI-WSGI and
Setting up a Mercurial server on IIS7 via Fast CGI then receiving a number of Python errors including Failed to import callback module ‘hgwebdir_wsgi’ I decided to author this updated guide to setting up Mercurial 1.8 on IIS 7.5 (x86 or x64) using Python 2.6 or Python 2.7.

There are a number of problems with the existing instructions due to changes in Mercurial 1.7+. Unfortunately all other existing instructions fail under Mercurial 1.7.2+ as well they force you to install 32 bit versions of Mercurial / Python even on 64 bit systems – this is no longer an issue, so enjoy true 64 bit goodness!

This guide will show you how to successfully setup Mercurial 1.7 / 1.8 (either 64 bit or 32 bit versions) HgWeb hosted repositories on IIS7 and will allow you to use either Python 2.6 or the current version, Python 2.7.2 – look no further – this guide works :) Please donate your mouse button to support this information ;)

How To Install and Setup Mercurial 1.8 on IIS 7

  1. How to Install Python (64 bit or 32 bit)

  2. Install PyWin32

  3. Mercurial IIS 7 Installation

  4. Configure IIS 7 as Repository Host

    • IIS ISAPI Configuration
    • IIS Fast CGI Configuration

How to Setup Python on Windows Server 2008 R2

  1. Choose a Python version (2.6 or 2.7) and architecture (32bit or 64bit)
    • It’s very important to be consistent when choosing a Python version (2.6 or 2.7) & architecture (x86 or x64).
    • I suggest using Python 2.7.2+
      Note: Python 3.X is NOT supported by Mercurial
    • If you are on a 64 bit version of Windows then I suggest you take advantage of x64 bit. Otherwise you will have to choose 32 bit.
  2. Download Python Installer
  3. Run Python Installer
    • Accept the default installation path of C:\Python2X
  4. Add Python to Environment Path
    1. Follow this step to ensure python.exe can run without it’s full path c:\python2X\Python.exe
    2. Bring up System Window (Right Click My Computer -> Properties or Control Panel – >System and Security -> System)
    3. Click on Advanced System Settings -> then Environment Variables in popup window
    4. Under system variables -> Click to edit ‘Path’
    5. Append the Python path to existing values such as ;c:\python26\; or ;c:\python27\; – note values are separated with a ; (semicolon).
    6. Test the python executable is in the environment path by opening a DOS prompt and typing ‘Python’ then CTR-Z to quit out of Python.

Install Python for Windows Extensions (PyWin32)

  1. Choose a PyWin32 Build (Build216 has been tested and verified)
    • It’s very important to be consistent choosing the PyWin32 version matching your Python version & architecture (x86 or x64).
    • For example if you installed the 64 bit version of Python 2.7 then you need to download the PyWin32 64 bit installer for Python 2.7
  2. Download PyWin32 Installer (Pay attention to match step 1)
  3. Run PyWin32 Installer
    • This will install into the \Lib\site-packages\ directory

Install & Build Mercurial Source Code

  1. Download the Mercurial 1.8.4 Source Code Release
    • Don’t waste time monkeying around with the installers. You need a version of Mercurial matching your Python install so get the source code!
    • You will need to unzip the pain in the ass .tar.gz compressed source code file. This is easy on windows using 7zip
    • You should now have opened the source code using 7Zip and copied to a folder such as c:\mercurial-source-1.8.4\
  2. Build Mercurial Source Code
    • This is done as per the “pure install” instructions
      • Open a dos window and browse to the source folder (c:\mercurial-source-1.8.4\ in this example)
      • Run the following commands:
      • python.exe --pure build_py -c -d . build_ext -i build_mo --force
      • python.exe --pure install --force
      • This will build and install Mercurial into the Python install folder eg. C:\Python27\Lib\site-packages\mercurial
  3. You now have a working Mercurial install – however you will want the hg command to work from anywhere. Currently you have to call the hg command directly via c:\Python2X\Scripts\hg. This can be fixed a few ways.
    • Add the c:\python2x\scripts\ path to the environment variable similar to the python steps above
    • Copy the c:\python2x\scripts\hg.bat file into the c:\python2X\ folder which is already added to windows Path var – then update the hg.bat file as follows:

    • @echo off

      rem Windows Driver script for Mercurial


      set HG=%~f0

      rem Use a full path to Python (relative to this script) as the standard Python

      rem install does not put python.exe on the PATH...

      rem %~dp0 is the directory of this script

      "%~dp0python" "%~dp0\scripts\hg" %*


    • You can test this works by typing hg from any directory in dos and you will see the hg instructions

Setup Mercurial HgWeb Host on IIS 7

  1. Choose an IIS7 plugin technology
    • ISAPI is the fastest and considered a superior option
    • CGI is an older and equally as capable option
    • If you are unsure use the ISAPI option

ISAPI Configuration Instructions for Mercurial HgWeb

  1. Download the ISAPI WSGI Handler Source Code
  2. Unzip the source code .zip file into a temporary downloads directory Eg. C:\ISAPI-204-Source\
  3. Compile the ISAPI source code from DOS by running the following command from the ISAPI source directory:
    • python install
  4. will be installed in the ‘site-packages’ directory of your Python installation. Eg. C:\Python27\Lib\site-packages\
  5. Download the file from the Mercural source. This can be found in the contrib\win32 directory
    • is a Python script that is configured to compile into the _hgwebdir_wsgi.dll file
  6. HgWeb ISAPI Configuration
    1. Create the file folder that will be hosting your Mercurial IIS7 repository website. Eg. c:\inetpub\hg\
    2. Copy file to the website folder.
    3. Open for editing by right clicking it and using the Python editor (notepad will not open the file properly due to the invalid new line characters)
      • You should see a pretty version of file with colors from the IDLE or Pythonwin editors
    4. Set the hgweb config value to the website folder. hgweb_config = r'c:\inetpub\hg\hgweb.config'
    5. Unless you are using a virtual application/directory set the path prefix to zero then save/close the file.path_prefix = 0
    6. Create a new hgweb.config file in the website folder with the following content [paths]
      / = c:\path-to-desired-repository-folder-location\*
    7. Open a DOS prompt and set the directory to your website folder. Eg. c:\inetpub\hg\
    8. Execute the following to compile a DLL called _hgwebdir_wsgi.dll python
    9. Create a new application pool in IIS Manager called “Mercurial-Repo”
      • Set the .net framework version to “No Managed Code”
      • *Only if* the installation is 32 bit then change the “Enable 32-bit Applications” to true.
    10. Create a new website in IIS Manager pointing it to the website folder. Be sure to select the Mercurial-Repo application pool.
    11. Open up the Handler Mappings for your new web site.
    12. Add a new “Wildcard Script Map” with the Executable location pointing to the _hgwebdir_wsgi.dll in the website folder. Name it Mercurial-ISAPI
    13. Click the OK button, and click YES to allow the ISAPI extension.
    14. Assuming you have followed all directions carefully browsing to the newly created website should serve the empty repositories listing!

This has been a comprehensive approach to configuring Mercurial on IIS 7 and Windows Server 2008 via building the source code directly to ensure the version of Mercurial setup matches the version of Python as well the system architecture. This is the only tutorial I know of that allows the 64 bit version of Mecurial 1.8+ to be setup.

Please post a comment if you have *any* issues with following these directions. I have invested time and care into testing this process and keeping these instructions current. Thank you.