Archive for the ‘Perl’ Category

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

WCN hosted content

Monday, June 8th, 2009

Wellington Community Net (WCN) is a free web hosting service for Wellington community groups. WCN has been funded by the City Council for a number of years now, but that funding is now under review.

WCN hosts 600 odd community groups, meaning that alot of the content that Search Wellington indexes is probably hosted on WCN. I wanted to find out how much.

All WCN sites are physically hosted on the same machine. Taking a dump of all valid websites from Search Wellington, and filtering out the who’s DNS resolves to the WCN server, should identify WCN hosted sites.

The Search Wellington API can then be used to tag each of these sites with a hidden ‘wcnhosted’ tag. The following Perl script checks the DNS for the uses an API call to apply a tag to wcn hosted sites.

All of the news items and feeds published by these sites are automatically pulled into this tag, giving:

http://wellington.gen.nz/wcnhosted

Which shows that approximately 340 of 9000 odd indexed, news items originated from WCN.

This is a low measurement, as Search Wellington chooses not to list all groups on WCN and only includes news items with a permanent url, but I think shows that the loss of WCN would leave quite a significant hole in the local webspace.