Using dnsmasq and virtual host wildcards for Drupal development

Dnsmasq is a lightweight, easy to configure DNS forwarder and DHCP server. This article explains how dnsmasq can help you development process by making it easier to create Drupal multi-sites on the fly.

We use DNSMasq on our personal development machines to allow for wildcard virtual hosts, that are each associated with a Drupal core. Each core has several multi-site domains associated with it that are used for testing and development. Below are examples of multi-site pseudo domain names we use for development in our team:

default.mpf || mysite.mpf || to n -> sites on a pressflow core
default.md5 || mysite.md5 || to n -> sites on a drupal5 core
default.md6 || mysite.md6 || to n -> drupal 6 core
default.md7 || mysite.md7 || to n -> drupal 7 core

Once the initial wild-card Apache virtual host & DNS mask is setup for that core, we don't have to edit our virtual host settings again. All domains suffixed with, for example ".md6" will point to the same Drupal core.

Our development team maintain an almost identical development environment. Passing urls and commands to each other on Skype during our regular development work-flow is pain-free process. Often we communicate in bash commands!

Here's how to create a wildcard virtual host and dns mask setting for domains ending with .drupal

Install dnsmasq:
sudo aptitude install dnsmasq

Edit your Apache vhost file and add the wildcard virtual host

<VirtualHost>
# The default server name is for the sites/mojahmedia.com Drupal installation
   ServerName default.drupal
   ServerAlias *.drupal
   # Path to the drupal root
   DocumentRoot /home/www/htdocs/drupal6/d6core

<Directory /home/www/htdocs/drupal6/d6core/>
   Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
</Directory>
</VirtualHost>

Now, edit the dnsmasq config file
$sudo vim /etc/dnsmasq.conf

And add these lines:

address=/.drupal/127.0.0.1
listen-address=127.0.0.1

Now, edit this file:
sudo vim /etc/dhcp3/dhclient.conf

And remove the # comment from this line
prepend domain-name-servers 127.0.0.1;

Finally:

sudo vim /etc/resolv.conf
#Add as the first line:
nameserver 127.0.0.1
# Generated by NetworkManager
nameserver 127.0.0.1       # <<< add this line
nameserver 200.42.43.111   # this could be different in your file
nameserver 200.42.37.111   # this could be different in your file

Restart Apache and dnsmasq (sudo /etc/init.d/dnsmasq restart). Now, when you access to http://yoursite.drupal this will be processed as a new virtual host and point to your Drupal core. Setup as many Drupal multi-sites in your /sites directory as you like with the .drupal suffix and you won't have to restart Apache or edit Apache config files.

As you can see from the example above, we have a dns mask setup for each Drupal core that we work with. We use a simple bash script that allows to type in commands like:

$multisite opengrid.mpf
#or
$multisite newclient.atrium

which creates a database, copies over the template files into the corresponding Drupal core and gives us a multi-site instance ready for testing in 2 seconds. Sweet!

Conversation

Hola! We are following your website Using dnsmasq and virtual host wildcards for Drupal development | Mojah Media for some time now and lastly have the bravery to go ahead and give you a shout out from Humble Texas I just want to tell you keep up this great work! Oh yeah what about Libya incredible tv news what does one believe ...

Permalink

It seems that in recent versions of Debian and Ubuntu you need to edit the /etc/dhcp/dhclient.conf file (note the lack of '3' in there). If you do this, then you don't need to edit /etc/resolv.conf file, as NetworkManager will do it for you when you renew your DHCP lease.