Note the following applies to Windows Vista, but is probably easier on MacOS/Linux.
Is your hosts
file becoming monstrous? Do you have an alias or shortcut to your hosts
file because you edit it so often? Tired of manually adding every subdomain and domain you work on?
I was too when I thought there must be a better way. And there was.
The general idea is this: by installing a local DNS nameserver in BIND, we can set up local development domains that look like regular domains on the internet. For real domains, we’ll just forward the requests on to a real nameserver. This gives us a couple more benefits: 1) we can use the local nameserver as a caching nameserver to speed up DNS queries (in theory, I have not actually done this), and 2) we can choose to use any DNS service we wish, i.e. OpenDNS, or Google DNS.
Here are the steps.
- Follow these instructions on installing and configuring BIND and configuring a zone for your local domain.
- I installed BIND to
C:\Windows\system32\dns
. - Here is my
named.conf
in its entirety.options { directory ";c:\windows\system32\dns\zones"; allow-transfer { none; }; forward only; forwarders { //208.67.222.222; // OpenDNS //208.67.220.220; 8.8.8.8; // Google DNS 8.8.4.4; }; query-source address * port 53; }; /* logging { channel queries_log { file "c:\windows\system32\dns\var\queries.log"; print-severity yes; print-time yes; }; category queries { queries_log ; }; }; */ zone "work.local" IN { type master; file "work.local.txt"; }; key "rndc-key" { algorithm hmac-md5; secret "xxxxxxxxxxxxxxxxxxxxxxxx"; }; controls { inet 127.0.0.1 port 953 allow { 127.0.0.1; } keys { "rndc-key"; }; };
- I created a zone file for my development domain work.local following this zone file example. Here is the zone file in its entirety. Note the
CNAME
wildcard record.$TTL 86400 @ IN SOA ns1.work.local. admin.work.local. ( 2008102403 10800 3600 604800 86400 ) @ NS ns1.work.local. IN A 127.0.0.1 ns1 IN A 127.0.0.1 www IN A 127.0.0.1 * IN CNAME www
- I installed BIND to
- Start or restart the BIND service.
- Configure you network connection to use 127.0.0.1 as your primary nameserver, instead of DHCP. My IPv4 properties look like this:
- Flush the Windows DNS cache by running:
C:\> ipconfig /flushdns
- Test BIND by pinging
www.work.local
. If you have errors, you can uncomment the logging block innamed.conf
. - Once that is working, create a
VirtualHost
in Apache for your development domain. Thanks toVirtualDocumentRoot
, we can map any number of subdomains to project roots. Here is myVirtualHost
block.<VirtualHost 127.0.0.1> ServerName www.work.local ServerAlias *.work.local VirtualDocumentRoot "C:/_work/%1" <Directory C:/_work> Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
- Start or restart Apache.
- Create a directory in
C:\_work
, for example,C:\_work\awesomeapp
. Create a test index.html file in that directory. - You should now be able to go to http://awesomeapp.work.local in your browser and see your index.html file!
Now, you should be able to repeat step 8 for any new website you create! No editing of hosts
files, no bouncing the webserver! Just create the project directory and it’s immediately available.
One other important note: Firefox has its own DNS cache independent of the OS. For sanity, restarting Firefox resets its DNS cache. You can also permanently disable DNS caching in Firefox.
2 Comments to 'Throw Away Your Hosts File! Developing Locally With BIND'
August 25, 2014
Thanks for the info. Other sites are too abstract rather than goal-oriented. Step 3) particularly seems to be neglected in most tutorials and had me confused how bind will get invoked. While I use a Mac and Ubuntu rather than Windows, like you say it should be easier with a *nix system.
April 27, 2015
this has been very helpful, probably one of the most helpful, in setting up bind, for local development work on the lan. Thanks much! Brian