Search code examples
apachevagrantvirtualhost

Problems accessing Apache vhost in Vagrant


I have Apache server running on Ubuntu in a Vagrant box. In my sites-available directory I have two files:

cats.com.conf

<VirtualHost *:80>
    ServerName cats.com
    ServerAlias cats.test
    DocumentRoot /var/www/cats.com
</VirtualHost>

dogs.com.conf

<VirtualHost *:80>
    ServerName dogs.com
    ServerAlias dogs.test
    DocumentRoot /var/www/dogs.com
</VirtualHost>

In Vagrant I have this port forwarding set up:

Host IP   | Host Port | Guest IP  | Guest Port
----------------------------------------------
127.0.0.2 | 8000      | 10.0.2.15 | 80

In my hosts file:

127.0.0.2 cats.test
127.0.0.2 dogs.test

But when I visit cats.test in my browser, it can't connect.


Solution

  • Assuming your site does responds correctly when you test it from your Ubuntu VM itself, then it is missing the port in the URL.

    https on the Ubuntu VM listens on port 80. When you specified the Vagrant port forwarding:

    Host IP   | Host Port | Guest IP  | Guest Port
    ----------------------------------------------
    127.0.0.2 | 8000      | 10.0.2.15 | 80
    

    it told Vagrant to listen to port 8080, and forward traffic to port 80 of the Ubuntu VM.

    Thus on your host system, you have to point your browser to http://cats.test:8080/.

    Info: when you connect to a web site with http://example.com/, it defaults to port 80 (http://). For https://example.com/ it defaults to port 443. But http traffic can be handled on any port. You specify the port with :PORT following the domain you want to access.