How to use Apache Virtual Hosts over a local network with MAMP

I was asked recently whether it was possible to use the Virtual hosting in Apache I describe for Magento on MAMP over a network rather than just locally. To make a sensible response I wanted to give a quick background to the HTTP requests going on and how Apache uses those requests to decide what HTTP response to send back. It turned into a bit of a long reply comment so here it is as a post instead.

I use a network Apache virtual hosts setup all the time (though the network isn’t a local network it’s the internet, but same principle).

Here’s a quick little summary to clarify. Basically when your browser requests www.example.com you OS goes to try and find an IP for that address. It’ll check in your hosts file and get the IP address, then it’ll connect and the HTTP request will be sent to the server. If it can’t find the server in your hosts file, it’ll ask the DNS server, the rest is history of course. I digress, this post is more about the hosts file configuration/scenario than the DNS one. If you wanted you could set up a local DNS relay server, and remove the need for the modified hosts file at all – a story for another day!

The important part is that the HTTP request contains the original hostname (www.example.com). If you have an Apache running without virtual hosting it will just send back and HTTP response without regard for the hostname, and in fact if you have a default virtual host, it will respond when no hostname matches anyway.

When you configure Apache virtual hosts you are basically telling Apache to send back different responses based on the requested hostname in the HTTP request from the client.

SO, why did I bore you with this? Hopefully to make it easy to understand for the network setup. Provided your friend can connect to your HTTP server running on MAMP (via your IP address) and is not blocked by any firewalls it will work perfectly. The HTTP request must contain a hostname that matches your Apache running on MAMP, and you’ll be in business.

So say your macbook IP address is 192.168.1.100 and your development Magento store is called ‘www.devstore.com’. In your hosts file (/etc/hosts) you would have something like:

localhost	www.devstore.com

In your friends hosts file they will need:

192.168.1.100	www.devstore.com

This tells your machine to connect to localhost when making requests to www.devstore.com and tells your friends machine to connect to 192.168.1.100. Both will cause the HTTP request sent to Apache to be for ‘www.devstore.com’ which would need to be a configured virtual host (or the only/default host) in your Apache.

I hope this clarifies it all a little, it seemed there was some confusion about the role the hosts file played in this MAMP virtual host setup. The hosts file should not contain any URL’s, it only maps hostnames to IP addresses.

You might also be interested in:

  1. How to set up Apache Virtual hosting on MAMP for Magento
  2. How to install MAMP on your Leopard Mac
  3. Fixing Magento Login Problem after a Fresh Installation
  4. How to use the MAMP Mysql command line client in a terminal
  5. A simple way to password protect your Magento store


Tagged as , , , , + Categorized as Apache, MAMP, Mac, Magento

2 Comments

  1. Interesting post!

    What would I do to connect to a folder on the remote machine, where my virtual host is set up? Can I do that?

    Thanks

  2. Hi, You can.
    I think this post on using Magento, MAMP and virtual hosts over a network is what you need? Is that what you were trying to do?

Leave a Comment

Name:

Email:

Website:

Sporadic Tweeting...

  • New Post: Why I don’t use Flash, ever (and neither should you NZ Herald): Seriously, just don’t do it – I think I’... http://bit.ly/c9p3p9 1 week ago
  • New Post: A new ASchroder.com for 2010 or, why I haven’t written anything for a couple of weeks…: Tonight I releas... http://bit.ly/91NeAk 1 week ago
  • Don't skip the last session. One of the best is last at #irwd Stephan Spencer was insightful last year. Highly recommended. 3 weeks ago
  • More updates...

What I'm listening to

  • The xx - xx
  • Mos Def - The Ecstatic
  • Phoenix - Wolfgang Amadeus Phoenix
  • Jane's Addiction - Nothing's Shocking
  • Florence + The Machine - Lungs
  • Vampire Weekend - Contra
  • The Temper Trap - Conditions
  • The White Stripes - De Stijl
  • Grizzly Bear - Veckatimest
  • Passion Pit - Manners
  • Everclear - Sparkle And Fade
  • The White Stripes - White Blood Cells