Mercurial Server on Ubuntu 12.04 LTS with Web Access

First we shall verify the version of the Ubuntu server that is running on our machine!

lsb_release -a

Install the Apache if you haven’t it already on your server!

sudo apt-get install apache2

Test the apache directly from the shell by doing a wget to the localhost. You will see the 200 OK response code.

Now install the Mercurial using the following command:

sudo apt-get install mercurial

Check the version of installed mercurial server:

hg --version

Let’s configure the master hgrc configuration file in order to remove the SSL restriction, allow to push the repository through web and trust the apache user (www-data).

sudo nano /etc/mercurial/hgrc

Add these lines to the hgrc file:

allow_push = *
push_ssl = false
allow_archive = gz, zip, bz2
users = www-data

Now configure the directory where Mercurial will keep all its repositories and serve them through Apache!

cd /
sudo mkdir mercurial
sudo mkdir mercurial/repositories
sudo chown -R www-data:www-data /mercurial

Let’s configure the hgweb.config!

cd /mercurial
sudo nano hgweb.config

Enter the following lines:

style = gitweb
/mercurial/repositories = /mercurial/repositories

Let’s configure the hgweb.cgi script in order to serve the repositories via Apache:

sudo cp /usr/share/doc/mercurial/examples/hgweb.cgi /mercurial
cd /mercurial
sudo chmod a+x hgweb.cgi

Open hgweb.cgi with nano:

sudo nano hgweb.cgi

Find the line that reads config=”…..” and replace with the following line:

Now we shall configure the Apache so it runs the hgweb.cgi when the /mercurial virtual directory is requested.

cd /etc/apache2
sudo mkdir mercurial
cd mercurial
sudo nano mercurial.conf

Type the following in the mercurial.conf file:

Move to the apache sites directory and edit the default file:

cd /etc/apache2/sites-available
sudo nano default

Enter the command in order to link the mercurial.conf file to Apache:

Include /etc/apache2/mercurial/mercurial.conf

Restart the apache server:

sudo apache2ctl restart

Now create a first repository as test, inside the /mercurial/repositories:

cd /mercurial/repositories
sudo mkdir first_repo
cd first_repo
sudo hg init

Change the owner of the repository to the Apache user:

sudo chown -R www-data:www-data /mercurial

To configure the security, we have to tell the Apache that allow only those users who are registered in the password file.

cd /mercurial
sudo htpasswd -mc hgusers arbab 
sudo chown www-data:www-data hgusers

Note: repeat command for more users without adding c.

Time to Test, Launch your browser pointing to http://servername/mercurial:

In order to add the “Description” and “Contact” detail, do the following steps:

cd /mercurial/repositories/first_repo/
cd .hg
sudo nano hgrc

Add these lines to hgrc:

contact = Arbab Nazar
description = First Mercurial Repository!

Refresh your browser, now you will see the contact and the descriptio 🙂

To solve the push permission denied error:

cd /mercurial/repositories
cd first_repo
sudo chmod -R 777 .hg

Again do it!!!

sudo chown -R www-data:www-data /mercurial

Congrats! now we have a working Mercurial server on Ubuntu 12.04 :-)

Hope this will help you!

Please Remember me in your prayers!

10 responses to “Mercurial Server on Ubuntu 12.04 LTS with Web Access

  1. guest February 1, 2013 at 11:23 pm

    Worked perfectly at first try. Excellent howto, thank you!

    There is just a tiny typo at the step after this line:

    “Now create a first repository as test, inside the /mercurial/repositories:”

    There, in the text, there is the “hg init” command, but it should be “sudo hg init”.

    Thank you again for this great howto!

  2. Chijioke John February 19, 2013 at 10:40 pm

    Thanks a lot. Worked perfectly on first try as well for me. And I sure will rememer you in my prayers. Thank you so much. You made my day! Also how does web access compare with ssh access for mercurial. Do you have a link to a tutorial using ssh access ?

  3. Pingback: Muhammad Nurrahman » Catetan

  4. Pingback: Sahil Alaracu Online Portfolio » Installing Latest Mercurial 2.2 in Centos 6.4 VPS Distributed SVN Server setup

  5. Anonymous March 27, 2014 at 5:13 pm

    Hi! I could able to setup the hg server with this settings earlier, but presently with ubuntu 13.10 and apache 2.4.6 and hg version 2.6.3, can you please help to configure?

    Thanks in advance!

  6. einstein September 7, 2014 at 8:52 pm

    thank you very much. Worked like charm. Although my server didnt have mod_cgi enabled. To fix that run ‘sudo a2enmod cgi’.

  7. Abdullah AbuMd October 17, 2018 at 5:03 pm

    Extremely useful, Illustrious style! Still relevant after 4 years!
    I just found this piece of information to be outdated:
    sudo nano default –> sudo nano 000-default.conf
    I am able to setup my own mercurial server in much less time.Thank you very much.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: