I recently came across a blog post by David Coallier which detailed a cool way to deal with local development with Apache. You can read his blog post at Wildcard /etc/hosts, an alternative, this is something I have been after for a very long time so decided to try it for myself.
I don’t use homebrew but instead I use macports so here is how I managed to achieve the same thing.
First things first make sure you have Macports installed and working.
Then lets install dnsmasq using Macports so open up a terminal shell and type
sudo port install dnsmasq
This will download and install dnsmasq to /opt/local/sbin
Next up you need to edit the dnsmasq.conf file to setup your local DNS entries. To do this type
sudo vim /opt/local/etc/dnsmasq.conf
I then added the following 2 lines to the configuration file
After saving the file I then needed to make sure that dnsmasq was started automatically when my macbook was restarted. I did this by typing
sudo port load dnsmasq
Next up I needed to add the new DNS server to my network configuration so that it would be used when trying to resolve domain names. I did this by going to
System Preferences -> Network -> Selected My Network Interface -> Advanced -> DNS
By clicking the + symbol and adding 127.0.0.1 to the list of DNS servers I was now all set.
I tested this was working by going back to the terminal window and first off tried an external domain to make sure I hadn’t screwed my DNS up
PING google.com (184.108.40.206): 56 data bytes
And then trying my new local addresses
PING mark.com.local (127.0.0.1): 56 data bytes
I was now all set. Next up to configure MAMP so that I didn’t need to setup a virtual host for every site I was working on.
I did this by editing /Applications/MAMP/conf/apache/httpd.conf and added the following lines to the end of the file
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
After a quick restart of MAMP I was now able to use any .com.local domain name and it would map correctly to a folder inside /Applications/MAMP/htdocs/
To test this I created a new folder inside /Applications/MAMP/htdocs/ called testsite
And added a simple index.php file which just did an echo “Hello World”
Then booting up a browser I went to http://testsite.com.local and voila I was treated to the simple words “Hello World”. My life was now complete, no more faffing around with vhosts and /etc/hosts!
Thanks to David for helping me realise just how easy it could be to set all this stuff up.