I’ve spent the last two days setting up virtual hosts with MAMP on two different Macs, one Tiger and one Leopard. I’m posting this so the next person doesn’t have to spend two days at it.
Setting up virtual hosts is really pretty straightforward, once you find a good set of instructions. (I had done this before… about three years ago. I’ve slept since then.) Dave Shea to the rescue. However, after I’d edited httpd.conf, Apache wouldn’t start. After long hours of trial, error, and searching the MAMP forum (where this problem had been often reported but never resolved), I learned that it was happening because (like any fool Mac user) I’d opened it up in TextEdit.
Don’t use TextEdit on your .conf files. It no workee. I’m guessing the problem has to do with Mac-specific line breaks, because when I edited them in Dreamweaver, which I’ve set to use UNIX-style line breaks, everything worked perfectly. I’ve seen other people report success with TextWrangler and TextMate. Of course, nano and pico work well too.
So, those evolt instructions Dave linked to were all fine and good in Tiger, but in Leopard, the NetInfo Manager application is gone. You therefore have two choices: you can edit /etc/hosts by hand (pico or nano again!), or you can use VirtualHostX. I don’t mind editing the hosts file in Terminal, but I generally prefer a nice GUI if I can get it, so I tried VirtualHostX to define my sites. Then I couldn’t figure out why I was seeing the default Apache page from /Library/WebServer/Documents rather than the servers I’d defined in httpd.conf, which used various things in my ~/Sites directory as their document roots.
Here’s the problem: VirtualHostX tries to be a little too helpful. Since it assumes that you’re using the built-in Apache rather than MAMP, it fires up Personal Web Sharing under System Prefs. Personal Web Sharing trumps MAMP, so to speak, and what you’re seeing is the other Apache installation on your system. Turn off Personal Web Sharing and everything’s fine again.
Simple, right?
Two systems. Two problems. Two days. Don’t let this happen to you.
Addendum: Lots of people have recommended Headdress for dealing with Apache config junk. When I tried it on the Tiger machine, Apache refused to start. Same problem I had with TextEdit? I dunno. I might try it again on the Leopard machine… after I back everything up. Several times.
Addendum #2: Tried Headdress in Leopard. No problems with Apache this time, but Headdress insists on using port numbers rather than server names (or aliases) to distinguish hosts — e.g. localhost:9000 and localhost:9001, rather than mysite and myothersite — because it’s not touching /etc/hosts; it’s just editing httpd.conf. It is smart enough to distinguish between MAMP and the built-in Apache, and it allows you to maintain both. Still, the port thing is bollocks. I also don’t like the fact that the details about Headdress’s operations are in its Help file, which you won’t see until you’ve installed it, rather than on the website. Oh well. Nano and I get along just fine.
For my next trick, I’m going to reinstall Windows under Parallels and get the virtual hosts working for IE testing.
matt says
Can’t I use textmate to edit all necessary files?
This is driving me crazy. I moved over to leopard from tiger, and I upgraded my MAMP. The servers seem to be running, they just aren’t connecting with my hosts.
Could you maybe post examples of your hosts, and config files?
matt says
Sorry, I meant from tiger to leopard.
Stephanie says
Hi, Matt. Here’s my hosts file after the commented stuff:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
# VIRTUAL HOST START
127.0.0.1 sillybean wordpress
# VIRTUAL HOST STOP
Here’s what the end of httpd.conf looks like:
#
# Use name-based virtual hosting.
#
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot /Users/stephanieleary/Sites/
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName wordpress
DocumentRoot /Users/stephanieleary/Sites/wordpress
</VirtualHost>
Many examples I saw used * instead of 127.0.0.1, but it wouldn’t work for me, and I have no idea why. If you have one, try the other.
Stephanie says
If you got a mangled version by email… sorry about that. Textile is persnickety about code. I’ve fixed it here.
I also left out the entry for sillybean, but it looks exactly like the others.
matt says
Thanks Stephanie. I’ll play around with it later. I have posted several times at the MAMP forum but it’s a lonely place in there.
Stephanie says
Tell me about it! I’ve had at least a dozen problems with it, and solved eleven of them myself (with attendant Googling, facepalming, and headdesking).
Stephanie says
Matt, did you ever get your problem sorted out? I’m now running into the same situation on another Leopard machine.
Matt says
I haven’t had time to play around yet. I’m going to try to pull aside one of our lead developers at work and fix it. He’ll take care of it I’m sure. I’ll share my results.
Stephanie says
I finally got mine working. I had to place each named host on a separate line in /etc/hosts rather than having them all on one. Now it looks like:
notextile..
# VIRTUAL HOST START
127.0.0.1 sillybean
127.0.0.1 wordpress
# VIRTUAL HOST STOP
I had to use
dscacheutil -flushcache
in Terminal to flush the DNS cache, but then my virtual hosts started right up. I made some changes to httpd.conf while mucking around… among other things, I’d found a reference that the DocumentRoot paths had to be enclosed in quotes in Leopard. Not sure if that actually did anything, though. (Now that it’s working, I’m not touching it.)Matt says
Interesting. It certainly is cleaner that way. By the way. I the developer sat down at my machine, popped open terminal, and within about a minute had my problem fixed.
The solution: I had the virtual host section of the conf file commented out. It’s the little things that get you.
Marcus Mucha says
I’m on Leopard and am trying to get multiple sites up with MAMP, but I can’t seem to get this working. I was curious what your Port settings are within MAMP?
Also, which .conf file are you modifying? the one at “/Applications/MAMP/conf/apache/httpd.conf” ?
ORin the case of Leopard “/private/etc/apache2/extra/httpd-vhosts.conf” ?I have all my sites set up in a sub-directory under “Documents”. I’m wondering if it is critical that I have them under “Sites” instead?
Any tips or help would be most appreciated!
Matt says
Don’t keep your directories in Documents. Keep them within the mamp folder.
Edit the /Applications/MAMP/conf/apache/httpd.conf file.
Keep the ports set to default. Apache: 8888, MySql: 8889
Stephanie says
Marcus: it doesn’t matter much where you have the files stored, as long as your httpd.conf file (the one Matt specified) references them correctly.
I use the Apache & MySQL defaults in MAMP, but Leopard requires an admin password every time you start Apache on port 80, so that’s an annoyance you have to live with if you choose that port. (I think Tiger did the same thing, in fact.)
Andy says
Hi everybody,
I just have a question (excuse me for my bad english!). Is it possible to create a virtual host for the apache start page? So you can type just ‘mamp’ instead of ‘http://localhost:8888/mamp/’?
If yes, how can I do it?
Thank you so much!
Stephanie says
Hi, Andy. You can. Just add this to the end of httpd.conf (use localhost instead of 127.0.0.1 if that’s what you’ve done elsewhere):
@
@
ServerName mamp
DocumentRoot /Applications/MAMP/bin/mamp
And then add “mamp” in /etc/hosts, as in comment #9.
However, you’d have to type “mamp:8888″ unless you change the MAMP preferences so that Apache uses its default port, 80, instead of the MAMP default.
Andy says
Thank you so much dear Stephanie!
Now it works perfectly! =)
Have a great weekend!
Cheers from Switzerland ;D
Stephanie says
Really glad your questions made me post bits of my /etc/hosts file here, because I just discovered that Archive & Install does not preserve that file. ARGH.
Daniel says
If any of you have had problems migrating from Headdress to VirtualHostX, I’ve detailed my steps on how I got it working here:
http://www.hyperarts.com/blog/moving-from-headdress-to-virtualhostx/
Gerald says
Greetings. 2017 and I am having issues with this. Not sure if anyone is still watching or helping on this topic, but I am at my wits end, as I have read You, @Stephanie was way back when.
Hopefully there are still people able to help as everything I read, I do, it does nothing. I know some other setting is off.
– gerald