Virtualhosts not working on guest VM accessed via host

apache-2.2virtualboxvirtualhost

I have a Windows 7 host machine running VirtualBox (v4.122) and a guest VM of Kubuntu 12.04 running Apache2.

I can access the Apache server from the host machine but it will only load the default Apache welcome page and is ignoring all of my virtual hosts.

Some more details about the configuration:

  • The Kubuntu VM has several virtual hosts that all work fine when accessed via a web browser inside the VM.
  • I am using port forwarding in VirtualBox to send port 8080 on the host machine to port 80 on the guest.
  • Both /etc/hosts in Kubuntu and C:\Windows\System32\drivers\etc\hosts have the entries for the local domains and are set to resolve to 127.0.0.1
  • Accessing 'localhost' on the Kubuntu machine will resolve to the directory set in the default vhost (/var/www/) but accessing localhost:8080 servers up the default web page for the server (i.e. "It works …")

Updated to add output from apache2ctl -S:

     VirtualHost configuration:127.0.1.1:80  is a NameVirtualHost
     default server dghportal.local (/etc/apache2/sites-enabled/dghportal.local:1)
     port 80 namevhost dghportal.local (/etc/apache2/sites-enabled/dghportal.local:1)
     port 80 namevhost local.classtoolkit.org (/etc/apache2/sites-enabled         /local.classtoolkit.org:1)
     wildcard NameVirtualHosts and _default_ servers:
     *:80 is a NameVirtualHost
     default server itechsrv-kuvm1 (/etc/apache2/sites-enabled/000-default:1)
     port 80 namevhost itechsrv-kuvm1 (/etc/apache2/sites-enabled/000-default:1)
     Syntax OK

Best Answer

Looks like your custom <VirtualHost> blocks are configured as <VirtualHost 127.0.1.1:80> - this won't ever catch requests from a different host.

The requests from the other device are being caught by the vhost defined at /etc/apache2/sites-enabled/000-default:1 - this is what's causing the welcome page to appear.

Change your vhost definitions from <VirtualHost 127.0.1.1:80> to <VirtualHost *:80>, and you should be all set! You might also want to a2dissite default, so that your sites, not the welcome site, can catch requests to a hostname that doesn't match one of the ones that's already configured.