Using update-alternatives with Subversion and Apache

One of the complications I have had with Apache and Subversion is working out the best way of switching between branches and making them visible in Apache. At first, I was checking out a branch, deleting and replacing it in the same location so that Apache could pick up the branch I wanted. This was really slow and could be problematic if some of a directory was not committed for whatever reason.

My original solution to this was to maintain virtual hosts for every branch, using a separate port for each one. However, this meant that I then had to keep an index of which branch was on which port, and updating all of this each time I created/checked out a branch was a big overhead.

I have now found a neat solution using the Debian/Ubuntu program update-alternatives. This program is very easy to configure, and scales well for what I am trying to do. It also allows me to put all branches for a website project on a single port. These instructions assume you already have a working Subversion repository and have a basic understanding of how to use Subversion and Apache in Debian/Ubuntu:

1. Create a Subversion workspace for your projects, if you do not already have one. NOT “/var/www”, we will be using that for something else. Use something like “/home/myself/workspace”.

2. Check-out your trunk/branches in this workspace directory, try to keep each project contained in a sub-directory for clarity:

$ mkdir ~/workspace/myproject; cd ~/workspace/myproject
$ svn co /path/to/repo/trunk
$ svn co /path/to/repo/branches/testing
$ svn co /path/to/repo/branches/1.x-release

2. You should now have a project directory containing the project trunk, and some of your branches:

| -workspace
|- myproject
| – trunk
| – testing
| – 1.x-release

3. Install each project branch into update alternatives, using “/var/www” as the target for the alias. Note the numbers at the end, they represent the priority of each entry (how far up the list they are); incrementing them by 10 leaves you space to include entries further up the list more easily:

$ sudo update-alternatives --install /var/www/myproject myproject /home/myself/workspace/myproject/trunk 10
$ sudo update-alternatives --install /var/www/myproject myproject /home/myself/workspace/myproject/testing 20
$ sudo update-alternatives --install /var/www/myproject myproject /home/myself/workspace/myproject/1.x-release 30
$ sudo update-alternatives --config myproject

There are 2 alternatives which provide `myproject’.

Selection Alternative
* 1 /home/myself/workspace/myproject/trunk
+ 2 /home/myself/workspace/myproject/testing
+ 3 /home/myself/workspace/myproject/1.x-release

Press enter to keep the default[*], or type selection number:


4. Hopefully you should be starting to see the advantages of this approach. At any time, you can type “sudo update-alternatives –config myproject” and you will be able to select which project branch will be provided to Apache.

5. Configure Apache to look at /var/www/myproject. That’s it.

When you want to add a new branch, you need to check it out as described above, and use the “update-alternatives –install” command to include it in the list.


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: