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

April 14, 2009

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.

Ashley

Posts

Hi! I'm Ashley Schroder, a Software Engineer from New Zealand - this is a collection of notes on my experiences with Ecommerce Web Development, particularly Magento Development.

Are Your Customers Getting The Magento Emails You Send?

MageSend Magento Email sending with Amazon SES

I have made a premium Magento Extension called MageSend. MageSend makes it simple and easy to send using Amazon's highly reliable SES email service.

The extension is $99, and comes with a complete money back guarantee, please check it out, I am confident it will resolve any email sending problems with your Magento store.

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

  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?

  3. Hey Ashley –

    Thanks for the writeup.. I’m in the middle of trying to accomplish exactly this.

    Here’s my setup:
    MBP + MAMP + VirtualHost = My development environment

    I want to view the websites I’m currently developing from anywhere on my local network (exactly what this article lays out), but I’m having some trouble here… :-/

    This is my vhost file on my development box:

    # Created Wed Feb 9 23:13:24 PST 2011
    <VirtualHost 127.0.0.1:80>
    DocumentRoot /Applications/MAMP/htdocs/jennylynne
    ServerName jennylynne.dev

    ScriptAlias /cgi-bin /Applications/MAMP/htdocs/jennylynne/cgi-bin

    <Directory /Applications/MAMP/htdocs/jennylynne>
    Options All
    AllowOverride All
    Order allow,deny
    Allow from all
    </Directory>
    </VirtualHost>


    Here’s my /etc/hosts file from my development box:

    ##
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting. Do not change this entry.
    ##

    127.0.0.1 localhost
    255.255.255.255 broadcasthost
    ::1 localhost
    fe80::1%lo0 localhost

    127.0.0.1 jennylynne.dev

    Here’s my /etc/hosts file from the computer on the same local network:

    192.168.1.156 jennylynne.net

    The problem is that I simply get a directory listing of my /Applications/MAMP/htdocs folder when loading up http://jennylynne.dev on my “local network” computer, whereas when i go to the same URL on my dev box, the site loads up fine. any thoughts?

    P.S. – I’ve verified the IP address also..

  4. Hi, Eric
    Not sure if it’s a copy paste issue, but your other box hosts file has:
    192.168.1.156 jennylynne.net

    That needs to match the ServerName in the Apache config, which is .dev. let me know if that was the issue.

  5. Hey Ashley,

    Thanks for the post … curious if you’d be able to help me.

    I’ve created a VM of CentOS on my MacBook Pro with the ethernet set to Bridged. So, the VM has it’s own static IP address of 192.168.1.111

    Within the “hosts” file on the VM, I’ve got:

    127.0.0.1 dev.example.com

    Within the “hosts” file on my MacBook Pro (the host machine), I’ve got:

    192.168.1.111 dev.example.com

    Now the site works fine from within the VM, but doesn’t pull up anything from the host MacBook Pro…

    I’ve spent literally 2 days trying to setup this stupid network and I have no idea what the issue is! :X

  6. Check if you can access the VM on port 80 by runnign this in a terminal:
    telnet 192.168.1.111 80

    If that does not connect, then I’d check for firewalls on CentOS, check Apache is listening on the right interface not just localhost.

    If you _can_ connect, then I’d try and look for errors in Apache logs. Try access just the default ‘It Works’ page etc.

  7. Hi,
    I have a question about the localhost IP and domain name.

    Firstly I registered one domain name called vietyoung.org
    Secondly, my Internet service provider gave me one static IP address. 123.45.67.89
    Thirdly, I gave this IP address to my domain registry company and they points my domain name to this IP address.

    Fourly, I installed Apache web server in my personal computer
    and I tested this Apache web server installation successfully with the testing page when I type http://localhost/index.htm
    However, when I typed in my domain name http://www.vietyoung.org, I got the issue about the page is not found or there is no internet connection.

    I checked that my index page is in the right document root folder as shown in localhost.
    I checked that my internet connection is fine because I can view yahoo web page.

    I try to configure the host file and virtual host file to set my static IP address and domain name. But I cannot view my site under http://www.vietyoung.org

    Can you tell me difference between the IP address of my computer or IP address of internet service provider?
    Which IP address can be used?