How to access http://localhost on the host operating system if the server is in a virtual machine?

3

I always used XAMPP for my LAMP environment and developed directly on Windows 7. But now I want to do all my web development on Linux so I installed Ubuntu in virtualbox and installed PHP/MySQL and Apache individually.

I can access everything in the browser inside my VM, but now I want to access http://localhost in Windows. How can I do this, I assume it should be easy enough since both OS's are in the same internal network?

Emily

Posted 2011-10-22T01:46:50.813

Reputation:

Answers

4

As far as your Windows machine is concerned, the Ubuntu webserver in VirtualBox is a completely separate computer. 'localhost' won't work. So long as the Ubuntu Apache is binding eth0, you should be able to access the Ubuntu-based site via direct IP. http://172.16.12.34/ for example.

You can find the IP of the Ubuntu install a couple of ways. /sbin/ifconfig eth0 being one of them. Use that IP address in the URL instead of localhost.

SysAdmin1138

Posted 2011-10-22T01:46:50.813

Reputation: 5 239

You can also try opening http://www.ipchicken.com/ in the Internet browser on the VM.

– bwDraco – 2011-10-22T03:41:03.967

4

VirtualBox provides several networking modes for attaching guests to the host machine. The default is NAT which by default does not expose the guest to the host. Check out which mode you are using by going to the settings window for your VM and clicking on the network tab. You'll see the Attached: property followed by a drop box.

If you're using the NAT mode we can use port forwarding to expose port 80 on the guest. Open the Advanced section on the networking settings window and click the Port Forwarding button. On the resulting dialogue we want to add an entry. Let's name it Web, the protocol will be TCP the host port can be any port you want to connect to on your windows box. Let's make it 80. The guest port needs to be 80 as that's what your web server is listening to on your linux guest VM. The Host IP and Guest IP can be left blank. Click OK for the dialogue and the setting window. Start up your VM and you should be able to connect to http://localhost from your windows machine.

For more information about how VirtualBox handles networking check out the documentation at http://www.virtualbox.org/manual/ch06.html

orien

Posted 2011-10-22T01:46:50.813

Reputation: 141