Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Virtual Hosts Not Working As Expected

My Apache "httpd-vhosts.conf" looks like this::

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/"
    ServerName localhost
    ServerAlias *.localhost
</VirtualHost>

<VirtualHost laravel.dev:80>
    DocumentRoot "c:/wamp/www/laravel/public"
    ServerName laravel.dev
    ServerAlias *.laravel.dev
</VirtualHost>

<VirtualHost learninglaravel.dev:80>
    DocumentRoot "c:/wamp/www/learningLaravel/public"
    ServerName learningLaravel.dev
    ServerAlias *.learningLaravel.dev
</VirtualHost>

and my "...system32/drivers/etc/hosts" also looks like this::

127.0.0.1       localhost
127.0.0.1       localhost

// I added the following entries. The first two entries above was already there
127.0.0.1       laravel.dev
127.0.0.1       learninglaravel.dev

When i enter "learningLaravel.dev" and "laravel.dev" into the browser, they work fine as expected. But i have other folders in my "www" folder that i use them to learn PHP and i want to be able to access the files in those folders directly from the browser like say "localhost/test/me.php". But anytime i enter such address the browser goes to the second entry in the vhosts-conf file [which prints a laravel error meaning that it can't find the file]. It seems that the first entry in the vhosts-conf file is not working and Apache bypasses it to the second entry. The first entry is suppose to be the catch all entry. I tried to swap the second and third entries to see how it will behave but it always direct the browser to the second entry instead of the catch all (first entry) that is suppose to handle addresses likes "localhost/test/me.php"

Anytime i enter only "localhost" into the browser, it goes straight to the second entry instead of say printing the contents of the "www" folder.

How do i solve this problem? thanks.

like image 774
Eddy Freeman Avatar asked Jul 19 '15 17:07

Eddy Freeman


People also ask

How to fix Apache not connecting to virtualhost on Linux?

At first, be sure that your /etc/apache2/ports.conf contain line Listen 80 At second, be sure that you have enabled your config using sudo a2ensite yoursite.name. Then, restart apache using sudo service apache2 restart and try to get access to virtualhost using the browser.

How do you determine the host of a virtual host?

Name based virtual hosts determine the host through the ServerName directive, and not through the FQDN in the VirtualHost directive ( <VirtualHost FQDN:80> ). In fact this is seen as <VirtualHost 127.0.0.1:80> What happens is your case is documented in the VirtualHost doc, last 2 paragraphs (just before "Security"), quoted:

What is the default virtual host for a given IP address?

If no matching name-based virtual host is found, then the first listed virtual host that matched the IP address will be used. As a consequence, the first listed virtual host for a given IP address and port combination is the default virtual host for that IP and port combination.

How does the server select the best virtual host?

If multiple virtual hosts contain the best matching IP address and port, the server selects from these virtual hosts the best match based on the requested hostname. If no matching name-based virtual host is found, then the first listed virtual host that matched the IP address will be used.


1 Answers

It seems the problem comes from the way you use the VirtualHost directive.

Using a fully qualified domain name for the IP address of the virtual host is not recommended. It is misleading how it works. Name based virtual hosts determine the host through the ServerName directive, and not through the FQDN in the VirtualHost directive (<VirtualHost FQDN:80>). In fact this is seen as <VirtualHost 127.0.0.1:80>

What happens is your case is documented in the VirtualHost doc, last 2 paragraphs (just before "Security"), quoted:

When a request is received, the server first maps it to the best matching based on the local IP address and port combination only. Non-wildcards have a higher precedence. If no match based on IP and port occurs at all, the "main" server configuration is used.

If multiple virtual hosts contain the best matching IP address and port, the server selects from these virtual hosts the best match based on the requested hostname. If no matching name-based virtual host is found, then the first listed virtual host that matched the IP address will be used. As a consequence, the first listed virtual host for a given IP address and port combination is the default virtual host for that IP and port combination.

So when you ask for localhost/somedir, the server will try to find from the non-wildcards VHosts declarations, but do not find any with corresponding host name (ServerName), and so it chooses as "default" the first VHost with IP:Port, and not the first with *:Port.

To solve your problem, try to use <VirtualHost *:80> in all three vhost declarations :

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/"
    ServerName localhost
    ServerAlias *.localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/laravel/public"
    ServerName laravel.dev
    ServerAlias *.laravel.dev
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/learningLaravel/public"
    ServerName learningLaravel.dev
    ServerAlias *.learningLaravel.dev
</VirtualHost>

And reload / restart Apache.

(My only doubt about this is why Nasreddine could make a working test case with your setup.)

like image 163
Zimmi Avatar answered Oct 08 '22 12:10

Zimmi