Mercurial Server using hgweb.cgi on Ubuntu
In a previous post, we set up a virtual machine template for an ubuntu server. Now that we have set up a clone of this machine, it is time to set up a Mercurial repository server
Abstract
Mercurial provides an easy to use repository server via a python cgi script. Mercurials protocol facilitates fast transfers over http, making it superior over an ssh solution based (such as git) when considering minimal protocol overhead vs. ease of use. As my webserver of choice, we will use lighttpd. This guide will follow the instructions published hereĀ http://mercurial.selenic.com/wiki/PublishingRepositories#multiple
Installing Python
This one is simple:
ubuntu@localhost: sudo apt-get install python
Installing Lighttpd
To install lighttpd, run:
ubuntu@localhost: sudo apt-get install lighttpd
Next, we need to create a specific configuration for our mercurial cgi script. We need to redirect all incoming requests to the cgi script, and then we apply some url rewrite magic to remove the ugly hgweb.cgi from our URLs. The hgweb.cgi script will be served from /var/www/hgweb.cgi. If you use a different location, make sure to chown it to www-data and chmod+x it (all described in the mercurial wiki). I created my config like this:
ubuntu@localhost:~$ sudo vi /etc/lighttpd/hg.conf
ubuntu@localhost:~$ cat /etc/lighttpd/hg.conf
url.rewrite-once = (
"^([/?].*)?$" => "/hgweb.cgi$1",
"^/([/?].*)?$" => "/hgweb.cgi$1"
)
$HTTP["url"] =~ "^/hgweb.cgi([/?].*)?$" {
server.document-root = "/var/www/"
cgi.assign = ( ".cgi" => "/usr/bin/python" )
}
Next is the lighttpd config, that will need to reference our hg.conf and enable mod_cgi:
ubuntu@localhost:~$ cat /etc/lighttpd/lighttpd.conf
include "hg.conf"
server.modules = (
"mod_access",
"mod_alias",
"mod_compress",
"mod_redirect",
"mod_rewrite",
"mod_cgi"
)
Further configuration Tricks
You should force hgweb.cgi to serve UTF-8 content. Fortunately enough, this is as simple as adding (or uncommenting) the following lines to hgweb.cgi:
import os os.environ["HGENCODING"] = "UTF-8"
You will also need a hgweb.conf right next to hgweb.cgi and reference it from there (again, described in the mercurial wiki). For reference, my configuration includes all repos sourced under /var/hg/repos (and subdirectories) and allows anonymous push (I’m authenticated via VPN policy):
ubuntu@localhost:~$ cat /var/www/hgweb.config [paths] / = /var/hg/repos/** [web] baseurl = / allow_push = * push_ssl = false
Final Words
That’s all there is to it. To make the server available via DNS, you need to make sure the servers hostname is registered with your local DNS. In my case, I simply added a static record to it.
