=^.^=

Mass Virtual Hosting Part Five: Dynamic MySQL Based Apache vhost Configuration with mod_vdbh

karma

Please skip to Part 6 for advanced vhost configuration with database-backed flat files.

mod_vdbh is a relatively obscure gem of an Apache module. It doesn't look like it has been maintained in years and its website is gone so Google (at present) won't give you much on it except the usual package list results and odd blog post like this. Despite living in a day of quadruple-digit version numbers, some software reaches a point where it's "just done." (if you don't believe me look at qmail). I'm hoping that's the case here, because if there's any 0day or problems with future versions of apache we're SOL. FreeBSD and Gentoo keep it in their package managers and that's more or less good enough for me.

The README is hard to find so I'm posting it below for your viewing pleasure:

Configuring mod_vdbh in Apache Configure Files

In order to use mod_vdbh with Apache Web Server server configuration blocks will need to be configured with mod_vdbh configuration directives described in the table below. mod_vdbh configuration directives must be located in a server configuration block (ie <VirtualHost></VirtualHost>).

vdbh    This switch makes mod_vdbh active for the specified server.
vdbh_CLIENT_COMPRESS    Enables the CLIENT_COMPRESS option with a MySQL server allowing the connection data to be compressed. Using this option will likely require more cpu time and less network bandwidth.
vdbh_CLIENT_SSL Enables the CLIENT_SSL option when communicating with a MySQL server.
vdbh_MySQL_Database     Sets the database name to use when running a query for file name translations.
vdbh_MySQL_Table        Sets the table name to use when running a query for file name translations.
vdbh_MySQL_Host_Field   Sets the name of the host field in the table specified by vdbh_MySQL_Table.
vdbh_MySQL_Path_Field   Sets the name of the path field in the table specified by vdbh_MySQL_Table.
vdbh_MySQL_Environment_Field    Sets the name of the environment field in the table specified by vdbh_MySQL_Table. This optional field contains data that will be set to the VDBH_ENVIRONMENT variable.
vdbh_MySQL_Host Sets the internet hostname where the MySQL server is located at. This option is not required and defaults to localhost.
vdbh_MySQL_Port Sets the port number to connect to when making a connection to a MySQL server. This option is not required and defaults to 0 for using a UNIX domain socket.
vdbh_MySQL_Username     Sets the username required to gain access to the MySQL server. This option is not required.
vdbh_MySQL_Password     Sets the password required to gain access to the MySQL server. This option is not required.
vdbh_Path_Prefix        Sets an optional location to prefix translations by. This option is not required.
vdbh_Default_Host       Sets the default host to use if a non-HTTP/1.1 request was received. This option is not required and usually won't do anything because the Apache Web Server by default catches these errors.
vdbh_Declines   Sets a list of glob patterns to match URIs against. If any match occurs then the URI is declined to the next translate phase.

The vdbh_MySQL_Host_Field and vdbh_MySQL_Path_Field along with vdbh_MySQL_Environment_Field are available as environment variables and can be included in logs if a LogFormat is defined for them. The environment variables are labled VDBH_HOST, VDBH_PATH, and VDBH_ENVIRONMENT. Information on how to use LogFormat is available at http://httpd.apache.org/docs/mod/mod_log_config.html. An example configuration may look something like this.

NameVirtualHost 206.9.161.29

<VirtualHost 206.9.161.29>
vdbh On
vdbh_CLIENT_COMPRESS On
vdbh_MySQL_Database virtual_hosts
vdbh_MySQL_Table virtual_hosts
vdbh_MySQL_Host_Field server
vdbh_MySQL_Path_Field path
vdbh_MySQL_Environment_Field environment_variable
vdbh_Default_Host julia.fractal.net
vdbh_Declines .htpasswd *.txt
</VirtualHost>

The corresponding database schema would look like this.

CREATE TABLE virtual_hosts (
server char(255) NOT NULL,
path char(255),
environment_variable char(255),
PRIMARY KEY (server)
);

INSERT INTO virtual_hosts VALUES ('julia.fractal.net','/export/home/mlink/public_html','julia.fractal.net');
INSERT INTO virtual_hosts VALUES ('visualphixation.com','/export/home/carlosp','visualphixation.com');
INSERT INTO virtual_hosts VALUES ('www.visualphixation.com','/export/home/carlosp','www.visualphixation.com');
INSERT INTO virtual_hosts VALUES ('www.fractal.net','/export/web/www.fractal.net','www.fractal.net');
INSERT INTO virtual_hosts VALUES ('fractal.net','/export/web/www.fractal.net','fractal.net');

Other handlers should still work accordingly.  mod_vdbh declares its translate_name phase as AP_HOOK_FIRST so it can run before other translations.  An example configuration allowing mod_tcl in specific directories follows.

<VirtualHost 206.9.161.29>
vdbh On
vdbh_CLIENT_COMPRESS On
vdbh_MySQL_Database virtual_hosts
vdbh_MySQL_Table virtual_hosts
vdbh_MySQL_Host_Field server
vdbh_MySQL_Path_Field path
vdbh_MySQL_Environment_Field environment_variable
vdbh_Default_Host julia.fractal.net
vdbh_Declines .htpasswd *.txt

<Directory /export/web/www.fractal.net>
AddHandler tcl-handler tm

Tcl_ContentHandler content_handler
</Directory>

<Directory /export/web/www.fractal.net/images>
SetHandler default-handler

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny
Allow from all
</Directory>

<Directory /export/web/www.fractal.net/files>
SetHandler default-handler

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny
Allow from all
</Directory>
</VirtualHost>

Additional Information

mod_vdbh assumes that its connection to the MySQL server is persistent. If there are excessive disconnections try setting the wait_timeout variable for MySQL to a larger value. Apache Web Server 2.0 is required, and at least MySQL 3.23 is required.

References

mod_vdbh is an Apache 2.0 module using MySQL libraries, more about Apache Web Server can be found at http://www.apache.org/. Documentation regarding MySQL can be found at http://www.mysql.com/

That's right. That's all there is to it. If you've been following the other parts in this series on Mass Virtual Hosting you should have a keen eye for the ways MySQL-backed services can be used (sexually?) to integrate into your custom web hosting front-end - or anything that interfaces with MySQL/ODBC!

Comments

karma

No problem. I would point out that some of the best modules don't come from Apache and that there is nothing wrong with compiling software from source but if it makes you uncomfortable that's all there is to it. I would recommend thoroughly testing any configuration changes before implementing them on a production box, whether they are made by Apache or third party, just because something works for someone else doesn't mean it will work the same in your particular environment. No programmer/quality control is perfect.

• Tinker

Yes, you are quite correct, I have only worked with Apache for somewhere beyond 10 years so I guess I could be classed as a beginner. Enough of one anyway that I will not risk running a production system on a module that is only available on one particular distro because it certainly is not available direct from apache.org, whether building from source or not. Anyway, thanks for the articles.

karma

You are quite wrong. In writing this article I had it working with apache 2.2.15, I get the feeling you are new to apache as a whole, this is not something for a beginner to tackle. Perhaps once you have acclimatized yourself to the fundamentals of apache administration you will be better suited to install and configure the module.

I settled on this method, mentioned at the top of this page, because mod_vdbh lacked the features I needed, you may find it easier or harder depending: http://foxpa.ws/2010/07/09/mass-virtual-hosting-part-six-remote-apache-vhost-configuration-and-privileged-command-execution-with-database-backed-upkeep-system/

• Tinker

After wasting a great deal of time looking I've come to the conclusion that mod_vdbh is not available for apache 2.2x and, to quote from apache.org:

"Add-in modules for Apache 1.3 or 2.0 are not compatible with Apache 2.2. If you are running third party add-in modules, you must obtain modules compiled or updated for Apache 2.2 from that third party, before you attempt to upgrade from these previous versions. Modules compiled for Apache 2.2 should continue to work for all 2.2.x releases."

So, thanks for the article but I will investigate another route for facilitating mass virtual hosting.

R

karma

This is not a core apache module, and I don't imagine most distros would include it in your release of apache by default. First you're going to have to use whatever package manager whatever you're running in the VM uses to install the module. If you can't find it, you will have to compile it from source. There are instructions in the documentation on configuring the module, chances are a standalone apache config file will be installed to /etc/apache2/modules.d (or whatever your distribution uses) if you install the module via a package manager. If you install from source you will probably have to copy one to the relevant location then include it in your httpd.conf. It's inadvisable to dump the configuration directly into your main apache config file for the sake of organization.

• Tinker

I get the message:
Syntax error on line 995 of /etc/httpd/conf/httpd.conf:
Invalid command 'vdbh', perhaps misspelled or defined by a module not included in the server configuration

Line 995 of httpd is:
vdbh On

The apache install is new, the only changes made to httpd.conf are those suggested in your article. I have no problems with experimenting with the config, it is only a VM on my MacBook Pro that I am using to test various setups with the aim to have a completely 'soft' setup for Apache (and sendmail, but that is another matter).

karma

Need you to be a little more specific; have you already installed it and added it to your apache config file? If so what are the errors you are encountering?

• Tinker

Hi,

I'm using Apache 2.2.9 and it does not recognise vdbh, is it necessary to install or enable it in some fashion?