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
sudo aptitude install dnsmasq
Edit your Apache vhost file and add the wildcard virtual host
# The default server name is for the sites/mojahmedia.com Drupal installation
# Path to the drupal root
Options Indexes FollowSymLinks MultiViews
allow from all
Now, edit the dnsmasq config file
$sudo vim /etc/dnsmasq.conf
And add these lines:
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;
sudo vim /etc/resolv.conf
#Add as the first line:
# Generated by NetworkManager
nameserver 127.0.0.1 # <<< add this line
nameserver 188.8.131.52 # this could be different in your file
nameserver 184.108.40.206 # 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:
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!