Project

General

Profile

Docs ModMySQLVhost » History » Revision 11

Revision 10 (Anonymous, 2007-11-30 12:12) → Revision 11/22 (Anonymous, 2007-12-01 05:39)

[[TracNav(DocsToc)]] 
 {{{ 
 #!rst 
 ==================== 
 MySQL-based vhosting 
 ==================== 

 ----------------------- 
 Module: mod_mysql_vhost 
 ----------------------- 

 .. meta:: 
   :keywords: lighttpd, mysql, vhost 

 .. contents:: Table of Contents 

 Description 
 =========== 

 With MySQL-based vhosting you can store the path to a given host's 
 document root in a MySQL database. 

 .. note:: Keep in mind that only one vhost module should be active at a time. 
           Don't mix mod_simple_vhost with mod_mysql_vhost. 

 Options 
 ======= 

 Example: :: 

   mysql-vhost.db               = "lighttpd" 
   mysql-vhost.user             = "lighttpd" 
   mysql-vhost.pass             = "secret" 
   mysql-vhost.sock             = "/var/run/mysqld/mysqld.sock" 
   mysql-vhost.sql              = "SELECT docroot FROM domains WHERE domain='?';" 


 MySQL setup: :: 

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

   CREATE DATABASE lighttpd; 

   USE lighttpd; 

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

   INSERT INTO domains VALUES ('host.dom.ain','/http/host.dom.ain/'); 

 }}} 


 == Per-vhost configuration == 

 I wanted to be able to add configuration directives deirectives per vhost.    This is how I did it. 

 {{{ 
 CREATE TABLE IF NOT EXISTS `domains` ( 
   domain varchar(64) NOT NULL PRIMARY KEY, 
   docroot varchar(128) NOT NULL, 
   config text, 
 ) 
 }}} 


 then I added this line to my mysql-vhost.conf (but you can use lighttpd.conf) 

 {{{ 
 include_shell "/usr/share/lighttpd/mysql_vhost.py lighttpd lighttpd secret" 
 }}} 



 the parameters are just the info above.    I didn't know how to pass the actual variables (maybe someone else does?) so I just repeated their values. 

 I then made a script called /usr/share/lighttpd/mysql_vhost.py, which just prints the info.    You could write this in perl, php, or bash, if you want. 

 it should output this format: 

 {{{ 
 $HTTP["host"] == "<DOMAIN_FROM_DATABASE>" { 
 <CONFIG_FROM_DATABASE> 
 } 
 }}} 

 mine looks like this: 

 {{{ 
 #!/usr/bin/env python 
 import sys 
 import MySQLdb 

 # load configuration data from the database 
 db=MySQLdb.connect(host='localhost', db=sys.argv[1], user=sys.argv[2], passwd=sys.argv[3]) 
 cur = db.cursor() 
 cur.execute("SELECT * FROM domains") 
 rs=cur.fetchall() 
 db.close() 

 for domain in rs: 
     print "$HTTP[\"host\"] == \"%s\" {\n%s\n}" % (domain[0], domain[2]) 
 }}} 

 Now, you can put whatever directives you want in the config field. 
 Make sure to restart your server to enable the settings, if you change them in the database.