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
How to Install Python (64 bit or 32 bit)
Mercurial IIS 7 Installation
Configure IIS 7 as Repository Host
IIS ISAPI Configuration
IIS Fast CGI Configuration
How to Setup Python on Windows Server 2008 R2
- 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.
- Download Python Installer
- Run Python Installer
- Accept the default installation path of C:\Python2X
- Add Python to Environment Path
- Follow this step to ensure python.exe can run without it’s full path c:\python2X\Python.exe
- Bring up System Window (Right Click My Computer -> Properties or Control Panel – >System and Security -> System)
- Click on Advanced System Settings -> then Environment Variables in popup window
- Under system variables -> Click to edit ‘Path’
- Append the Python path to existing values such as ;c:\python26\; or ;c:\python27\; – note values are separated with a ; (semicolon).
- 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)
- 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
- Download PyWin32 Installer (Pay attention to match step 1)
- Run PyWin32 Installer
- This will install into the \Lib\site-packages\ directory
Install & Build Mercurial Source Code
- 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\
- 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 setup.py --pure build_py -c -d . build_ext -i build_mo --force
python.exe setup.py --pure install --force
- This will build and install Mercurial into the Python install folder eg. C:\Python27\Lib\site-packages\mercurial
- This is done as per the “pure install” instructions
- 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:
rem Windows Driver script for Mercurial
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
- 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
- Download the ISAPI WSGI Handler Source Code
- There is a known installer issue where setup fails missing MSVCR71.dll so you have to build the source.
- (work around provided where you download the egg is not as flexible and could cause issue when using the latest version of Python 2.X)
- Unzip the source code .zip file into a temporary downloads directory Eg. C:\ISAPI-204-Source\
- Compile the ISAPI source code from DOS by running the following command from the ISAPI source directory:
- python setup.py install
- isapi_wsgi.py will be installed in the ‘site-packages’ directory of your Python installation. Eg. C:\Python27\Lib\site-packages\
- Download the hgwebdir_wsgi.py file from the Mercural source. This can be found in the contrib\win32 directory
- isapi_wsgi.py is a Python script that is configured to compile into the _hgwebdir_wsgi.dll file
HgWeb ISAPI Configuration
- Create the file folder that will be hosting your Mercurial IIS7 repository website. Eg. c:\inetpub\hg\
- Copy hgwebdir_wsgi.py file to the website folder.
- Open hgwebdir_wsgi.py 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
- Set the hgweb config value to the website folder.
hgweb_config = r'c:\inetpub\hg\hgweb.config'
- Unless you are using a virtual application/directory set the path prefix to zero then save/close the file.
path_prefix = 0
- Create a new hgweb.config file in the website folder with the following content
/ = c:\path-to-desired-repository-folder-location\*
- Open a DOS prompt and set the directory to your website folder. Eg. c:\inetpub\hg\
- Execute the following to compile a DLL called _hgwebdir_wsgi.dll
- 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.
- Create a new website in IIS Manager pointing it to the website folder. Be sure to select the Mercurial-Repo application pool.
- Open up the Handler Mappings for your new web site.
- Add a new “Wildcard Script Map” with the Executable location pointing to the _hgwebdir_wsgi.dll in the website folder. Name it Mercurial-ISAPI
- Click the OK button, and click YES to allow the ISAPI extension.
- 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.