Aug 31

One of the fun tasks I had to look at recently was establishing a way of serving up multiple sites from a single installation of Drupal on a machine. This is incredibly useful for developers as it allows them a great deal of control over their local development sites with the convenience of not exclusively dealing with IP addresses… unless they want to :)

For those who might be new to this kind of concept, it’s fairly similar to how IIS might serve up multiple websites on a single IP address by analysing the host headers. However, we’re using Apache so we don’t need to worry about IIS’s interesting interpretation of GUI design.

Drupal.org has a fairly detailed overview of the process, but it’s missing a fairly useful step I believe which is how to alias another IP to your local loopback adapter. I’m running a copy of OS X 10.5 (Leopard to most) and if I ping the address 127.0.0.2 I don’t get a response. “That’s strange” you might say… and indeed in terms of convention, it is. If I do the same on a Windows or Linux box it immediately comes back with a chirpy:

64 bytes from 127.0.0.2: icmp_seq=3 ttl=64 time=0.059 ms

Lovely. So why does OS X not respond on this? It could well be down to Apple developers’ interpretation of Special use IP v4 Addresses.

Either way, it doesn’t respond by default. A temporary solution is this sudo ifconfig lo0 alias 127.0.0.x up – this assigns an additional address to the loopback adapter (lo0). It isn’t persistent and I haven’t found out exactly why at this stage, but it’s on the to do list :)

Configuring Apache

I’m assuming you’ve already got Apache set up with the prerequisites for Drupal. If not, get this done first as I’m not covering clean URLs, PHP configuration or installation basics…

Once you’ve got an IP address to listen on, you’ll need to get Apache listening on that address and serving up the right stuff. Your Apache config may differ to mine as implementations are different (sometimes considerably!) from distro to distro. Broadly speaking, you will want to find the place where you can configure your VirtualHosts. Sometimes that’s at the bottom of your httpd.conf file, other times it’ll be in a totally seperate directory – it varies depending on your server version and distro. Eeek.

Boiler plate VirtualHost config

Assuming you’re using MAMP on OS X – you can find the VirtualHosts section at the bottom of the /conf/apache/httpd.conf file. All you have to do is add the following declaration:

<VirtualHost 127.0.0.x:80>
DocumentRoot /path/to/your/drupal/root/
ServerName whatever.name.you.like
</VirtualHost>

Adding a DNS entry, or editing your local hosts file

If you can do this part through DNS on your network, that’s ace. If not, you can edit a file on your machine that binds a name to a given IP address. This little beauty lives under:

/private/etc/hosts

under OS X. I know, what a great path name! For more information, see Wikipedia’s entry for the hosts file.

On a new line, add your 127.0.0.x address plus the corresponding name you’re using for your VHost. Save the file, ping whatever.name.you.like and you should get a chirpy response on 127.0.0.x. If not, make sure you’ve not got any spelling mistakes or haven’t bound the alias improperly. If you screwed up aliasing… using the ifconfig -alias command to remove it.

Once that’s done – reboot Apache for the changes to kick in. Point your browser to your site name to check Apache is doing what it’s meant to do.

Configure Drupal

Ok, so by now you should have a web site that is serving up Drupal which should be prompting you to install a new site. Nice.

The next step is to add an entry into the /drupal/sites/ folder. Create a folder in this for your site, eg: whatever.name.you.like – it’s really important you’re consistent as if the folder name doesn’t match what’s in the host header Drupal won’t know which site to serve up and defaults to the /drupal/sites/default/ folder.

Once you’ve created your folder, all you need to do is copy the settings.php file from another site, or the default.settings.php file under /drupal/sites/default/ into whatever.name.you.like/settings.php.

After that, edit the file, complete the $db_url (and if necessary – the $base_url) for your specific database, save the file and browse to http://whatever.name.you.like

If all has been done correctly you should now be looking at a separate site being served up by a single instance of Drupal.

Summary

This approach is really useful, because if you have a lot of sites and/or clients you can run all those sites locally. All you need is:

  • A database per site
  • A free IP address on your loopback adapter
  • An entry in your hosts file or DNS for your site name
  • An Apache VirtualHost
  • A sites folder for Drupal with the same name

Other considerations

  • You don’t have to use a loopback adapter. It could be another network card in your machine, or indeed another machine.
  • You don’t have to use site names – you can stick with IP addresses too.
  • Drupal settings.php files are just that – PHP files. If you’ve got loads of sites, consider centralising all site settings into a single file and using the require_once() function to control which files are loaded, depending on which site is being requested.

Tagged with:
preload preload preload