Archive for November, 2009

Multihoming a PHP application with Apache2::PerlSections

Sunday, November 15th, 2009

Squadlist is a simple Mysql / PHP application which is used by a number of UK rowing clubs. Each instance of the application is hosted on a separate Apache virtual host and uses a separate Mysql database.

Unmodified, each installation requires a separate copy of the webapp and a separate Apache virtual host configuration file. Maintaining separate copies in this way would be complicated. Generally, I don’t do complicated, so needed a way to reduce the duplication.

Extracting the database configuration into the Apache virtual host

The application contains a service class which holds the database connection creditials for the given instance:

The first problem is that this file needs to be localised for each instance, requiring a separate copy of the application to be installed and maintained for each new instance.

We’re going to fix that by defining each instances unique database connection settings in the apache environment for each given vhost

Then in PHP, we can assess the apache environment to retrieve the database credentials specific to this instance.

Because our PHP app is completely stateless, this is a safe approach and we’re now down to one copy of the application files.

Automatically generate the apache vhosts

Next problem is maintaining the apache vhosts.

Adding a new install requires a new vhost file to be hand crafted and installed. Upgrading the system requires the DocumentRoot to be updated in each file.

Enter Apache2::PerlSections which lets you execute Perl code in your apache config files. By placing the credentials for each instance into a mysql database, we can use a Perl DBI script to automatically generate the required vhosts on apache startup.

‘instances’ is the Mysql table containing our credentials. The connection details for this instance are in the root owned apache conf file so this does not expose the access details for the instance databases.

Note how $PerlConfig lets you print conf directly into Apache. To reiterate to those who haven’t seen this approach before – that Perl code is in the Apache conf files and is actually been executed inside Apache on startup to generate the vhosts.

Adding a new instance of the application now consists of adding a row to ‘instances’, setting up the DNS and restarting apache.

Sceptics may well ask what drugs was I on I wrote this? I’m happy to answer; it was Oseltamivir

Wellynews tips – custom RSS feeds

Sunday, November 15th, 2009

A number of people take the main wellynews RSS feed at http://wellington.gen.nz/rss. However, this is just one of many feeds which the system automatically creates.

Tag feeds

All tags have there own RSS feed. You just need to substitute the tagname into the url:

ie. http://wellington.gen.nz/transport/rss

This feed contains news items tagged as transport, or any of the transport sub tags (such as airport and parking). It also includes all news items published by organisations tagged with transport (ie. Snapper card).

A full list of available tags is shown here.

Combinations

If you want an RSS feed about, say, zombie outbreaks but only on the waterfront then you can use a combination feed.

ie. http://wellington.gen.nz/zombies+waterfront/rss

Note two tag names separated by a plus sign in the url. Interestingly enough, zombies and waterfront is really a valid combination and will return content.

Publisher combinations

If you’re looking for what a given publisher has said about a given tag, you can extract that using a publisher combination.

For example, mentions of Newtown made by Wellington City Council.

ie. http://wellington.gen.nz/wellington-city-council+newtown/rss

A list of available publishers is shown here.

This url scheme is inspired by the excellent implementation on the Guardian newspaper’s site.