Shared Apache VirtualHost module

This module allows you to store your virtual hosts in a database and extract them on the fly.

While this may not be the most speed efficient way of using mass virtual hosting, it has it's advantages.

On a beefy dedicated MySQL server, the difference is speed was negligable - from 312/hps to 284/hps - a small loss. However, this was tested using 3 load balanced servers using a seperate dedicated MySQL server backend, connected via gigbit network.

The main advantage is any changes to virtual hosts are immediate, and do not require a restart of apache at all. Memory consuption is dramatically reduced (6000 vhosts used around 250mb RAM on my test system - it is now using 4mb).

The code has been running for a long time (about 2 years now) at a number of large sites with no problems at all, so i would consider it stable. But still, read the code, and check it yourself first.

The configuration directives are:

ShapVHOn [on|off]            - Turns on ShapVH handling for this host.
ShapVhHost                   - MySQL host to connect too.
ShapVhUser                   - MySQL Username
ShapVhPass                   - MySQL Password
ShapVhDbName                 - MySQL database name to use
ShapVhDefaultRoot [path]     - Documentroot to use when Vhost is not found.
ShapVhDefaultAdmin [address] - Email Address for server administrator when a vhost is not found.
ShapVhQuery [query]          - MySQL query to use. (see bellow)
ShapVhVISP [on|off]          - Turns on VISP support (see bellow)

Query String

The query string should return 4 columns, in the following order:

  • Full path to ducument root.
  • UserID to execute CGI scripts under (for suexec)
  • Server admin email address
  • GroupID to execute CGI scripts under (for suexec)

If the query string is for a normal vhost, it should contain 1 '%s', which will be replaced by the hostname. If the querystring is for a VISP vhost, then it should contain 2 x '%s' - the first for the username, the second for the VISP/Domain (see bellow for more info).

for example, for a standard vhost:

SELECT DocRoot, UID, EmailAddress, GID FROM VirtualHosts WHERE Hostname = '%s'

Note: As the hostname can't contain "'" or "\", it does not need to be escpaed, however, the module does do sanity checking to ensure there are no charecters that shouln't be in the supplied hostname.

VISP Support

Disclaimer: The following exmaples are not how I'd ever set somethign up, but a lot of people seem to - so i'll use en easy example rather than methods i use personally.

VISP (Virtual ISP support) allows you to give every user in your system a home directory, much like mod_userdir, however, there is support for systems which run more than one (virtual) ISP on one system, using seperate paths to home directories.

For example, lets say we run 3 vISP's - slowhosts.com, smallbrother.com, and blahmeep.co.uk. All the users are stored in a MySQL database and using some funky code your users can FTP in to your system using their username and password and upload into their home directory.

Each user can access ~/public_html in their home directory using www.username.visp.tld, for exmaple:

 www.johndoe.slowhosts.com  -> /home/slowhosts.com/j/johndoe/public_html/
 www.theo.smallbrother.com  -> /home/smallbrother.com/t/theo/public_html/
 www.john666.blahmeep.co.uk -> /home/blahmeep.co.uk/j/john666/public_html/

If you had a database with your users in:

CREATE TABLE Users (
  Username varchar(25) NOT NULL default '65535',
  VISP varchar(60) NOT NULL default 'anlx.net',
  UID int(4) NOT NULL default '0',
  GID int(4) NOT NULL default '0',
  Home varchar(255) NOT NULL default '',
) TYPE=MyISAM;

You could have a select like this:

SELECT
	CONCAT('/home/', VISP, '/', LEFT(Username, 1), '/', Username, '/public_html/'), 
	UID, 
	CONCAT('postmaster', '@', Username, '.', VISP),
	GID 
FROM Users WHERE (Username = '%s') AND (VISP = '%s')

This would set the hostnames correctly, and also set the email address to postmaster@username.visp.tld.

If you are sure you want to break your servers, you can Download the code. Don't hold me responsible when you loose your job because you installed this on your web servers ;)

Frequently Asked Questions

  • Q: Will this work with apache 2.x?. A: No! I don't use apache 2 in a hosting enviroment, and until I do, I have no need to write it. If you want apache 2 support, look at my amazon wishlist (in the menu on the left hand side), choose some things from it, e-mail me to let me know what you'll buy when it's done, and i'll consider doing it :)
  • Q: Can I store other values, such has PHP config directives in SQL?. A: No! Because of the way apache works, it's not possible without a GREAT BIG hack to get it working, sorry. I really don't like great big hacks.

Feel free to contact me with any questions, after you have read ALL of the above.