Friday, 19 September 2008

Apache Automatic Sites Using VirtualDocumentRoot

Right, so i hate having to go edit config files when i'm editing and changing loads of new sites every day. It's time consuming and it breaks up my development.


I have a Fedora linux development server setup. I have a staic ip address and I have bind setup and a domain (*.mydomain.com) with wildcard subdomains pointing to it. That allows me to make a new site on my subdomain per website i'm editing and then i can demonstrate that to clients without ftp-ing the site and uploading databases.


For example if i'm editing newsite.com, i download a copy to my dev server, then log into my dev server apache config file and add a virtualhost for newsite.com.mydomain.com I can then edit this version of the site, allow the client to approve the site, then ftp up my changes.


However, if you have over 20 of these sites it gets a bit confusing in your apache file, and it also breaks up development time when you edit the file, not to mention increasing the chance of you fucking things up.


Therefore, on a recent new install of my dev server, i decided to spend some time researching into apaches ability to rewrite stuff. I use mod_rewrite quite heavily and I wondered if it allows you to edit things before apache runs any php code. After much reading of peopletrying to do stupid nonsensical things with apache that have no real point... I happend across a way of doing this using the apache VirtualDocumentRoot. Here's an example;


<virtualhost>

ServerAlias *.mydomain.com

DocumentRoot /home/www/sites

VirtualDocumentRoot /home/www/sites/%-3+/

</virtualhost>


So, if you have <strong>sitea.com.mydomain.com</strong> this now maps to /home/www/sites/sitea.com/.


There is one small problem with this; If you use $_SERVER['DOCUMENT_ROOT'] in php, then this causes problems as document_root point to /home/www/sites. But, there's a work around for this. You have to make a php file to reset document_root and make that file run before anything else when the server requests this page. Here's an example of the php file;


<?

$location = explode(".",$_SERVER['HTTP_HOST']);

for($i=sizeof($location)-6; $i>=0; $i--) $lurl = '.'.$location[$i].$lurl;

$lurl = '/'.ltrim($lurl, '.');

$_SERVER[DOCUMENT_ROOT] .= $lurl;

?>


The above script resets the document_root variable correctly, Now you just need to edit the Apache config file;


<virtualhost>

ServerAlias *.mydomain.com

DocumentRoot /home/www/sites

VirtualDocumentRoot /home/www/sites/%-3+/

php_admin_value auto_prepend_file /home/www/set_path.php

</virtualhost>


Voila! You should now be able to make dev sites without having to worry about the apache config files ever again!

3 comments:

Ian said...

Excellent, thanks! Solved my problem wonderfully. I have a dev server set up in an almost identical fashion (except with dev and staging copies of sites) so it works great.

Slavi said...

This could be accomplished by using adding this line in a file that's always included e.g. config.php

$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);

Marvelan said...

Hi,
what a useful blog i like this so much, Its really important blog for me, Insertion of small Flash animation in some important pages or giving the introduction with Flash will always make your website look attractive.
Thanks for this great sharing.
jakveli