Virtual hosts with MAMP on Leopard

Assumptions (Kind of major assumptions):

  • You are running Leopard (and therefore cannot use “Netinfo Manager” to edit your hosts).
  • You already have MAMP up and running and MAMP is using the Apache config file located here: /Applications/MAMP/conf/appache/httpd.conf
  1. Set up a directory for your dev website. I created: /Applications/MAMP/htdocs/csimmons
  2. Create a “host” for your site. In Leopard this will require you to edit the hosts file manually. Be sure to use sudo or you will not be able to save the file. Type the following and enter the root password when prompted:
    $ sudo pico /etc/hosts
    Add the following at the bottom

    # VIRTUAL HOST START
    127.0.0.1 csimmons.dev
    # VIRTUAL HOST STOP

    CTRL+O to write the file (then hit ENTER)
    CTRL+X to exit pico
  3. Flush the DNS cache
    $ dscacheutil -flushcache
  4. Edit the Apache config file:
    $ pico /Applications/MAMP/conf/apache/httpd.confChange the following:
    OLD:
    ServerName localhost:8888
    NEW:
    ServerName 127.0.0.1:8888

    Continue to the very bottom of the file (use CTRL+V to page down faster) and you will find “Section 3: Virtual Hosts”. Add the following at the very end:

    NameVirtualHost 127.0.0.1
    <virtualhost 127.0.0.1>
    DocumentRoot /Applications/MAMP/htdocs
    ServerName localhost
    </virtualhost>
    # DEVELOPMENT HOSTS START
    <virtualhost 127.0.0.1>
    DocumentRoot /Users/username/Sites/csimmons
    ServerName csimmons.dev
    </virtualhost>
    # DEVELOPMENT HOSTS STOP

    CTRL+O to write the file (then hit ENTER)
    CTRL+X to exit pico

  5. Restart the MAMP servers. I just click “Stop Servers” and then “Start Servers” on the MAMP widget.
  6. Try it out. Point your browser to the following (no www in the address and don’t forget the port[8888]): http://csimmons.dev:8888
  7. Rinse. Repeat for each of your dev sites. Multiple sites would look like this:

    hosts file:

    # VIRTUAL HOST START
    127.0.0.1 csimmons.dev
    127.0.0.1 site2.dev
    # VIRTUAL HOST STOP

    Apache config file:

    # DEVELOPMENT HOSTS START
    <virtualhost 127.0.0.1>
    DocumentRoot /Users/username/Sites/site2
    ServerName csimmons.dev
    </virtualhost>
    <virtualhost 127.0.0.1>
    DocumentRoot /Users/username/Sites/site2
    ServerName site2.dev
    </virtualhost>
    # DEVELOPMENT HOSTS STOP