When you sit down and start a new project, often times, you'll want to set up a virtual host,
a spoof domain that will emulate a live environment. The effort is usually two parts: set up a
domain in /etc/hosts
, and then add a <VirtualHost>
entry in /Applications/MAMP/conf/apache/httpd.conf
to make your files accessible.
This tutorial is setting up a 'zero-configuration' development environment. We'll be setting up a
spoof domain ending in .dev
. In MAMP, we'll create a folder called dev
. Any folder inside,
the name will become the domain.
-
####Install homebrew
Follow the instructions available on the homepage or just copy and paste this snippet into terminal:$ /usr/bin/ruby -e "$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
-
####Install dnsmasq
$ brew install dnsmasq
-
####Setup dnsmasq
$ mkdir -pv $(brew --prefix)/etc/ $ echo 'address=/.dev/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf $ sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons $ sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist $ sudo mkdir -v /etc/resolver $ sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'
-
####Verify new nameserver
Run$ scutil --dns
to show all of your current resolvers, and you should see that all requests for a domain ending in.dev
will go to the DNS server at127.0.0.1
:resolver #9 domain : dev nameserver[0] : 127.0.0.1
If you ping any domain that ends in
.dev
, you'll get the IP address127.0.0.1
back as a result:$ ping -c 1 thereisnowaythisisarealdomain.dev PING thereisnowaythisisarealdomain.dev (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.057 ms
Note: you may need to reboot before the
/etc/resolver
settings take effect globally. -
Open up
/Applications/MAMP/conf/apache/httpd.conf
in a text editor, scroll down, and add the following lines to the file.NameVirtualHost * <VirtualHost *> # Apache will form URLs using the hostname supplied by the client UseCanonicalName Off # available aliases to use ServerAlias *.dev VirtualDocumentRoot /Applications/MAMP/htdocs/%2/%1 </VirtualHost>
-
For every additional domain you want to server locally, add the following entry to
/usr/local/etc/dnsmasq.conf
:address=/.domain/127.0.0.1
Now let your OS know that you want to redirect requests to this domain to your local dnsmasq nameserver. Do this by creating a file
domain
in/etc/resolver
./etc/resolver/domain
has the following content:nameserver 127.0.0.1
The Apache configuration will also need to be modified. In the recent
<VirtualHost>
entry, updateServerAlias
with the new domainServerAlias *.dev *.domain
Note: you may need to reboot before the
/etc/resolver
settings take effect globally. -
$ mkdir -p /Applications/MAMP/htdocs/dev/example $ echo "example.dev" > /Applications/MAMP/htdocs/dev/example/index.html
Now visit http://example.dev/ in your web browser. Hopefully you should see
example.dev
– if so congratulations; you’re going to save so much time in the future now. -
Because apache's VirtualDocumentRoot does not set
DOCUMENT_ROOT
correctly (its a bug thats been discussed for long time), you need to do the following in your PHP code if you useDOCUMENT_ROOT
:Add the following line below the
VirtualDocumentRoot
vhost definition:$ php_admin_value auto_prepend_file /Applications/MAMP/htdocs/setdocroot.php
Then, create the referenced PHP file and add two lines:
<?php $_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'],"",$_SERVER['SCRIPT_FILENAME']);
Now, every page load has this file executed, which properly sets
DOCUMENT_ROOT
. -
So you’re developing an awesome site locally at a custom domain, like
customerxyz.dev
. So how can you access or share your site across a network, including mobile and tablets?The best trick I learned with MAMP is using XIP.io. What
XIP.io
does is basically forward the domain with the IP and then it directs the device properly to the site you want on your machine.For example, say your machine’s IP is
192.168.0.15
– and the domaincustomerx.dev
– you would whip out your smart phone and entercustomerx.192.168.0.15.xip.io
The Apache configuration will again need to be modified. In the recent
<VirtualHost>
entry, add a newServerAlias
with thexip.io
extension. To help with working on a network with a dynamic IP or working on multiple networks (work vs home), we'll replace the IP with a wildcard '*'.ServerAlias *.xip.io