Installing a Mercurial server on Debian

This guide describes how to setup a Mercurial server on Debian. It is designed to be a good a starting point to make the first steps easier. The provided scripts are tested on Debian Squeeze and has to be executed with root privileges, so if the current user is not root, type this before doing anything:

su root

First, install the required packages:

apt-get install mercurial apache2 libapache2-mod-wsgi

Create a directory for the future Mercurial repositories:

mkdir /var/repo
mkdir /var/repo/hg

Create a script file the automates the creation of a repository. It initializes a repository and grants the appropriate rights.

hg init $1

chmod 770 ./$1 -R
chown www-data.root ./$1 -R

Script to achieve this:

cd /var/repo/hg
echo "hg init \$1" >
echo "chmod 770 ./\$1 -R" >>
echo "chown www-data.root ./\$1 -R" >>
chmod +x

The Mercurial server is hosted by Apache, create a directory for its virtual host:

mkdir /var/www/vhosts
mkdir /var/www/vhosts/hg
mkdir /var/www/vhosts/hg/cgi-bin

We need a wsgi script and configuration file in the cgi-bin directory.

This sample configuration makes the server to show all repositories located in the previously created directory.


style = coal
allow_push = *
push_ssl = false
/ = /var/repo/hg/**

The stub of the wsgi script should be downloaded from the appropriate repository, but I just provide it here (I hope this does not result in licensing issues).  This script refers to the previously created configuration file. Unfortunately using an absolute uri is inevitable.


config = "/var/www/vhosts/hg/cgi-bin/hgweb.config"
import cgitb; cgitb.enable();
from mercurial import demandimport
from mercurial.hgweb import hgweb
application = hgweb(config)

Script that creates these files:

cd /var/www/vhosts/hg/cgi-bin
echo "[web]" > hgweb.config
echo "style = coal" >> hgweb.config
echo "allow_push = *" >> hgweb.config
echo "push_ssl = false" >> hgweb.config
echo "" >> hgweb.config
echo "[paths]" >> hgweb.config
echo "/ = /var/repo/hg/**" >> hgweb.config
echo "config = \"/var/www/vhosts/hg/cgi-bin/hgweb.config\"" > hgwebdir.wsgi
echo "" >> hgwebdir.wsgi
echo "import cgitb; cgitb.enable();" >> hgwebdir.wsgi
echo "" >> hgwebdir.wsgi
echo "from mercurial import demandimport; demandimport.enable()" >> hgwebdir.wsgi
echo "from mercurial.hgweb import hgweb" >> hgwebdir.wsgi
echo "" >> hgwebdir.wsgi
echo "application = hgweb(config)" >> hgwebdir.wsgi

The virtual directory that is going to be hosted by Apache has to be initialized, so create the corresponding site configuration file: /etc/apache2/sites-available/hg

<VirtualHost *:80>
 ErrorLog /var/log/apache2/hg-error.log
 CustomLog /var/log/apache2/hg-access.log common
 WSGIScriptAliasMatch ^(.*)$ /var/www/vhosts/hg/cgi-bin/hgwebdir.wsgi\$1

Script to achieve this:

cd /etc/apache2/sites-available
echo "<VirtualHost *:80>" > hg
echo " ErrorLog /var/log/apache2/hg-error.log" >> hg
echo " CustomLog /var/log/apache2/hg-access.log common" >> hg
echo " WSGIScriptAliasMatch ^(.*)$ /var/www/vhosts/hg/cgi-bin/hgwebdir.wsgi\$1" >> hg
echo "" >> hg
echo "</VirtualHost>" >> hg

Almost there! The site and Apache has to be enabled and reloaded, respectively.

a2dissite default
a2ensite hg
/etc/init.d/apache2 reload

We are complete. Now, you can use this to create a new repository:

/var/repo/hg/ repository_name

To access the repositories, simply browse this url: http://ipaddress_of_server/

Keep in mind that the provided scripts only serve as a starting point. For example, it is highly recommended to complete the site configuration file with the ServerName directive and provide authentication and authorization mechanism.

The complete script can be downloaded here.