Linux – How to access apache server running in virtualbox from host

linuxnetworkingvirtualbox

I want to access apache web server running in centos virtualbox. With my laptop connected in office lan network, I can access that webserver with bridged networking mode. But what I want to do is, access it when not connected to any network. Is this possible with virtual interfaces?

Best Answer

The port forwarding feature accessible from the Network advanced settings of your virtual machine allows you to selectively expose a port of your virtual machine to your host.

This way, you can setup virtual machine port 80/tcp (http traffic) to be easily accessible from your localhost port 8080. Below is an exemple for this setting:

Name: webserver http
Protol: TCP
Host IP: 127.0.0.1
Host Port: 8080
Guest IP: 
Guest port: 80

Do the same with port 443/tcp (https traffic) if you serve it as well (ex: 443 -> 8443). Once done, you don't even need to bother with you virtual machine IP address to access its website. Just browse http://127.0.0.1:8080/ and you are done.

Notes:

  • It is fine to let Guest IP blank if it is assigned or known by VirtualBox itslef (I don't think this is the case with bridge mode, so I advise you to choose Network Address Translation (NAT), Internal Networking or Host-Only networking options. VirtualBox online documentation explain key differences between networking options. NAT will allow your virtual machine to connect the Internet for example, which you might like or dislike.

  • If you clears Host IP field, then other computers on you physical network will be able to connect this virtual machine web-server using your host IP address (ex: http://192.168.1.xxx:8080). This can help you setting up a unique configuration for your both use cases and even improve the security of your virtual machine by not exposing all its open ports to real networks.

  • You can not easily use local port 80 for this forward since ports below 1024 require privileges. Hence the common choice of 8080 in such case.