After hunting for months to find a way of easily adding, removing and editing apache virtual hosts without having to log in via ssh, and manually edit the httpd.conf file, I found out about an apache module called mod_shapvh. This module prevents you needing to have hundreds of VirtualHost definitions
The intention of this document is to explain how I have used this module, and to give you guidance on how to use it on your own system.
One thing that is missing on Theo's site is the Makefile for this module. This is shown below...
APXS = /usr/bin/apxs
all: $(APXS) -i -c -l mysqlclient ./mod_shapvh.c
This simple Makefile should build and install the module. You will (obviously) need APXS installed.
The way your setup your database for this module is entirly up to you. I use libnss-mysql for my servers, so I have a setup that intergrates with the existing table data. Below are the CREATE TABLE statements that describe my database layout.
# # Table structure for table `http_hosts` # CREATE TABLE http_hosts ( host_id int(10) unsigned NOT NULL auto_increment, site_id int(10) unsigned NOT NULL default '0', hostname varchar(255) NOT NULL default '', PRIMARY KEY (host_id) ) TYPE=MyISAM; # # Table structure for table `http_sites` # CREATE TABLE http_sites ( site_id int(10) unsigned NOT NULL auto_increment, description varchar(255) NOT NULL default '', user_id int(11) NOT NULL default '0', email varchar(255) NOT NULL default '', docroot varchar(255) NOT NULL default '', PRIMARY KEY (site_id) ) TYPE=MyISAM;
The next task in setting up this module, is to design a SELECT statement that will return the required data. I use the following statement.
SELECT http_sites.docroot as DocRoot, nss_user.uid as UID, http_sites.email as Emailaddress, nss_user.gid as GID FROM http_sites INNER JOIN http_hosts ON http_sites.site_id = http_hosts.site_id INNER JOIN nss_user ON http_sites.user_id = nss_user.user_id WHERE http_hosts.hostname= '%s'
IMPORTANT: Order is very important here, names of fields can be anything, but the order of the fields must be maintained.
NOTICE By default you can only use GIDs and UIDs greater then 1000, if you wish to change this modify the source
The next job is to edit your httpd.conf. Below shows what needs adding to the httpd config file.
LoadModule shapvh_module /usr/lib/apache/1.3/mod_shapvh.so
ShapVHOn on
ShapVhHost localhost
ShapVhUser {user}
ShapVhPass {password}
ShapVhDbName {database}
ShapVhDefaultRoot /var/www
ShapVhDefaultAdmin webmaster@pengus.net
ShapVhQuery {sql select statement}
Now all that remains is to restart apache, and fix all your errors :)