VirtualHost always returns default host with Apache on Ubuntu 14.04

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>

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-.

Setting Up Apache Virtual Hosts- I keep getting the Apache default , I stopped after the database was setup and followed this guide How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS to test if it would  Apache comes with a default virtual host file, 000-default.conf. Copy this file and modify it for the first domain. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/test1.conf sudo nano /etc/apache2/sites-available/test1.conf The file will look like this (without comments):

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?

How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS , Apache comes with a default virtual host file called 000-default.conf that we can use as a jumping off point. We are going to copy it over to create  apache - how - ubuntu virtual host not working VirtualHost always returns default host with Apache on Ubuntu 14.04 (8) I try to setup a virtual host besides the default localhost .

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

Make 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.

Add an Apache Virtual Host on CentOS 7 and Ubuntu 16.04, 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  Apache comes with a default virtual host file called 000-default.conf that we can use as a jumping off point. We are going to copy it over to create a virtual host file for each of our domains. We will start with one domain, configure it, copy it for our second domain, and then make the few further adjustments needed.

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!

Default Apache page shows up even after configuring virtual host, You can add as many Virtual Hosts to Apache as your server can handle, based and running by default on Cloud Servers running CentOS 7 and Ubuntu 14.04. Note: For any Cloud Server with Plesk, virtual hosts should always be added  By default, apache comes with a default virtual host file called 000-default.conf. We will copy the 000-default.conf file contents to our new virtual host files. We will copy the 000-default.conf file contents to our new virtual host files.

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 

Updating Virtual Host Settings from Apache 2.2 to Apache 2.4, Please make sure that your virtual host config is enabled by checking the symbolic file link ls -l /etc/apache2/sites-enables which should contain  Stack Overflow Public questions and phpmyadmin is not working with apache virtual host Ubuntu 14.04. VirtualHost always returns default host with Apache on

How to fix common problems with Apache2, A step-by-step guide to updating virtual host settings from Apache 2.2 to Apache 2.4. new version of Apache makes to the default virtual host and module configuration. Ubuntu and Debian: Virtual host configuration files in the Ubuntu, 14.04 LTS, April 17, 2014 We're always expanding our docs. I have one virtual host but when i make second one and going to check ping its goes to first one website, and when check in browser its says 404 not found, but first one works fine.

Apache2 still shows default page after trying to configure it, Apache2 problems: if you've encountered an issue loading your web page, follow these On Debian and Ubuntu machines have a virtual host file by default and it is stored in /etc/apache2/sites-enabled/. find a fault, but the website just still won't load, it's always a good time to dig through logs. 2019-05-14 at 06:58. As you can see, the IP address in my case is 192.168.21.166.It will be different for you. So, make sure to replace it with yours from now on. If you want to learn how to configure Apache VirtualHost locally, then you can use the /etc/hosts file for local DNS/name resolution.

Whenever I try to call my virtual host http://test I get the default Apache2 Index file that sits in the Ubuntu 14.04 seems to display the same message either way.

Comments
  • So you already did a2ensite test?
  • yes, sure. Sorry, forgot that.
  • Your vHost file and hosts files doesn't look bad for me.. Are you getting the apache default page when doing a2ensite test ? Are you able to find anything from logs ?
  • There is nothing in the logs. But I figured it out. See my own answer. Thanks.
  • This has worked for me. But I hate to have solutions without explanation. Why isn't Apache following my virtual host and instead loading the default virtual host? Totally doesn't make sense.
  • This is the correct solution. Please add some details about it.
  • Usually apache2 on Ubuntu has a virtual host setup for the default site, i.e. when apache can't find a more specific domain (TLD) attached to another virtual host. The command a2dissite SITE_NAME is a script for disabling any enabled site on Ubuntu/Debian systems. The opposite command is a2ensite for enabling a disabled site. These commands can only be executed with privileged rights, i.e. sudo. It also works with Ubuntu current 16.04LTS and 17.04.
  • worked for me. I think the damn * in the <virtual host> is the problem...
  • Nope. I have the reverse problem. I have <VirtualHost *:443> and it still is processing e.conf instead. This makes sense as e.conf is first alphabetically, but what baffles me is that it didn't stop ssl.conf from being processed first before I installed letsencrypt serverfault.com/questions/1013270/…
  • Without sudo, apache throw no exception. So you don't know if you reloaded properly your conf. Thank you.