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:
In your friends hosts file they will need:
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.