Run multiple Drupal sites easily

By Erik, Mon, 05/30/2011 - 01:30
Once you've set up a development environment, probably the next thing that you've run into is that you're hooked and you need another (and another, and another...). Drupal can easily handle multiple sites run out of the same Drupal codebase. Drupal multisites are super powerful and if you're not hacking core are pretty easy way to keep things a little more centralized. Some folks use multisites only when the individual subsites have common database elements, or tasks, or owners, etc. Especially on a local development machine, I tend to run everything in one main Drupal codebase. For me, it makes tasks like Drush aliases, moving sites, configuring backups, running cron, etc. a little easier. I used to set up localhost multisites with symlinks and accessing them via urls like: http://localhost/mysubsite. But the problem was that very very rarely did I have a site that was going to be run in the real world in a subdirectory. Usually the finaly URL was going to be instead. Drupal handles this pretty well, but especially if you ever have to manually enter a path to an img or something, you're in for trouble.

Local DNS

The key would be to be able to have my own domain names pointed locally. For example so that *.dev points to localhost. Turns out that this is easy on most Linux based servers, but not on Mac OS X. To have wildcards, you need to run a local DNS server. I started down that route and decided that with being sometimes on a corporate LAN and sometimes at home, it really wasn't easy for me to get it right. So I turned to /etc/hosts. You can easily add fake (or real) domain names to /etc/hosts. Simply map which is the IP address for localhost to any domain you'd like.
Now you can visit and it should pull up the same as http://localhost

Modifying Apache

So to have Apache serve your local ~/Sites/drupal folder whenever the domain ends in .dev, you can use Apache NameVirtualHost commands. Add the following to your /Applications/MAMP/conf/apache/httpd.conf file (backing up the original of course):
NameVirtualHost *:80
<VirtualHost *:80>
  DocumentRoot /Users/username/Sites
  ServerName localhost
<VirtualHost *:80>
  DocumentRoot /Users/username/Sites/drupal
  ServerAlias *.dev

Setup Multisite

Now you need to create a new /sites/ subdirectory for your site:
cd ~/Sites/drupal/sites
cp default/default.settings.php settings.php
Now with a single line added to /etc/hosts and a new directory in ~/Sites/drupal/sites with its own settings.php file, you can have another new Drupal site. Next: Rectifying the vs. dilemma