2010-11-18

Mercurial server on Windows with Apache

In this tutorial I will show you how to set up a Mercurial 1.7.1 server on Windows using the Apache server, version 2.2.17. I successfully verified this configuration on Windows 7 and Windows XP.

It should be fairly similar for Unix based system, but I haven’t tried that. You can obviously use a different server too.

Thanks goes to my friend Jeroen who helped me out with this.


Installing Mercurial


Obviously, you need Mercurial. It’s up to you how you want to install it, but make sure that the hg command is available from command line. Use the MSI installation and it will do it for you.

I will assume you installed it to C:\Program Files\Mercurial.

Verify that hg is accessible from command line


Installing Apache


The second step is to obtain and install the Apache server. You may want to install it as a service. There is a nice and simple ApacheMonitor application that will put itself in your system tray and you can control your Apache from there.

I will assume you installed it to C:\Program Files\Apache Software Foundation\Apache2.2.


Installing Python


For the Mercurial server to work, you need to have Python installed. The Python version must correspond to the Mercurial version. For Mercurial 1.7.1 you should get Python 2.6.

Please not that you do NOT need to set a system variable called PYTHONPATH as some tutorials suggest.

I will assume you installed your Python to C:\Python26.


Downloading Mercurial source


Download Mercurial source for your Mercurial version and unpack it. If you are lazy, you don’t have to do it - all the necessary files are available on my website (see links below).


Configuring Mercurial


  1. Go to your Mercurial installation directory.
  2. Unpack the library.zip file contents into a folder called lib. You will have to create it yoruself. In this folder the contents of the library.zip file should reside direcly, i.e. the path to for instance zipfile.pyc should be C:\Program Files\Mercurial\lib\zipfile.pyc.
  3. Move the C:\Program Files\Mercurial\templates folder into the new lib folder.


Creating a projects folder


You need to create a project where Mercurial will store your projects. I will assume it’s C:\Projects\hg.


Configuring Apache


In the Apache htdocs folder put this file - hgweb.cgi. Get it from the Mercurial source. In another folder (I will assume it’s the Mercurial installation folder) put hgweb.config - just create it.

Please note those two files are also available for download from my website (see links below).

hgweb.cgi


#!c:/Python26/python.exe -u
#
# An example hgweb CGI script, edit as necessary
# See also http://mercurial.selenic.com/wiki/PublishingRepositories

# Path to repo or hgweb config to serve (see 'hg help hgweb')
config = "C:/Program Files/Mercurial/hgweb.config"

# Uncomment and adjust if Mercurial is not installed system-wide:
import sys; sys.path.insert(0, "C:/Program Files/Mercurial/lib")

# Uncomment to send python tracebacks to the browser if an error occurs:
import cgitb; cgitb.enable()

from mercurial import demandimport; demandimport.enable()
from mercurial.hgweb import hgweb, wsgicgi
application = hgweb(config)
wsgicgi.launch(application)

So if you got this file from the source, you need to make it look more or less like this one.

hgweb.config


[collections]
C:\Projects\hg = C:\Projects\hg
[web]
allow_push = *
style = monoblue
contact = you@localhost.com
push_ssl = false

C:\Projects\hg is, again, where Mercurial will contain your projects. It allows anyone to push. It uses the monoblue style. Provide your email in the contact field. The last line makes SSL not compulsory to be able to push.

httpd.conf


I didn't have to do this, but user Good reported he did and he found it on the editrocket.com site.

Search the httpd.conf file for the line
Options Indexes FollowSymLinks
Add ExecCGI to this line. The line should now look similar to the following (NOTE: there may be more options listed):
Options Indexes FollowSymLinks ExecCGI
Next, search for the following:
#AddHandler cgi-script .cgi
Uncomment this line by removing the # in front of the line, and add a .py to the end of the line. The new line should look like this:
AddHandler cgi-script .cgi .py

Thanks Good!

Creating a sample project


Now, for each project you will have to manually create a repository in the C:\Projects\hg folder.

Go to console, go to this folder and type hg init projectName. This will create the repository.


Verifying


Launch the server and go to http://localhost/hgweb.cgi. You should now see your project listed there under projectName.

You can push and pull to http://localhost/hgweb.cgi/projectName/.

Sample hgweb.cgi (I changed the extension to .txt so that the server wouldn’t treat it as a script)
Sample hgweb.config

16 comments:

  1. i followed all the steps properly n finally wen started server n typed http://localhost/hgweb.cgi in browser it shows only hgweb.cgi file in browser. how can i see repository contents? please help me

    ReplyDelete
  2. Hi raj, thanks for visiting the blog. Do you have Python installed properly and in the path?

    ReplyDelete
  3. I am facing same problem as Raj, I have installed python properly and it is also in C drive
    Can you please help me,

    thanks

    ReplyDelete
  4. @Good
    Is python available from command line? In other words, is it added to path?

    ReplyDelete
  5. I thought it would set the path, Sorry to bother, and asking basic questions,
    I am a beginner in python and I would like to use Mercurial for my project as version control

    I will retry all the process again, please let me know If I need to be aware of any thing...
    How can I know python has been installed properly?Is there any way to check,

    I really appreciate your help,
    THanks

    ReplyDelete
  6. Can I download Mercurial 1.9 version and try above steps?

    ReplyDelete
  7. Hi again.

    If you're a Python beginner, you're ahead of me already. ;) I'm not sure whether it would set the path. How to check it: Open a console window and browse to a folder where Python is NOT installed (C:\Users for instance), type python. That should not say command not found, but give you python.

    As for 1.9, I would assume it's the same, but I have no idea which version of Python they use.

    ReplyDelete
  8. Hi Michal,

    I have tried installing same version as stated above, but no luck, still I am can see the contents of the hgweb.cgi

    I need to set the path for python inorder to see the shell of python

    I have installed 32 bit version softwqare on 64bit laptop

    I see no road ahead, can you please help me
    thanks

    ReplyDelete
  9. I think python is not being invoked in the cgi script....... do you think installing 32bit version makes difference on 64bit?

    ReplyDelete
  10. I must ask again - is Python available in the path?

    ReplyDelete
  11. When we install python, it will set path, and I think, we dont need to set path as you said earlier.

    I got it working, modifications are to be done in apache server.

    I did the modifications as follows for the httpd.conf file located in the apache folder

    The next step is to use EditRocket to open the httpd.conf apache configuration file located in the apache install directory in the conf directory. Search the httpd.conf file for the line

    Options Indexes FollowSymLinks

    Add ExecCGI to this line. The line should now look similar to the following (NOTE: there may be more options listed):

    Options Indexes FollowSymLinks ExecCGI

    Next, search for the following:

    #AddHandler cgi-script .cgi

    Uncomment this line by removing the # in front of the line, and add a .py to the end of the line. The new line should look like this:

    AddHandler cgi-script .cgi .py

    I found on this website
    http://www.editrocket.com/articles/python_apache_windows.html


    If you can incorporate this changes, I would say it would be complete.... and Thanks to you.... It is the simplest installation method I have ever seen in web,,,simple and easy...... Will explore more and keep you updated....thanks for your help and posting this great tutorial,,,,

    ReplyDelete
  12. When I try to commit it says
    "abort: no username supplied (see "hg help config")"

    Any pointers please???

    Thanks

    ReplyDelete
  13. Good, thanks for your info. I will update the article.

    Try this perhaps: http://stackoverflow.com/questions/2329023/mercurial-error-abort-no-username-supplied

    ReplyDelete
  14. Thanks for your post - very helpful. I struggled with an error in line 10 of osutil.pyc

    ImportError: DLL load failed: The specified module could not be found.

    The bug report at http://mercurial.selenic.com/bts/issue2582 has a suggestion to use pmezard's distribution of hg. After installing that, and setting my sys.path.insert to point to the new "C:/Python26/Lib/site-packages/mercurial" location, it worked for me.

    ReplyDelete
  15. I appreciate your post; it was quite useful. I had trouble with an osutil error in line 10.Pyc I'm really delighted with your fantastic blog post; it offers genuine facts and is quite transparent about the issues at hand. Your webpage is really beneficial. I appreciate you sharing.
    Distrito Nueva Jersey Protección Orden

    ReplyDelete
  16. Columbia Garage Door Repair is your trusted local expert for all garage door needs. Specializing in repairs, installations, and maintenance, we ensure safe, reliable, and long-lasting solutions. Our team of skilled technicians is trained to handle all garage door types, from traditional to modern automated systems. With prompt service and competitive rates, we’re dedicated to restoring the functionality and security of your garage door.

    We use high-quality parts, ensuring durability and smooth operation for years to come. Available for emergency repairs, Columbia Garage Door Repair is committed to excellent customer satisfaction and making your home or business safer and more convenient.

    ReplyDelete