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>
The Apache HTTP server supports virtual hosts, meaning that it can respond to requests that are directed to multiple IP addresses or host names that correspond to the same host machine. You can configure each virtual host to provide different content and to behave differently.
Create a Virtual Hosts By default on Ubuntu systems, Apache Virtual Hosts configuration files are stored in /etc/apache2/sites-available directory and can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory.
I was facing this issue, and it turned out I had to disable the default virtual host.
sudo a2dissite 000-default.conf
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-
.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With