The instructions available elsewhere when it comes to accessing a web server from a host operating system to a guest operating system, inside a VirtualBox machine, may seem vague or incomplete. I’m trying to rectify that situation by taking you through the process, step-by-step, after you install a guest operating system.
Whether your guest operating system is Windows, Linux or something else, the instructions are the same. You can click on each picture to get a full-sized view of what I’m explaining.
You don’t to do anything with your machine to create a host network, but you have to shut it down or discard the saved state when you change the network settings within it. Let’s do this the right way and make sure it’s shut down and not saved.
Click on “Properties” for “vboxnet0” and make sure “Configure Adapter Manually” is selected. You’ll then see the IPv4 address assigned to the host machine. If you want to change the “/24” block, you can do it here.
Click on “DHCP Server” next to “Adapter” and you can see the details of your “/24” block. Make sure “Enable Server” is selected. When you install a web server on your guest operating system and try to access it from your host operating system, you’ll access an IP address anywhere from the range of “Lower Address Bound” to “Upper Address Bound”. Unless you’re doing something unusual, it’ll probably be the first one available, ending in “.3”.
If you want to make sure you always access the same IP address, set the lower and upper address bounds to the same number. It’s easier than trying to set a static IP address for the guest operating system from within.
Regardless of what web server software you use, you should now be able to access it from a web browser on the host operating system, using the IP address the machine provided in the address bar. If you want to use a domain name instead, you’ll need to add the IP address and domain name in your “hosts” file. On Windows, it’s located at “C:\Windows\System32\Drivers\etc\hosts”. You may have to uncheck the read only status of the file properties.
On Linux, it’s located at “/etc/hosts”. I’m not aware of where it is on a BSD or Macintosh system, but it’s probably in the same place since they’re both UNIX-like systems, just like Linux. The format is the IP address from your guest operating system and your chosen domain name, like this:
192.168.56.3 example.com www.example.com
There are many web development environments and this could be one of them. You could share a folder between the host operating system and the guest operating system to edit web server files. Or, you could do everything from within the VirtualBox machine when you use a lightweight Linux distribution with a desktop.
Since I already use Linux Mint for everything, it’s easier for me to install the web server components without messing around with VirtualBox machines. With Windows, you have to either install a web server stack (WAMP, WEMP or something else) or do it in a virtual environment. Setting up a Linux server in a VirtualBox machine is a much better idea, but that’s only my opinion.