Now is time to breathe a sigh of relief — at least if you are on OSX. In this short blog post I’ll show you how to setup a local nameserver using dnsmasq, setup a wildcard Virtual Host in Apache 2.0.x and be running in minutes without a million entries in your /etc/hosts file.
So you are a developer and not unlike the author — me —, you have about a quinzillion hostname lines in your /etc/hosts file with different local hostnames because you can’t find a decent way to set wildcard-domains from within /etc/hosts. Well the solution isn’t as simple as adding a *.domain.com.local but if you follow the following instructions closely, you’ll be able to achieve similar results.
Before we start, you will require a few things in order to be able to do this. The most important thing you’ll need — and merely the only one — is homebrew. If you don’t know about homebrew yet because you have somehow been living under a huge rock for the past 700 years, you should go read and get it here.
If you made it this far, it means you have brew setup and usable on your system. If not, I take no responsibility if things break — either way I won’t take responsibility.
First thing first, install dnsmasq using brew:
brew install dnsmasq
Dnsmasq is a lightweight and easy to configure DNS server. This is what your computer is going to query when invoking wildcarded domains.
Secondly, you need to make sure that your Apache server is configured to serve Virtual Hosts. What follows will assume you can server name-based vhosts so make sure to read this or die — I really mean the rest of the tutorial will be pointless if you don’t read it.
Now that you’ve installed dnsmasq, the next thing you have to do is copy the example dnsmasq configuration file over to the default dnsmasq configuration file location.
cp /usr/local/Cellar/dnsmasq/2.55/dnsmasq.conf.example \ /usr/local/etc/dnsmasq.conf
Whilst this is relatively painless, the next step is excruciating. You have to edit the /usr/local/etc/dnsmasq.conf file and assuming you want to transparently respond to all *.com.local hostame requests, you have to edit the aforementioned configuration to contain:
Save that file — :wq! — and start dnsmasq using /usr/local/sbin/dnsmasq. Dnsmasq will use the configuration file located in /usr/local/etc/dnsmasq.conf by default.
You theoretically successfully configured dnsmasq and now your system is nearly ready to homogeneously serve your “local” domains without requiring you to edit the /etc/hosts each time you add a new virtual host.
All you have to do from now on is make sure the set the ServerName value of your virtual hosts to end in .com.local as such as:
<VirtualHost *:80> ServerName domain.com.local # So I hear you want whitelabel subdomains? #ServerAlias *.domain.com.local DirectoryIndex index.php ServerAdmin email@example.com DocumentRoot /path/to/index.html </VirtualHost>
Test your apache configuration — apache2ctl -t — and restart it with apache2ctl restart. Your subdomain will now be ready to be invoked. Keep reading, the last step is crucial.
As this tutorial is for OS X users, I’m not going to go deeply into how to change the nameserver on UNIX or Windows platforms, but I believe on UNIX, one would mainly change the /etc/resolv.conf and add nameserver 127.0.0.1.
On OS X, you have to go to your System Preferences, then click on Network, then for the active interface, click on Advanced, click on DNS and finally click on the + symbol at the bottom of the left hand-side panel and add 127.0.0.1 to your list of DNS Servers.
You have dnsmasq installed and launched, you created a name-based subdomain and restarted your apache server, added a DNS Server to your list of nameservers and had a sip of strong liquor. You should now be able to access any virtual-host that end with .com.local. Good luck!
Here’s a quick recap of the steps required.