Setting up OS X Yosemite for web development with Apache & Dnsmasq
15 Apr 2015I had a couple of issues getting this properly configured, so I thought I'd note down what worked for me. Credit for the content in this post mostly goes to Chris Mallinson and Thomas Sutton.
EDIT: I have changed the TLD used below from .dev to .localhost after receiving (rightfully) a recommendation from a friend to avoid real TLD's; I suggest anyone else does the same. Thanks Ben! For more background, take a look at Don't use .dev for development by Danny Wahl.
You'll need to download and install XCode and launch it at least once. Accept the licence agreement.
Now, edit /usr/local/etc/dnsmasq.conf
to tell dnqmasq to answer any request for a .localhost
domain with localhost:
# Add domains which you want to force to an IP address here.
# The example below send any host in double-click.net to a local
# web-server.
#address=/double-click.net/127.0.0.1
address=/localhost/127.0.0.1
Restart dnsmasq to ensure it picks up the changes, then request a .localhost
domain with dig
to check you get the answer you want:
Now we need to configure OS X to use the local dnsmasq server. Create a file for each TLD you want to use under /etc/resolver
as follows:
Enter nameserver 127.0.0.1
then save/close the file. This tells OS X to use 127.0.0.1
as the nameserver for all domains under that TLD, instead of searching the public servers for it. Now test that it works:
Now we need to configure Apache to serve up domains we want to use for development. The instructions below are for OS X 10.7 onwards.
Start apache and check that it works with sudo apachectl start
. If you don't get any errors, browse to localhost. You should see an "It works!" screen.
Open /private/etc/apache2/httpd.conf
and continue.
- Activate
mod_vhost_alias
around line 160:#LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
- On or around line 169, uncomment
#LoadModule php5_module libexec/apache2/libphp5.so
if you want to use PHP. - Comment out (using #) or reconfigure the following section around line 220 to allow apache access to the filesystem folder you want to store sites in:
<Directory />
AllowOverride none
Require all denied
</Directory>
And finally...
- Around line 500, enable vhosts:
#Include /private/etc/apache2/extra/httpd-vhosts.conf
. - Open
\private\etc\apache2\extra\httpd-vhosts.conf
and enter the following, replacing/Users/adam/Sites
with your desired root folder:
<Directory "/Users/adam/Sites">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
# You might be able to remove wwwroot for .../Sites/<sitename>.
<Virtualhost *:80>
VirtualDocumentRoot "/Users/adam/Sites/%1/wwwroot"
ServerName sites.localhost
ServerAlias *.localhost
UseCanonicalName Off
</Virtualhost>
Now you should be able to visit yoursite.localhost in a browser, with the files stored in e.g. /Users/adam/Sites/yoursite/wwwroot.