﻿{"id":910,"date":"2012-07-16T21:58:30","date_gmt":"2012-07-16T13:58:30","guid":{"rendered":"http:\/\/nick.workao.org\/?p=910"},"modified":"2012-07-16T21:58:40","modified_gmt":"2012-07-16T13:58:40","slug":"setting-up-a-mercurial-server-under-iis7-on-windows-server-2008-r2","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/other\/910","title":{"rendered":"Setting up a Mercurial server under IIS7 on Windows Server 2008 R2"},"content":{"rendered":"<p>This guide walks you through setting up a Mercurial server under IIS7 on Windows Server 2008 R2.<\/p>\n<p><strong>Note:<\/strong>\u00a0This post uses Mercurial 1.4.3 and Python 2.5.4, although this process will also work with Mercurial 1.5 and Python 2.6.4<\/p>\n<h3>Install Mercurial<\/h3>\n<p>First you\u2019ll need\u00a0<a href=\"http:\/\/mercurial.selenic.com\/\">to download<\/a>\u00a0and install Mercurial. I won\u2019t walk through this as it\u2019s a simple case of pressing &#8220;next&#8221; several times.<\/p>\n<h3>Install Python<\/h3>\n<p>Next you\u2019ll need to install Python. Note that you need to use the same version of Python that was used to build Mercurial. This guide uses Mercurial 1.4.3 with Python 2.5.4, but if you\u2019re installing Mercurial 1.5 then you\u2019ll need to use Python 2.6 instead.<\/p>\n<p><strong>Be sure to install the x86 version of Python even if you\u2019re running on an x64 system.<\/strong><\/p>\n<h3>Get hgwebdir<\/h3>\n<p>Next you\u2019ll need to download hgwebdir.cgi. This is the python script that will allow serving Mercurial repositories through a web server.<\/p>\n<p>Hgwebdir is part of the Mercurial source code, so you\u2019ll need to download the source package to get it. This can be found\u00a0<a href=\"http:\/\/mercurial.selenic.com\/downloads\/\">on the Mercurial site<\/a>\u00a0or you can check out the hg source by running the following command:<\/p>\n<div>\n<div>\n<pre>hg clone http:\/\/selenic.com\/repo\/hg<\/pre>\n<\/div>\n<\/div>\n<p>Once downloaded, hgwebdir.cgi is in the root of the source distribution.<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358312mB.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"01-hgwebdir\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358312mB.jpg\" alt=\"\" width=\"364\" height=\"597\" \/><\/a><\/p>\n<h3>Install IIS<\/h3>\n<p>Under Windows Server 2008 you can install IIS under the Server Manager and clicking &#8220;Add Roles&#8221;. Proceed through the wizard and select the &#8220;Web Server (IIS)&#8221; role.<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135831ZZ5.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"02-iisrole\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135831ZZ5.jpg\" alt=\"\" width=\"782\" height=\"573\" \/><\/a><\/p>\n<p>Under &#8220;Role Services&#8221; ensure that you enable Basic Authentication as well as CGI extensibility.<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135832oEs.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"03-iis-cgi\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135832oEs.jpg\" alt=\"\" width=\"776\" height=\"571\" \/><\/a><\/p>\n<h3>Configure Python for IIS<\/h3>\n<p>First, create a new directory under the wwwroot directory (C:inetpubwwwroot). I\u2019m going to call it &#8220;hg&#8221;.<\/p>\n<p>In the &#8220;Handler mappings&#8221; section for this directory select &#8220;Add Script Map&#8221;:<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358320tw.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"04-iis-handlermap\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358320tw.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>Next, enter\u00a0<strong>*.cgi<\/strong>\u00a0as the Request Path and the Executable Path should be set to<strong>c:Python25python.exe -u &#8220;%s&#8221;<\/strong>. Enter &#8220;Python&#8221; as the Name.<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135833Q1w.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"05-iis-scriptmap\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135833Q1w.jpg\" alt=\"\" width=\"394\" height=\"323\" \/><\/a><\/p>\n<p>At this point, you can test whether Python is working properly by creating a simple python script:<\/p>\n<div>\n<div>\n<pre>print 'Status: 200 OK'\nprint 'Content-type: text\/html'\nprint\n\nprint '<html><head>'\nprint ''\nprint '<h1>It works!<\/h1>'\nprint ''\nprint ''<\/pre>\n<\/div>\n<\/div>\n<p>Save this in the directory that you created (C:inetpubwwwroothg) as test.cgi. Now, when you point your browser to <a href=\"http:\/\/localhost\/hg\/test.cgi\">http:\/\/localhost\/hg\/test.cgi<\/a> you should see the following output:<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135833JhP.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"06-iis-scriptmap\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135833JhP.jpg\" alt=\"\" width=\"652\" height=\"425\" \/><\/a><\/p>\n<h3>Enabling hgwebdir.cgi<\/h3>\n<p>First, copy hgwebdir.cgi (that you downloaded in step 3) and paste it into c:inetpubwwwroothg. Open this file in a text editor and scroll down to the end. The last lines should look like this:<\/p>\n<div>\n<div>\n<pre>application = hgwebdir('hgweb.config')\nwsgicgi.launch(application)<\/pre>\n<\/div>\n<\/div>\n<p>Change the first line to explicitly specify the path to your hg directory:<\/p>\n<div>\n<div>\n<pre>application = hgwebdir('c:inetpubwwwroothghgweb.config')\nwsgicgi.launch(application)<\/pre>\n<\/div>\n<\/div>\n<p>Next, you\u2019ll need to unzip the Mercurial library into c:inetpubwwwroothg. This can be found in<strong>Library.zip<\/strong>\u00a0under the\u00a0<strong>c:program files (x86)Mercurial<\/strong>\u00a0directory.<\/p>\n<p>You\u2019ll now need to copy the hgweb templates directory into c:inetpubwwwroothg. This is located in the root of the Mercurial installation directory (C:program files (x86)Mercurial)<\/p>\n<p>Finally, create a file called\u00a0<strong>hgweb.config<\/strong>\u00a0in c:inetpubwwwroothg. This file can be empty for now (we\u2019ll be putting something in it shortly).<\/p>\n<p>At this point, visiting\u00a0<strong><a href=\"http:\/\/localhost\/hg\/hgwebdir.cgi\">http:\/\/localhost\/hg\/hgwebdir.cgi<\/a><\/strong>\u00a0will show you an empty repository page:<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135834y6r.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"07-hgwebdir-empty\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135834y6r.jpg\" alt=\"\" width=\"711\" height=\"529\" \/><\/a><\/p>\n<h3>Configuring Repositories<\/h3>\n<p>Now you\u2019ll need to create some repositories to publish. To do this, create a directory in the root of the C: drive called &#8220;Repositories&#8221;. This is where our repositories are going to be stored.<\/p>\n<p>Next, I\u2019m going to create a &#8220;test&#8221; repository by issuing the following commands:<\/p>\n<div>\n<div>\n<pre>cd c:repositories\nmkdir test\nhg init test<\/pre>\n<\/div>\n<\/div>\n<p>Now we have a repository created, we need to tell hgwebdir where to find it. We can do this by opening up the hgweb.config file we created earlier and adding the following lines:<\/p>\n<div>\n<div>\n<pre>[collections]\nC:repositories = C:repositories<\/pre>\n<\/div>\n<\/div>\n<p>Now, visiting <a href=\"http:\/\/localhost\/hg\/hgwebdir.cgi\">http:\/\/localhost\/hg\/hgwebdir.cgi<\/a> should display our &#8220;test&#8221; repository<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358345Gb.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"08-display-repositories\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358345Gb.jpg\" alt=\"\" width=\"643\" height=\"572\" \/><\/a><\/p>\n<p>At this point it should now be possible to clone the test repository from the server with the following command:<\/p>\n<pre>hg clone http:\/\/localhost\/hg\/hgwebdir.cgi\/test<\/pre>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358357Mq.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"09-cloning\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358357Mq.jpg\" alt=\"\" width=\"677\" height=\"333\" \/><\/a><\/p>\n<h3>Pretty URLs<\/h3>\n<p>Personally, I don\u2019t like having to specify &#8220;hgwebdir.cgi&#8221; in the URLs. I\u2019d much prefer something like\u00a0<strong><a href=\"http:\/\/localhost\/hg\/test\">http:\/\/localhost\/hg\/test<\/a><\/strong>\u00a0to access my test repository.<\/p>\n<p>This can be achived by using the URL rewriting extension for IIS which can be\u00a0<a href=\"http:\/\/www.iis.net\/expand\/URLRewrite\">downloaded from Microsoft.<\/a><\/p>\n<p>Once installed, you can access the URL rewriting settings though the &#8220;URL Rewrite&#8221; section of the IIS Manager. Select the &#8220;hg&#8221; subdirectory in the Connections pane and then select &#8220;URL Rewrite&#8221;:<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135835h5S.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"10-iis-urlrewrite\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135835h5S.jpg\" alt=\"\" width=\"703\" height=\"491\" \/><\/a><\/p>\n<p>In the URL rewrite section add a new blank rule. The name of the rule is going to be &#8220;rewrite to hgwebdir&#8221;.<\/p>\n<p>Under the &#8220;Match URL&#8221; section set &#8220;Using&#8221; to &#8220;Wildcards&#8221; and set the &#8220;Pattern&#8221; to &#8220;*&#8221;<\/p>\n<p>Under &#8220;Conditions&#8221; we want to ensure that we do not re-write URLs to any physical files, so add a condition for &#8220;Is Not a File&#8221;:<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358360cK.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"11-iis-isnotafile\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358360cK.jpg\" alt=\"\" width=\"416\" height=\"279\" \/><\/a><\/p>\n<p>In the &#8220;Rewrite URL&#8221; box at the bottom of the screen enter hgwebdir.cgi\/{R:1}<\/p>\n<p>The end result will look like this:<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135836KfS.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"12-iis-rewriterule\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135836KfS.jpg\" alt=\"\" width=\"613\" height=\"705\" \/><\/a><\/p>\n<p>Finally, re-open your hgweb.config and add the following section:<\/p>\n<div>\n<div>\n<pre>[web]\nbaseurl = \/hg<\/pre>\n<\/div>\n<\/div>\n<p>This will ensure that hgwebdir generates urls to \/hg rather than \/hg\/hgwebdir.cgi<\/p>\n<p>Now, visiting <a href=\"http:\/\/localhost\/hg\">http:\/\/localhost\/hg<\/a> will display our repositories page and <a href=\"http:\/\/localhost\/hg\/test\">http:\/\/localhost\/hg\/test<\/a> will show our test repository. Likewise, we can now clone repositories using this url format.<\/p>\n<h3>Pushing Changes<\/h3>\n<p>By default, all repositores served via hgwebdir are read only \u2013 you cannot push changes to them. To change this, we can specify the users that should be able to push to the repositores by adding an &#8220;allow_push&#8221; section to our hgweb.config:<\/p>\n<div>\n<div>\n<pre>[collections]\nc:repositories = c:repositories\n\n[web]\nbaseurl = \/hg\nallow_push = Jeremy<\/pre>\n<\/div>\n<\/div>\n<p>This means that the user account &#8220;Jeremy&#8221; (a local user account on the server) will have push access to the repository.<\/p>\n<p>However, if we try and push changes we\u2019ll get an error:<\/p>\n<div>\n<div>\n<pre>c:projectstest&gt;hg push\npushing to http:\/\/localhost\/hg\/hgwebdir.cgi\/test\nsearching for changes\nssl required<\/pre>\n<\/div>\n<\/div>\n<p>For now, we\u2019ll disable SSL by setting push_ssl to false in our hgweb.config:<\/p>\n<div>\n<div>\n<pre>[collections]\nc:repositories = c:repositories\n\n[web]\nbaseurl = \/hg\nallow_push = Jeremy\npush_ssl = false<\/pre>\n<\/div>\n<\/div>\n<p>Now when we try and push we get a different error:<\/p>\n<div>\n<div>\n<pre>c:projectstest&gt;hg push\npushing to http:\/\/localhost\/hg\/hgwebdir.cgi\/test\nsearching for changes\nabort: authorization failed<\/pre>\n<\/div>\n<\/div>\n<p>This happens because by default IIS is serving up our site without authentication. We need to enable Basic Authentication in the Authentication area of IIS:<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358371FZ.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"13-iis-auth\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/1358371FZ.jpg\" alt=\"\" width=\"401\" height=\"416\" \/><\/a><\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135837gFA.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"14-iis-auth2\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135837gFA.jpg\" alt=\"\" width=\"579\" height=\"325\" \/><\/a><\/p>\n<p>Now you\u2019ll be prompted to enter your username and password:<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135838PwN.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"15-push-username\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135838PwN.jpg\" alt=\"\" width=\"436\" height=\"331\" \/><\/a><\/p>\n<p>After specifying the credetails, the changes will be pushed up. We can view the commit in our web UI:<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135838su8.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"16-view-log\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135838su8.jpg\" alt=\"\" width=\"726\" height=\"528\" \/><\/a><\/p>\n<h3>Enabling SSL<\/h3>\n<p>When you use Basic authentication, your username and password will be sent over the wire in plain text. To make this more secure we can enable SSL. For this example I\u2019m going to use a self-signed certificate, although this will also work with a real SSL certificate purchased from a provider.<\/p>\n<p>First, you\u2019ll need to go into the IIS manager, select &#8220;Server Certificates&#8221; and click &#8220;Create Self-Signed Certificate&#8221;<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135839BkR.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"17-iis-createcert\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135839BkR.jpg\" alt=\"\" width=\"825\" height=\"477\" \/><\/a><\/p>\n<p>Now, add a binding for your Web Site for https on port 443 by right clicking on the site and selecting &#8220;Edit Bindings&#8221;.<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135839s4O.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"18-iis-bindings\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135839s4O.jpg\" alt=\"\" width=\"350\" height=\"381\" \/><\/a><\/p>\n<p>Add a new binding for https on port 443:<\/p>\n<p><a href=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135840C8J.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"19-iis-bindings2\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/07\/135840C8J.jpg\" alt=\"\" width=\"653\" height=\"470\" \/><\/a><\/p>\n<p>Once this is done, you should now be able to access the hgwebdir site by using https (<a href=\"https:\/\/localhost\/hg\">https:\/\/localhost\/hg<\/a>). You\u2019ll probably get an invalid certificate warning in your browser.<\/p>\n<p>Now you can re-clone the repository using the secure url (also be sure to remove the &#8220;push_ssl = false&#8221; line from hgweb.config)<\/p>\n<h3>All done!<\/h3>\n<p>At this point, you should have successfully set up everything you need to use Mercurial in IIS7.<\/p>","protected":false},"excerpt":{"rendered":"<p>This guide walks you through setting up a Mercurial server under IIS7 on Windows Server 2008 R2. Not&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[328,83,327,14,17],"class_list":["post-910","post","type-post","status-publish","format-standard","hentry","category-other","tag-hg","tag-iis","tag-mercurial","tag-server","tag-windows"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=910"}],"version-history":[{"count":2,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/910\/revisions"}],"predecessor-version":[{"id":931,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/910\/revisions\/931"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=910"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}