mod_myvhost

Current version: 0.13

Installation instructions:

Prerequisities:

This packages are required for successfull compilation:

apache13-dev
mysql-dev

Download source code:

The latest version of mod_myvhost is here

Decompress the source:

tar -xzvf mod_myvhost-x.xx.tar.gz

Building mod_myvhost:

just type make
If you do not want php support than remove -DWITH_PHP from CFLAGS in Makefile. If you want to test module's internal caching than add -DWITH_CACHE to CFLAGS in Makefile.

Install mod_myvhost:

make install (as root)

MySQL:

Now it is time to create database that will be used to store vhost configurations. You can use vhosts.sql file as example.
CREATE DATABASE `hosting`;
USE `hosting`;
	    
CREATE TABLE `vhosts` (
    `vhost` varchar(255) NOT NULL default '',
    `enabled` enum('yes','no') NOT NULL default 'no',
    `rootdir` varchar(255) NOT NULL default '',
    `admin` varchar(255) default '',
    `extra_php_config` text,
    UNIQUE KEY `vhostname` (`vhost`),
    KEY `enabled` (`enabled`)
) TYPE=MyISAM COMMENT='vhosts';
					
GRANT SELECT ON hosting.vhosts TO 'nonpriv'@'localhost' IDENTIFIED BY 'M3Ga PaSsVVd';
					
INSERT INTO `vhosts` VALUES ('w_3.vhost.net', 'yes', '/var/www/vhosts/01', 'w_3@vhost.net', 'enable_dl=0');
INSERT INTO `vhosts` VALUES ('www.vhost.net', 'yes', '/var/www/vhosts/02', 'www@vhost.net', '');
	    
Create database and table:
mysql -u root -p < vhosts.sql

Apache:

Edit apache config, you may use httpd.conf.add as example, make shure that mod_myvhost.so is loaded after libphp4.so.
<IfModule mod_myvhost.c>
    MyVhostOn		on
    MyVhostDefaultHost	"www.notvhost.net"
    MyVhostDefaultRoot	"/var/www/html"
    MyVhostDbHost	"localhost"
    MyVhostDbSocket	"/tmp/mysql.sock"
    MyVhostDbUser	"nonpriv"
    MyVhostDbPass	"M3Ga PaSsVVd"
    MyVhostDbName	"hosting"
    MyVhostQuery	"SELECT rootdir,admin,extra_php_config FROM vhosts WHERE vhost='%s' AND enabled='yes'"
    <Directory "/var/www/vhosts">
        Options Indexes
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</IfModule>
	    

The most important parameter that you should take into account is MyVhostQuery, it must contain one and only one %s that is substituted by vhostname from http request. If hostname is absent or no vhost found, than request served by default vhost, that is defined by MyVhostDefaultHost and MyVhostDefaultRoot.

The module expects MySQL to return up to three fields: the first one is treated as absolute path of vhost's server root (magically becomes open_basedir), than goes vhost's admin (email) and the third (if exists) is pairs of php variables and their values delimeted by equality sign, pairs in turn are separated by semicolon, for boolean variables you should use 1 and 0.

Make it all working:

Just restart apache:
apachectl stop; apachectl start
So, it should work.
This module is tested to compile and work on FreeBSD and Linux with MySQL 4.0.xx, 4.1.xx and 5.0.xx.

TODO

MySQL flushes query cache whenever that table is changed. If updates are often, peformane decrease significantly, to achive good perfomance, module should use internal caching of MySQL responses (positive and negative).

At present time module already has ability of internal caching, so, next step will be support for global caching.