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.
Copy, paste and edit the example and :
CustomLog "logs/dummy-host.example.com-access_log" common
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
Edit host file in Mac OS X.
This blog post was useful.
- In terminal, edit
- to do so type
$ sudo nano /private/etc/hosts(you will be asked for your mac password as it is sudo)
- add your new virtual hostname after localhost on the
- Save and exit by doing control-o, control-x
- then in terminal
$ dscacheutil -flushcache
- Stop and start again the servewr in MAMP
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 dummy-host.example.com 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)
- Start -> All Programs -> Accessories
- Right click on Notepad and select “Run as Administrator”
- File -> Open
- Local Disk (C:) -> Windows -> System32 -> drivers -> etc
- Make sure you have “All Files” selected
- Select hosts
- 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.