Accessing localhost in Mac OsX from Windows VM in Virtualbox
Oh the joy of cross-browser testing… The worst part is that it’s rather difficult to even just see your dynamic pages from localhost in your Virtualbox VMs… Here’s how I did it.
Since I’m not going to cover everything, here’s what you need to have to get started :
- Virtualbox installed
- Some Windows virtual machines installed (I have XP and Seven)
- A website on local, Mamp in my case
Having your Windows installations access your localhost
First part is to give access to your host’s (Mac) localhost to your guests (Windows). This is rather easy, open up Virtualbox, go in
Virtualbox>Preferences>Network and click on the little icon to add a new one. You don’t need to configure anything in here. Here’s what it’s supposed to look like
Now let’s give each of our VMs access to this new local network we created, in the list of VMs you have, select one, click on “Settings” at the top of the list and go in
Network>Adapter 2. If you already have a network adapter in here, go to the 3rd one instead, but if you have done anything to your network settings, it should be empty.
Activate this 2nd adapter and set it with
Host-only Adapter and the only Name attribute in the list we just created earlier.
IP stuff you’ll need to know
While you’re still enjoying the beauty of Mac Os, fire up your Terminal.app and run
ifconfig vboxnet0(change vboxnet0 to the actual value of your local name).
Some stuff will appear in there, including
inet, you’ll need this IP to setup your VMs so either keep this Terminal opened or note the IP somewhere.
If you’re on Windows XP then it’s rather simple, start by opening your Notepad app, then in Windows’ ugly Finder-like thing, browse to
C:\WINDOWS\system32\drivers\etc and drag the
hosts file in your Notepad window. Here you see the current configuration.
Add something like that to the end of your hosts file, using the IP I told you to carefully note earlier and the url you’d like to access your localhost from your VM.
Now if you open your browser and go to the url you’ve just setup in your VM in some crappy old browser you want to test stuff on (cmon who runs IE on a VM for fun ?) and it should appear… Unless
Issues I’ve encountered
Along the path, I’ve had to face a few issues and spent countless time googling, here’s how I solved them :
Your local website isn’t exactly on
If you’re using MAMP or something similar, you may have setup your local website to be on
some.url.local or any kind of fancy url you like. This may not work depending on how you’ve setup your fake url.
In your httpd.conf you can write it in 2 ways :
<VirtualHost some.local.url:80> which is going to fail, or
<VirtualHost *:80> which will work in your VM.
If you’re using something that looks like the first piece of code, go ahead and change it to the second one. Now it should work on your VM (once you’ve rebooted your Apache server).
This is going to be an issue if you need to have access to 2 different local urls in your VMs. I don’t need this so it’s not an issue for me and I didn’t research how to fix that, you’ll have to do some more googling for that.
hosts on Windows 7 (and Vista as well I’ve heard)
Due to permissions issues, you may not be allowed to save your
hosts file on some versions of Windows… This is rather annoying and it took me a while to figure out, but basically what you want to do is right click on the file, edit the permissions for all users then edit and save the file.
It gets faster after the 2nd VM
The process is tedious and takes a moment but once you’ve done it a couple times it gets really easier to do. I can’t believe it’s so difficult to make something that simple work but I guess it’s the price one has to pay to have websites that don’t break in IE7 or commit logs full of garbage.
Also if you think that you can just save static generated pages share them using a shared folder and test that, know that IE7 and 8 will actually show the file as an intranet file with different parameters, making this solution rather unreliable, avoid that whenever possible. Plus it’s much easier to actually edit the files rather than copies of the files to copy the changes back when you’re done !
Have nice testing folks !