VirtualHost always returns default host with Apach

2019-03-08 04:13发布

问题:

I try to setup a virtual host besides the default localhost. Whenever I try to call my virtual host http://test I get the default Apache2 Index file that sits in the directory of http://localhost. Furthermore apache returns this page still after disabling (a2dissite) both VirtualHost files an reloading apache (service apache2 reload).

What could go wrong that the virtual host is not working?

Configuration:

My directory structure is the following:

/var/www/html                  # Default localhost dir
/var/www/html7index.html       # Apache2 default index

/var/www/test                  # HTML dir for the virtual host
/var/www/test/index.html       # My "website" 

Content of /etc/hosts:

127.0.0.1       localhost
127.0.1.1       Laptop
127.0.0.1       test

Directory content of /etc/apache2/sites-available:

000-default.conf
default-ssl.conf
test.conf

File 000-default.conf:

<VirtualHost localhost:80>
        ServerName localhost
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

File test.conf:

<VirtualHost test:80>
        ServerAdmin test@localhost
        ServerName test
        NameVirtualHost test
        ServerAlias test
        DocumentRoot /var/www/test
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

回答1:

I was facing this issue, and it turned out I had to disable the default virtual host.

sudo a2dissite 000-default.conf

Possible Expanation:

According to the apache documentation An In-Depth Discussion of Virtual Host Matching:

[...] If the main server has no ServerName at this point, then the hostname of the machine that httpd is running on is used instead.

That means if the default vhost (commonly 000-default.conf) has no ServerName set - which is the default - Apache will fallback to the hostname of the operating system.

As a result, Apache selects and serves from the default vhost (000-default.conf) even though another user-created vhost with the same ServerName as the machine's hostname is configured.

The reason is that Apache sorts the vhosts alphabetically by filename and chooses the first vhost configuration that matches the requested HTTP Host header. Thus, the 000-default.conf is checked before user-defined vhosts, as they are usually not prefixed with 000-.



回答2:

tl;dr: Call it with sudo: sudo service apache2 reload


Looks like the behaviour of service apache2 reload fooled me. See the following log:

user@Laptop:/etc/apache2/sites-available$ sudo a2ensite test.conf 
Enabling site test.
To activate the new configuration, you need to run:
  service apache2 reload
user@Laptop:/etc/apache2/sites-available$ service apache2 reload
 * Reloading web server apache2                                                  * 
user@Laptop:/etc/apache2/sites-available$

Try to reach http://test: NOT working

user@Laptop:/etc/apache2/sites-available$ sudo service apache2 reload
 * Reloading web server apache2                                                  * 
user@Laptop:/etc/apache2/sites-available$

Try to reach http://test: WORKING

So, find the difference! The point is that I thought it would've reloaded correctly in the first place. There are no entries in the log files either. Calling it with sudo helped. Is this a bug?



回答3:

Just pointing this out that may be obvious for experienced users, but not so much if your a first timer.

take sure that the config you're using in /sites-enabled ends with a .conf as expected in your apache2.conf

That was my problem and it fixed it.



回答4:

If you have already tried the following:
1. Checked permissions of the document root and parent folder.
2. a2dissite 000-default etc.
3. Restarted apache with sudo service apache2 reload
And it is still not working then do the following:

1. Enable debug logging:
vi /etc/apache2/apache2.conf
LogLevel debug

2. Restart apache2 and monitor the logs
sudo service apache2 restart
tail -f /var/log/apache2/*.log

3. Notice if the IP of the server is showing in the logs as expected:

==> /var/log/apache2/other_vhosts_access.log <== ip-xxx-xx-xx-xx.eu-west-2.compute.internal:80 xx.78.xx.2xx - - [13/Sep/2017:18:40:44 +0100] "GET / HTTP/1.1" 200 3509 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0"

You'll notice that instead of xxx.xx.xx.xx:80 like a typical IP address
I've got some FQDN like "ip-xxx-xx-xx-xx.eu-west-2.compute.internal:80"
I'm using AWS Elastic IP's to make a fixed IP for my Amazon EC2 web server.

4. Check the head of your virtual-hosts .conf file

<VirtualHost 127.0.0.1:80 11.22.33.44:80>
# Added from nessus to make more secure
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]
ServerName mydomain.com
...

OK so what we see here is we're telling Apache to serve this vhost for requests from 127.0.0.1 or 11.22.33.44 on port 80. But the server never sees these IPs as it get's that weird FQDN and so it never matches! Eureka!

SOLUTION:
Add *:80 to the VirtualHost tag so it looks like this:

<VirtualHost 127.0.0.1:80 11.22.33.44:80 *:80>


AND restart apache. I hope this helps. There may be other reasons but if this is the cause of your problem then it's time to sit back, have a cuppa tea and relax! If not then I hope you find the solution.

PS. Remember to put your logs back to warn rather than debug!



回答5:

I ran through all of these solutions and none of them worked. Then, I decided to go back and check the apache2.conf file. I discovered that the line to include the "sites-enabled" conf files was commented out by default. By uncommenting the line and restarting apache, it fixed my problem (it was close to the end of the file):

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf 


回答6:

it has been driving me nuts too, I have cheked my .conf files over and over again all was absolutely defined correctly. I ended up on this post tried the a2ensite and a2dissite commands as a sudoer, restarting apache, still landing on the default apache welcome page as if apache would have never disabled the default site or taken my new virtual host onboard.

It turns out I was trying to restart apache without using sudo...

service apache2 restart

use

sudo service apache2 restart


回答7:

if you keep default with *:80; *.443 make sure it is on the bottom - so rename 000-default to zzz-default



回答8:

This has bitten me more than once; sometimes I would enable with sudo - sometimes I would not.

Ubuntu 14.04 seems to display the same message either way. When moving at 100 miles an hour, sometimes you miss a sudo or two.

The symptom of the new website defaulting back to wrong directory has been my only clue the the enabling never happened. I'd say it's a bug.

It should at least warn when a command isn't executed!!!



回答9:

For me it started to work after restarting apache with: sudo service apache2 reload. And pressing ctrl + F5 on http:/test.



回答10:

I have seen this happen when using a fully qualified domain name (FQDN) in the /etc/hosts file that is the same as a site I was trying to connect to. To solve this problem I had to rename the FQDN to something other than one of my VirtualHost ServerName values.

So in other words, if my /etc/hostname was:

apple

And in my /etc/hosts, I had:

192.168.54.34 apple.example.com apple

And in my /etc/apache2/sites-enabled/apple.example.com.conf I had:

<VirtualHost *:80>
    ServerAdmin blah@apple.example.com
    ServerName apple.example.com
    DocumentRoot /var/www/apple.example.com
    DirectoryIndex index.html index.php
</VirtualHost>

apple.example.com would be accepted by the 000-default.conf because it is also listed in the /etc/hosts file. To solve this I just changed the FQDN of my server as follows in the /etc/hostname file:

server1

and /etc/hosts file:

192.168.54.34 server1.example.com server1


回答11:

My case on Apache 2.4 the issue was solved by adding below setting to virtual host configuration:

Listen 80

so that settings are something like:

Listen 80
<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName testsite.com
    ServerAlias www.testsite.com
    DocumentRoot /var/www/testsite
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


回答12:

You need to replace the default html file (/var/www/html/index.html) with your site or rename the file to something like index.html.bak. For some reason apache defaults to the index.html unless you change it.