Search code examples
apachevagrantvirtual-hosts

How to access vagrant guest servername?


I installed Vagrant,set up vagrant box as per the tutorial of the site Hashicorp, i have set up the port forwarding, the private network with a unique IP, and folder syncing.

Here is what I have when I SSH in :

login as: vagrant
vagrant@127.0.0.1's password:
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-101-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Wed Nov 30 10:35:39 UTC 2016

  System load:  0.22              Processes:           86
  Usage of /:   3.7% of 39.34GB   Users logged in:     0
  Memory usage: 25%               IP address for eth0: 10.0.2.15
  Swap usage:   0%                IP address for eth1: 192.168.33.10

The vagrant file is in C:\vagrant2 (Windows 10)

I have two folders that containt index.html file.

The var/www/html is synced with C:\vagrant2\html folder in the host (by default), i added another folder The var/www/html2 is synced with C:\vagrant2\html2 folder in the host

the vagrant file is like this :

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.synced_folder "./html2", "/var/www/html2", :mount_options => ["dmode=777", "fmode=644"]
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 4567
config.vm.network "public_network", ip: "192.168.33.10"

in the guest the /etc/hosts file is like this (I defined 2 hosts):

127.0.0.1 localhost
192.168.33.10 box.local
127.0.0.1 bebox.local
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

the Apache configuration file is like this :

Listen 81
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ServerName box.local
</VirtualHost>

<VirtualHost *:81>
    DocumentRoot /var/www/html2
    ServerName bebox.local
    ServerAlias bebox
</VirtualHost>

I can access the pages via the ip address forwarding : http://127.0.0.1:4567/

i can access the same site with its IP : 192.168.33.10

I can access the second site that is listening on port 81 : 192.168.33.10:81

However i need to access by the servername i defined in the guest. I typed in my browser in Windows 10 :

http://box.local

http://bebox.local

none of them work i have in Chrome console :

GET http://box.local/ net::ERR_NAME_NOT_RESOLVED

In guest operating system, when I ping box.local or bebox.local, it's okay.

On host machine, pinging the names doesn't work, it says didn't find the host box.local however pinging even localhost does work

I tried disabling the Firewall of Windows, but doesn't change anything.


Solution

  • You need to modify the host file from your windows host machine (it should be at C:\WINDOWS\system32\drivers\etc\hosts) and add the following

    192.168.33.10    box.local
    192.168.33.10    bebox.local