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