Lighttpd
上QQ阅读APP看书,第一时间看更新

MySQL based Virtual Hosting

To have the freedom to put our sites up anywhere we want, we can use mod_mysqlvhost. This module reads the document root for a domain from a MySQL database table and will thus need a running MySQL server. mod_mysqlvhost gives the maximum flexibility, usually at a small performance cost.

Many Web projects already use MySQL as a backend database, so the cost of running MySQL does not need to be taken into the calculation. For all other installations, the cost of running a MySQL server would probably not be worth the additional flexibility.

Installing MySQL

Note

If you already have a running MySQL, you can skip this section.

First, we need to download MySQL. The MySQL download site, available under http://www.mysql.com/downloads/mysql/, links to mirrors that have packages for almost all systems. To pick the optimal package source, here are my recommendations:

The options file fo Windows installations should include the following lines (given that you have installed MySQL in C:\mysql):

[mysqld]
basedir=C:/mysql # note the / instead of \
datadir=C:/mysql/data

The installers usually set up their own options file.

Bringing MySQL and mod_mysqlvhost Together

mod_mysqlvhost is as flexible as possible you can create your database the way you want it. The only thing that is really needed is a table with at least two columns, one of which contains the domain name and the other the path. Both are usually stored as VARCHARs. A possible database setup SQL script might be:

GRANT SELECT ON domains.* TO lighttpd@localhost
IDENTIFIED BY '********';
CREATE DATABASE domains;
USE domains;
CREATE TABLE domains (
domain VARCHAR(64) not null primary key,
document_root VARCHAR(256) not null);

The corresponding Lighttpd configuration is:

mysql-vhost.db = "domains"
mysql-vhost.user = "lighttpd"
mysql-vhost.password = "********"
mysql-vhost.sock = "/var/sock/mysql.lighttpd.sock"
mysql-vhost.sql = "SELECT document_root from domains WHERE domain=?"

Of course, we should take a password not only consisting of asterisks. Adding, moving, and deleting domains can now be done with the following SQL:

# Add a domain
INSERT INTO domains
VALUES ('subdomain.ourdomain.net', '/var/www/sub1');
# Move a domain to another directory
UPDATE domains SET document_root = '/var/www/subdomain'
WHERE domain = 'subdomain.ourdomain.net';
# Delete a domain
DELETE FROM domains WHERE domain='subdomain.ourdomain.net';
# Change a domain name by deleting the old and adding a new entry.

Now we have everything in place to be completely free with our domain to document root mapping.

Note

MySQL Administration Programs

MySQL comes with a very minimal command line client. To speak with the server more comfortably, the following programs might help:

MySQL Gui Tools come from the same folks as the MySQL database itself, so they are easily the tool of choice when it comes to working with MySQL.

phpMyAdmin from http://phpmyadmin.net is a CGI-based tool written in PHP. It even works with Lighttpd. To learn to install it, read chapter 9.

Java SQL Admin Tool can work with MySQL and is completely written in Java, so it "runs everywhere" according to the Sun Microsystems marketing slogan. Find it at http://www.trash.net/~ffischer/admin/index.html.

Under Linux, both Gnome and KDE have MySQL administration programs (Gnome MySQL client and KNoda). Lastly, go to http://freshmeat.net and search for "MySQL admin".