Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SethHorsley/ec9b49d6ba375c45f3557da2818450fa to your computer and use it in GitHub Desktop.
Save SethHorsley/ec9b49d6ba375c45f3557da2818450fa to your computer and use it in GitHub Desktop.
Simplify Local Development with MAMP & DNSMASQ (& XIP.io)

Simplify Local Development with MAMP & DNSMASQ

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.

Instructions

  1. ####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)"
  2. ####Install dnsmasq

    $ brew install dnsmasq
  3. ####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'
  4. ####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 at 127.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 address 127.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.

  5. Add Dynamic Virtual Hosts to your Apache configuration

    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>
  6. Add additional domains

    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

    More info about resolver

    The Apache configuration will also need to be modified. In the recent <VirtualHost> entry, update ServerAlias with the new domain

    ServerAlias *.dev *.domain

    Note: you may need to reboot before the /etc/resolver settings take effect globally.

  7. Testing it all works

    $ 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.

  8. Apache Bug

    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 use DOCUMENT_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.

  9. Accessing MAMP sites from local devices, phones, tablets.

    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 domain customerx.dev – you would whip out your smart phone and enter customerx.192.168.0.15.xip.io

    The Apache configuration will again need to be modified. In the recent <VirtualHost> entry, add a new ServerAlias with the xip.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment