Creating Advanced MySQL-Based Virtual Hosts On Lighttpd (Debian Etch) - Page 2

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Sun, 2008-08-17 18:14. ::

5 Configuring Virtual Hosts

I will now configure two virtual hosts, one for www.example.com (with the document root /var/www/www.example.com/web) and one for www.example.org (with the document root /var/www/www.example.org/web).

I will add custom directives to both vhosts. For the www.example.com vhost I will enable directory listings and create an alias test which points back to the document root /var/www/www.example.com/web, and for the www.example.org vhost I will disable directory listings.

First, we create the document roots of both web sites (if they don't already exist):

mkdir -p /var/www/www.example.com/web
mkdir -p /var/www/www.example.org/web

Then we log in to MySQL...

mysql -u root -p

USE lighttpd;

... and create the vhosts as follows:

INSERT INTO domains VALUES ('www.example.com','/var/www/www.example.com/web/','dir-listing.activate = "enable"\nalias.url = ( "/test" => "/var/www/www.example.com/web" )');
INSERT INTO domains VALUES ('www.example.org','/var/www/www.example.org/web/','dir-listing.activate = "disable"');

As you can see in the first INSERT statement, if you want to use more than one custom directive, put a linebreak (\n) between the directives.

We can now leave the MySQL shell:

quit;

That's it, the vhosts are now configured. To check if our /usr/share/lighttpd/mysql_vhost.py script is working as expected, we can call it on the command line...

/usr/share/lighttpd/mysql_vhost.py lighttpd lighttpd secret

... and it should display the correct vhost configurations:

server1:~# /usr/share/lighttpd/mysql_vhost.py lighttpd lighttpd secret
$HTTP["host"] == "www.example.com" {
server.document-root = "/var/www/www.example.com/web/"
dir-listing.activate = "enable"
alias.url = ( "/test" => "/var/www/www.example.com/web" )
}
$HTTP["host"] == "www.example.org" {
server.document-root = "/var/www/www.example.org/web/"
dir-listing.activate = "disable"
}
server1:~#

Unlike the mod_mysql_vhost way, this method needs a lighttpd restart whenever the vhost configuration is changed, so let's restart lighttpd:

/etc/init.d/lighttpd restart

 

6 Testing

Now let's test if our MySQL-based vhosts www.example.com and www.example.org are working as expected. I'm assuming that you have no index files in each document root.

Let's call http://www.example.com (for which we have enabled directory listings) in a browser, and you should see a directory listing:

Now let's go to http://www.example.com/test (there's no directory test in that vhost), and it should go back to the document root because we have configured that alias for the www.example.com vhost:

Now go to http://www.example.org, and you should get a 404 - Not Found error (unless there's an index file in the document root) because we've disabled directory listings for that vhost:

Now let's try http://www.example.org/test - as this directory does not exist and we haven't defined such an alias in the www.example.com vhost, we get a 404 - Not Found again:

So everything's working as expected.

 

7 Links


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by andybill (registered user) on Tue, 2008-12-16 20:01.

I've just done this on an FC8 box; only real difference was start off with

yum install python MySQL-python

Other than some file locations (my original Lighttpd docroot was at /var/www/lighttpd; there is no /usr/share/lighttpd so I put the python script elsewhere), all works just fine.

Andy