Here's some stuff you may find useful if you're setting up or using a linux machine or server. Someone needs to stick these things on the internet, right? :)
Apache - Storing Virtual Hosts in MySQL
There are several ways to do it, this one uses mod_perl and needs to have different virtual host titles (ie if you dont specify domains in your vhost tags, ie <VirtualHost *>, then this is not for you). Ensure your perl install has the DBI module, you have MySQL installed, and your Apache install has mod_perl. Your httpd.conf file will have a line like
LoadModule perl_module /usr/lib/apache/1.3/mod_perl.so
Then you'll need a mysql database with a table to hold all the data for the virtual hosts. Columns for the VirtualHost argument, DocumentRoot, ServerName etc. The code below uses a table 'apache_virtual_hosts' and has four columns, but seeing as you're reading this you must know something, but I threw in a default for those of you who are server admins but not too hot on perl... Load up the table, and add something similar to the following to your httpd.conf:
<Perl>
use DBI;
my $dbh = DBI->connect("dbi:mysql:database","database","password",{RaiseError => 1, AutoCommit => 0}) || die "Error connecting: $DBI::errstr";
my $sth = $dbh->prepare("select virtualhost, documentroot, servername, serveradmin from apache_virtual_hosts");
$sth->execute;
while (my ($tvirtualhost, $tdocumentroot, $tservername, $tserveradmin) = $sth->fetchrow_array) {
$VirtualHost{$tvirtualhost} = {
DocumentRoot => $tdocumentroot,
ServerAdmin => ($tserveradmin) ? $tserveradmin : "webmaster\@myserver.com",
ServerName => $tservername
}
}
$sth->finish;
$dbh->disconnect;
</Perl>
Then restart apache. It'll pull the data from the table and set up the virtual hosts.
Storing and retrieving ServerAliases
Add another table in your database, ie serveralias with two columns, virtualhost and serveralias, both as keys. Then you can run the query above to retrieve all of the stuff that's in your virtualhost table, then another query to find all of the serveraliases from the serveralias table and put them into an array (ie @tserveralias). Then you can put a reference to the array in the ServerAlias value in the VirtualHost hash:
$VirtualHost{$tvirtualhost} = {
DocumentRoot => $tdocumentroot,
ServerAdmin => ($tserveradmin) ? $tserveradmin : "webmaster\@myserver.com",
ServerName => $tservername,
ServerAlias => \@tserveralias
};
Then to test if it exists, you can just retrieve the aliases by step through the ServerAlias array, ie here we're testing the www.myserver.com virtual host for the server alias thing.myserver.com:
my $found = 0;
foreach (@{${$VirtualHost{"www.myserver.com"}}{"ServerAlias"}}) {
$found = 1 if ($_ eq "thing.myserver.com");
}