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" > create.sh
echo "chmod 770 ./\$1 -R" >> create.sh
echo "chown www-data.root ./\$1 -R" >> create.sh
chmod +x create.sh

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.

hgweb.config

[web]
style = coal
allow_push = *
push_ssl = false
[paths]
/ = /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.

hgwebdir.wsgi

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
</VirtualHost>

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/create.sh 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.

Advertisements

Installing a Subversion server on Debian

This guide describes how to setup a SVN 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 apache2 subversion libapache2-svn

Create a directory for the future Subversion repositories:

mkdir /var/repo
mkdir /var/repo/svn

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

svnadmin create $1
chmod 770 ./$1 -R

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

Script to achieve this:

cd /var/repo/svn
echo "svnadmin create $1" > create.sh
echo "chmod 770 ./\$1 -R" >> create.sh
echo "chown www-data.root ./\$1 -R" >> create.sh
chmod +x create.sh

The Subversion server is hosted by Apache, so create the corresponding site configuration file: /etc/apache2/sites-available/svn

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

<VirtualHost *:80>
 ErrorLog /var/log/apache2/svn-error.log
 CustomLog /var/log/apache2/svn-access.log custom

 <Location />

  DAV svn
  SVNParentPath /var/repo/svn/
  SVNListParentPath on
  SVNAutoVersioning on
 
 </Location>
</VirtualHost>

Script that creates this configuration file: cd /etc/apache2/sites-available

echo "<VirtualHost *:80>" > svn
echo " ErrorLog /var/log/apache2/svn-error.log" >> svn
echo " CustomLog /var/log/apache2/svn-access.log custom" >> svn
echo " >> svn 
echo " <Location />" >> svn
echo "  DAV svn" >> svn
echo "  SVNParentPath /var/repo/svn/" >> svn
echo "  SVNListParentPath on" >> svn
echo "  SVNAutoVersioning on" >> svn
echo " </Location>" >> svn
echo "</VirtualHost>" >> svn

The configuration is complete, now the site and Apache has to be enabled and reloaded, respectively.

a2dissite default
a2ensite svn

/etc/init.d/apache2 reload

It’s done. Now, we can use this to create a new repository:

/var/repo/svn/create.sh repository_name

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

Keep in mind that the provided script only serves 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 from here.