VMware Images:
|
Creating Advanced MySQL-Based Virtual Hosts On Lighttpd (Debian Squeeze)
|
Submitted by falko (Contact Author) (Forums) on Mon, 2012-08-20 17:44. :: Debian | Web Server | Lighttpd | MySQL
Creating Advanced MySQL-Based Virtual Hosts On Lighttpd (Debian Squeeze)Version 1.0 This guide explains how you can create advanced virtual hosts on a lighttpd web server on Debian Squeeze that are stored in a MySQL database. The method described here does not use the lighttpd mod_mysql_vhost module, and unlike mod_mysql_vhost (which allows you to store only the hostname and document root of a vhost in a database), this method allows to store individual configuration directives for each vhost in the MySQL database. I do not issue any guarantee that this will work for you!
1 Installing MySQL 5First we install MySQL 5 like this: apt-get install mysql-server mysql-client You will be asked to provide a password for the MySQL root user - this password is valid for the user root@localhost as well as root@server1.example.com, so we don't have to specify a MySQL root password manually later on: New password for the MySQL "root" user: <-- yourrootsqlpassword
2 Installing Lighttpd, Python, And python-mysqldbWe will use a Python script to read the vhost configurations from the MySQL database, therefore we must install Python and python-mysqldb in addition to lighttpd. You can install these packages as follows: apt-get install lighttpd python python-mysqldb
3 Preparing The MySQL DatabaseNow 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'; (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 IF NOT EXISTS domains ( quit;
4 Creating The Python Script To Read The Vhost Configuration From The DatabaseNow we create the Python script /usr/share/lighttpd/mysql_vhost.py which will connect to the lighttpd database and read the vhost configurations from it: vi /usr/share/lighttpd/mysql_vhost.py
Make the script executable: chmod 755 /usr/share/lighttpd/mysql_vhost.py Now we must tell lighttpd to use that script. Therefore we open /etc/lighttpd/lighttpd.conf and add the following line at the end of it: vi /etc/lighttpd/lighttpd.conf
(The first lighttpd refers to the name of the MySQL database, the second lighttpd to the database user, and secret to the MySQL password.) Restart lighttpd afterwards: /etc/init.d/lighttpd restart
5 Configuring Virtual HostsI 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 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" )'); 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: root@server1:~# /usr/share/lighttpd/mysql_vhost.py lighttpd lighttpd secret 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 TestingNow 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
|








Recent comments
13 hours 38 min ago
16 hours 33 min ago
17 hours 47 min ago
19 hours 11 min ago
20 hours 49 min ago
22 hours 17 min ago
23 hours 31 min ago
1 day 15 hours ago
1 day 16 hours ago
1 day 20 hours ago