Creating Simple Virtual Hosts With mod_mysql_vhost On Lighttpd (Debian Etch)

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Tue, 2008-08-05 11:34. :: Debian | Lighttpd

Creating Simple Virtual Hosts With mod_mysql_vhost On Lighttpd (Debian Etch)

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 07/31/2008

This guide explains how you can use mod_mysql_vhost to create simple virtual hosts on a lighttpd web server on Debian Etch. With mod_mysql_vhost, lighttpd can read the vhost configuration from a MySQL database. Currently, you can store the domain and the document root in the MySQL database which results in very simple virtual hosts. If you need more directives for your vhosts, you'd have to configure them in the global section of lighttpd.conf, which means they'd be valid for all vhosts. Therefore, mod_mysql_vhost is ideal if your vhosts differ only in the domain and document root.

I do not issue any guarantee that this will work for you!

 

1 Installing MySQL 5.0

First we install MySQL 5.0 like this:

apt-get install mysql-server mysql-client

Create a password for the MySQL user root (replace yourrootsqlpassword with the password you want to use):

mysqladmin -u root password yourrootsqlpassword

Then check with

netstat -tap | grep mysql

on which addresses MySQL is listening. If the output looks like this:

tcp        0      0 localhost.localdo:mysql *:*                     LISTEN     2713/mysqld

which means MySQL is listening on localhost.localdomain only, then you're safe with the password you set before. But if the output looks like this:

tcp        0      0 *:mysql *:*                     LISTEN     2713/mysqld

you should set a MySQL password for your hostname (my hostname is server1.example.com here), too, because otherwise anybody can access your database and modify data:

mysqladmin -h server1.example.com -u root password yourrootsqlpassword

 

2 Installing Lighttpd And mod_mysql_vhost

You can install lighttpd (if it's not already installed) and mod_mysql_vhost as follows:

apt-get install lighttpd lighttpd-mod-mysql-vhost

To enable mod_mysql_vhost, we open /etc/lighttpd/lighttpd.conf and add/enable "mod_mysql_vhost", in the server.modules stanza:

vi /etc/lighttpd/lighttpd.conf

[...]
server.modules              = (
            "mod_access",
            "mod_alias",
            "mod_accesslog",
            "mod_mysql_vhost",
#           "mod_rewrite",
#           "mod_redirect",
#           "mod_status",
#           "mod_evhost",
#           "mod_compress",
#           "mod_usertrack",
#           "mod_rrdtool",
#           "mod_webdav",
#           "mod_expire",
#           "mod_flv_streaming",
#           "mod_evasive"
 )
[...]

Afterwards, we restart lighttpd:

/etc/init.d/lighttpd restart

 

3 Configuring mod_mysql_vhost

Now we log in to MySQL...

mysql -u root -p

... and create the database lighttpd:

CREATE DATABASE lighttpd;

Next we create a database user (which we name lighttpd as well) with SELECT privileges for the lighttpd database:

GRANT SELECT ON lighttpd.* TO lighttpd@localhost IDENTIFIED BY 'secret';
GRANT SELECT ON lighttpd.* TO lighttpd@localhost.localdomain IDENTIFIED BY 'secret';
FLUSH PRIVILEGES;

(Replace secret with a password of your choice.)

Then we create the domains table in the lighttpd database and leave MySQL:

USE lighttpd;

CREATE TABLE domains (
domain varchar(64) not null primary key,
docroot varchar(128) not null
);

quit;

Now we open /etc/lighttpd/lighttpd.conf and add the following mod_mysql_vhost configuration at the end of the file:

vi /etc/lighttpd/lighttpd.conf

[...]
mysql-vhost.db             = "lighttpd"
mysql-vhost.user           = "lighttpd"
mysql-vhost.pass           = "secret"
mysql-vhost.sql            = "SELECT docroot FROM domains WHERE domain='?';"
mysql-vhost.hostname       = "localhost"
mysql-vhost.port           = 3306

(Replace secret with the password you've previously set for the lighttpd MySQL user.)

Restart lighttpd:

/etc/init.d/lighttpd restart

Now it's time to configure virtual hosts...

 

4 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).

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/');
INSERT INTO domains VALUES ('www.example.org','/var/www/www.example.org/web/');

We can now leave the MySQL shell:

quit;

That's it, the vhosts are now configured and working, and no lighttpd restart is required.

To check if the vhosts are working as expected, we create an index.html file in each document root, one with the string "www.example.com" in it, the other one with the string "www.example.org"...

echo "www.example.com" > /var/www/www.example.com/web/index.html
echo "www.example.org" > /var/www/www.example.org/web/index.html

and call http://www.example.com and http://www.example.org in a browser. http://www.example.com should show www.example.com, and http://www.example.org should display www.example.org.

 

5 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 Sannny (registered user) on Wed, 2008-08-06 13:50.

If the url contains port numbers like "http://howtoforge.com:80/index.html", lighttpd will lookup the database with the port number. To ignore this, use

"SELECT docroot FROM domains WHERE domain= SUBSTRING_INDEX('?', ':', 1);"

as sql statement.

You also can add a new column into the database table with the port number to separate the docroots for different ports. 

"SELECT docroot FROM domains WHERE domain = SUBSTRING_INDEX('?', ':', 1) AND port = SUBSTRING_INDEX('?', ':', -1);"

Greets, Sannny