I have two websites, websitea and websiteb.

I have them both pointing to the same document root and all works fine, however I want websitea to have different db details to websiteb.

Database details are stored in /config/database.php

I could just write an if statement in that php file to tell it which database details to use depending on the hostname, however I am going to add another 10 websites like this, and that is going to get a pain very fast.

The second issue is I have a folder called /images/ which contains user uploaded pictures, these again need to be different.

When I was first thinking about it, I thought I could use apaches alias's - however thats not going to work with php, as doing

<?php include('config/database.php');?>

will not follow the alias (not a web request, it's a local dir lookup) and thus will fail.

So I am a bit stuck :)

Anyone have any thoughts?


Just use symlinks for all files and directories in the site's root folder.


I have a couple solutions that you may want to consider, but they do have downsides, they also assume that you are using Linux, or another *nix.

One option you could use here is to use unionfs. Unionfs is basically provides a method to combine two filesystems. The downside here is that unionfs isn't available in all systems by default. You may have to recompile a kernel.

So basically you could setup something that looks like this

/srv/www/basefolder (all the common files
/srv/www/localstuff/site1.example.org (files unique to a specific site)
/srv/www/unions/site1.example.org (unionfs ro:/srv/www/basefolder + rw:/srv/www/localstuff/site1.example.org)
/srv/www/unions/site2.example.org (unionfs ro:/srv/www/basefolder + rw:/srv/www/localstuff/site2.example.org)
/srv/www/unions/siten.example.org (unionfs ro:/srv/www/basefolder + rw:/srv/www/localstuff/siten.example.org)

Another option would be to simply use hardlinks. Setting this up is pretty easy to do using rsync. The downside of this is that you make break something for many sites, if you aren't paying attention. If you forget you have hard-linked everything together, and you make a change to the files in one location, that changes in all locations. You will also need to re-run the rsync command whenever you update files.

/srv/www/basefolder (all the common files

Use commands like this to build this structure

rsync -vr /srv/www/basefolder /srv/www/site1.example.org --link-dest /srv/www/basefolder
rsync -vr /srv/www/basefolder /srv/www/site2.example.org --link-dest /srv/www/basefolder
  • This is similar to what I have now - and want to move away from. (rsync - hard links) – Wizzard Aug 27 '11 at 5:53

Is placing all the common content in a separate place and creating many docroots with different config files and images folders and symlinks to common data directories an option? You can even create a bash script for automatic deployment of this structure.

  • This is similar to what I got, and want to move away from. Thanks :) – Wizzard Aug 27 '11 at 5:54

It isn't clear what uses the /images/ folder or how many php scripts you have.

However, it would be possible to rewrite the php to use config/HTTP_HOST.database.php so it scales as you add virtualhosts. If the php code uses the /images or creates the links to /images it could be rewritten to use HTTP_HOST as well (passed in from Apache). If the links come from another app you could use apache rewrite rules to put the vhost info in requests for /images/ so you end up with dirs that look like /images/ but map to unique things on disk.

More info on HTTP_HOST in php:


This solution would eliminate the need to duplicate data for each user deployment, but does require code changes and perhaps rewrite rules in apache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.