How to access the VirtualBox web server from the host using VBox port forwarding

If you are looking for a way to access/connect to the web server running inside your Ubuntu VirtualBox VM from a Windows host, here are the steps.;

  • Run the web server in Ubuntu VM (apache or Nginx)
  • Identify the port of the web server – by default, it is PORT 80
  • From VirtualBox host – VM settings, enable port forwarding

Let us do it in straightforward three steps

Related: How to attach a VM to the VirtualBox NAT network?

Step #1 Start and run the webserver in the VM

First, ensure that the webserver is running in the VM. You may verify this by accessing the URL: localhost from a browser inside the VM

Or using the command:

# For Apache2
sudo systemctl status apache2.service

If in case you have Nginx running

# For Nginx
sudo service nginx status

Once you are sure that the website is up and running, proceed to Step #2

Step #2 How to enable port forwarding in VirtualBox VM

We are going to forward the Host machine’s ( your laptop’s ) PORT 80 to the VirtualBox VM’s PORT 80

For this, follow the below configuration steps

  • Open VirtualBox VM -> Settings
  • Go to Network -> Attached to NAT
  • Expand Advanced and enable checkbox – Cable Connected
  • Click on Port Forwarding
  • Doubleclick and enter the value 80 under Host PORT and Guest PORT, as shown here:

Save your settings by clicking OK.

Step #3 Accessing the URL from your host’s web-browser

Now open your host machine browser and enter localhost as the URL

Now, you should be seeing your Ubuntu-hosted webserver from your Windows Host!

Notes:

This setup works, even if your machine is not connected to the internet (or has an external network).

You may also access the web server using a domain alias by entering them in your Windows host file. The host file is located at:

C:\Windows\System32\drivers\etc\hosts

Make a new line entry for the test-domain name, such as for code.codetryout.com:

127.0.0.1   code.codetryouot.com

By entering this address in your browser, you can access the VM-hosted webserver from the host machine.