Setting up virtual host on MAMP on Mac OS X Mountain Lion

Learning new things everyday. I use MAMP (Mac Apache, MySql, PHP) for my local developmemt, and love it. But doing AJAX call, I encountered a problem. There are ways to get around it by aceess it via php, or force abling it in chrome or this (I don’t know if I trust myself with it) but my colleague helped me set up a vhost on my mac for development purpose (as it only works on my machine alone). Nice. (P.S setting up vhost did not fix my problem. But still cool.)

It was pretty simple. Here are the steps.

Edit /Appliations/MAMP/conf/apache/extra/httpd-vhosts.conf

Copy, paste and edit the example and :

    ServerAdmin [email protected]
    DocumentRoot "/Applications/MAMP/Library/docs/"
    ErrorLog "logs/"
    CustomLog "logs/" common

Edit /Applications/MAMP/conf/apache/httpd.conf

Uncomment (Remove # from in front of “Inclde”) this line. On my version it is line 525. After removing the #, it should look like this:

# Virtual hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

Edit host file in Mac OS X.

This blog post was useful.

  1. In terminal, edit /private/etc/hosts
  2. to do so type $ sudo nano /private/etc/hosts (you will be asked for your mac password as it is sudo)
  3. add your new virtual hostname after localhost on the localhost
  4. Save and exit by doing control-o, control-x
  5. then in terminal $ dscacheutil -flushcache
  6. Stop and start again the servewr in MAMP

That’s it.

Except one thing. By default, MAMP use port 8888 (for example it is http://localhost:8888)

We want to change it to apache default port (80) and while I am at it, default mysql port (3306) from MAMP’s default which is 8889.

To do this, you only need to open MAMP, click on “Preferences…” select “Ports” tab, and click on “Set to default Apache and MySQL ports.” That is it! (This blog post was very useful.)

Now your DocumentRoot defined in the httpd-vhosts.conf should show up when you type in the domain you specified in your browser from your machine.

Awesome! No more localhost.

(UPDATED: June 19, 2013)
Additionally, I use Parallels desktop to run Windows on my mac. Now I need to test IE8. What if I want my to resolve from there? I just need to edit “hosts” file on Windows to resolve that domain to IP of my Mac. (Go to System Preference -> Network, and note/copy your IP Address – XXX.XXX.XXX.XXX)

  1. Start -> All Programs -> Accessories
  2. Right click on Notepad and select “Run as Administrator”
  3. File -> Open
  4. Local Disk (C:) -> Windows -> System32 -> drivers -> etc
  5. Make sure you have “All Files” selected
  6. Select hosts
  7. Add at the end of the file:


UPDATE: Keep finding much better write up on this topic:
How to set up virtual hosts in the MAMP environment by Danilo Stern-Sapad.
Usable MAMP on OS X 10.8 Mountain Lion by Alan Ivey.
This one does Dynamic Mass vhosts.

I wanted to access the files from outside of my machine, and I was editing the vhost.conf but I screwed up. For some strange reasons, when I started this machine today, MAMP couldn’t start the web server. Turns out apache was already running, as I found out when I run the command: ps aux | grep "http". I saw this
_www 137 0.0 0.0 2436172 672 ?? S 11:31AM 0:00.00 /usr/sbin/httpd -D FOREGROUND
root 20 0.0 0.0 2436172 4128 ?? Ss 11:30AM 0:00.29 /usr/sbin/httpd -D FOREGROUND

Huh? ANd it was showing the contents from /Library/WebServer/Documents. Which is not what I wanted. So I had to run command sudo apachectl stop to stop the default apache (this post was very useful.), then MAMP apache was able to start. I need to investigate… Mmmm.

One Response to “Setting up virtual host on MAMP on Mac OS X Mountain Lion”

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>