Connect to Your Local Development Sites from VirtualBox Guests

Like many web developers, I do most of my work with a web server running on my local machine.  In my case, I’m running Apache HTTPD from MacPorts on my MacBook Pro, and I have various sites in development using either Ruby or PHP.  Due to the nature of the projects, or simply because it’s a good idea, I prefer to test my sites in various browsers, including Internet Explorer, which is not available on Mac OS.  So, I’ve set up multiple virtual machines using the freely available VirtualBox.

I’ve set up aliases to localhost, and corresponding NameVirtualHost settings for Apache, on my Mac for each of the sites I’m developing.  Then I can just enter, for example, “myproject.local” in my browser, and view my work in progress.  (I use the nice Ruby gem “ghost” to manage aliases.)  However, this doesn’t work with a default VirtualBox guest, so below is my method for getting this to work.

Start the VirtualBox VM Manager

  1. To begin, start your install of VirtualBox.  I believe that you need at least version 2.2.0, which introduced host-only networking.
    vbox_start
  2. Open the settings for the desired VM and navigate to the Network tab.  You should already have something in the Adapter 1 section.
    vbox_adapter1

Add a Local-Only Network Interface

  1. Click on the Adapter 2 tab.  You should typically see a disabled interface, something like this:
    vbox_adapter2
  2. Enable Adapter 2 by checking the box.  Then change the “Attached to:” box to “Host-only Adapter.”  The Name box shows “vboxnet0″ on my install:
    vbox_adapter2_new
  3. Save your settings and start your virtual machine.

Verify Host-Only Adapter Address

VirtualBox should create a new network interface on your host OS that is used for host-only networking.  On Mac OS, verify this by entering the following in a Terminal:

ifconfig vboxnet0

You should see something like the following.  Note the IP address – you will need it later:

vboxnet0: flags=8943

Set up Guest OS

You need to set up host aliases in your guest OS that match those you set up in your host, only that you will point them to the IP address you noted previously.  The following instructions apply to Windows XP and newer.

  1. Open your hosts file.  Use Start -> Run… and enter “notepad c:WINDOWSsystem32driversetchosts”.  UPDATE: On Windows Vista and Windows 7, there are additional protections on this file.  You will have to run Notepad (or your favorite text editor) as Administrator first – right click on its shortcut and choose “Run as administrator.”
  2. Add a line to the file that associates your host-only IP address with your development site aliases.  The IP address comes first, followed by each alias, separated by spaces.
    192.168.56.1    myproject.local otherproject.local

Test the Configuration

As long as your local web server is running on the host OS, you should be able to enter one of your site aliases in the guest’s browser and get the expected results.

This entry was posted in Code and tagged , . Bookmark the permalink.

5 Responses to Connect to Your Local Development Sites from VirtualBox Guests

  1. Robert Pierce says:

    Pete, thank you for taking the time to figure this out and post it up. This saved me a bunch of time and stress in the midst of an important project push. I appreciate it.

  2. James says:

    This is great thank you so much for this tutorial I have been searching every where for it. You have saved my life.

    However my VM is producing some weird results. I’m running:

    Host Machine: Mac OS X Snow Leopard 10.5.6
    VirtualBox 3.2.0
    Guest: Windows XP

    one of my sites work dev.curo-consulting.com
    but dev.cereno.co.uk isn’t working it tells me that there is a 500 Internal Server error.

    Even after a server reboot.

    My hosts file looks like this:

    127.0.0.1 localhost

    192.168.56.1 dev.cereno.co.uk dev.curo-consulting.com

    Have you ever experienced this?

    I did have it like this as well:

    127.0.0.1 localhost
    192.168.56.1 dev.cereno.co.uk
    192.168.56.1 dev.curo-consulting.com

    But that didn’t work either :-(

    Any Ideas?

  3. pete says:

    James,

    You may want to check your web server logs in order to get more information than the “500 Internal Server Error” message. This doesn’t seem like a problem caused by your use of a VM.

  4. Yuriy Voziy says:

    This helped alot! Worked perfectly for Ubuntu Host and XP guest.

  5. Jay says:

    Than you very much. I have documented your solution as it worked perfect. The IP address given to me was the same as given to you 192.168.56.1 so perhaps that is universal.

    Jay
    Owner
    CompuMatter

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>