Skip to content


Setting up a virtual host on a Mac running MAMP

Update: I just found out about some great software that makes setting up virtual hosts with MAMP a breeze. You’d better check out this tutorial before reading mine, then go download VirtualHostX for only $9 dollars.

Or, if you want to do it for free, read on…

The super-easy, “How to set up name-based virtual hosts on a Mac, running MAMP”, Tutorial

When I’m developing a new website for a client, I like being able to open my browser and test out the site I’m working on. So lately I’ve been uploading my “work in progress” to a new sub-domain that only I know, such as http://stage.myClient.com or http://alpha.myClient.com. The problem with this approach is that even though I’m the only one who “knows” what the address is, people can still find, and view my unfinished business since it’s publically accessable. What I really want to do is test my site, in my browser, just as if it was on the web, without anyone being able to see it, but me.

There’s a really easy way to do this. It’s called Virtual Hosting. Name-based virtual hosting will allow us to associate http://stage.myClient.com with a directory on our computer, rather than going out to the internet. The great thing about this is that we can name our virtual host whatever we want. We could call it http://stage.myClient.com or http://local.myClients.clientOne. We could even name it http://foobar if we felt so inclined. http://localhost is an example of a virtual host. Go ahead and type that into your address bar. I’m not sure what you’ll get back, but it’s probably already set up and redirecting to a folder on your computer somewhere.

If you’ve already downloaded and installed MAMP, good for you! If you haven’t, you really should. If you’re developing websites in PHP, and you want to test your site without having to upload it to your server, you need to install PHP on your computer. The same goes with using a database. Setting these up individually can be a pain! MAMP is nice because it installs everything you need, you can turn it on and off, and it makes it really easy to start developing websites locally without having to set anything up. This tutorial is about setting up a virtual host, on a Mac, running MAMP, so I’m going to assume you’ve already got MAMP set up.

So one of the things you might have noticed about MAMP is that it sets its “localhost” directory to /Applications/MAMP/htdocs…which is fine, except I like to keep my sites in my /Users/andrew/Sites directory. I mean…that’s what it’s there for.

So if you want to set yours up that way…

  1. Start MAMP.
  2. Click “Preferences…”
  3. Click the “Apache” tab
  4. Browse to the directory of your choice

Now going to http://localhost should resolve to the directory that you chose. Now, let’s set up our very own virtual host!

  1. Open Terminal by going to /Applications/Utilities/Terminal
  2. Type: sudo pico /etc/hosts
  3. Type your Administrator password
  4. Edit your hosts file to look like mine, with exception to your chosen virtual host name. I’ve named my virtual host stage.myClient.com
  5. Press Control+O, then Return to save the changes
  6. Press Control+X to exit Pico

One last step:

  1. Open /Applications/MAMP/conf/apache/httpd.conf (TextEdit is fine)
  2. Scroll to the very bottom, this is where we declare our virtual host.
  3. Edit your httpd.conf file to look like mine. The server name should be the name of your virtual host that you declared in the previous file, and your document root should be the directory you want your virtual host to pointing to.

We’re done! Make sure you have MAMP running when you try it out for yourself.

If you found this tutorial helpful, or if you want to tell me how much it sucks to make it even better, please leave a comment!

Thanks, for reading!

–Andrew

Posted in Tutorials.

Tagged with , , , , , , , , .


3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. dave says

    Thanks, i was looking for this exact thing, It worked great for me.

    You’re the third hit on google for when searching for
    MAMP localhost directory

    Also the tooltip rollover on the input filter help here in the comment entry is a nice touch.

  2. andrew says

    I’m glad I could help. =]

    Thanks for letting me know I was easy to find!

  3. anler says

    great tut!!! :)



Some HTML is OK

or, reply to this post via trackback.