I am setting up an apache2 server with a number of virtualhosts.
They are all working except for www.mydomain.com, which goes to the default virtualhost instead of the virtualhost I created for it.
The virtualhost file (www.mydomain.com.conf) looks like this:
<VirtualHost *:80>
ServerName www.mydomain.com
ServerAdmin [email protected]
DocumentRoot /var/www/mydomain.com/www
ErrorDocument 404 "/error.php"
</VirtualHost>
If I change ServerName
to www2.mydomain.com
it works. If I change it back to www.mydomain.com
it goes to the default virtualhost.
The default virtual host file (000-default.conf) looks like this:
<Virtualhost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/mydomain.com/html
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/mydomain.com/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
One other thing to note, that may be affecting this, is that the hostname of the server (/etc/hostname) is www.mydomain.com
Is www.
a special case? Why is this happening and how do I make it work like I want it to?
I am using Ubuntu 16.04, Apache2 Server version: Apache/2.4.18 (Ubuntu)
EDIT: I have confirmed that the problem was /etc/hostname having www.mydomain.com - I changed it to www-server.mydomain.com and now the www virtualhost goes to the right place. I still don't know why this happens.
As I remember, if ServerName is not set, Apache use the server hostname as ServerName for default virtualhost, you should also see a warning about that on Apache logs or as output when you restart Apache.
You can also workaround it adding ServerName directive to default virtualhost or globally in Apache conf, but I think that is a best practice to name the server with a dedicated hostname like srv-something.example.com.
As to the issue of where the implied
ServerName
came from,In other issues, however, you need to look at your authorization scheme. It looks like you have not granted access to your new virtual host, while using outdated directives for the default.
You should ensure that
mod_authz_core
is enabled and add this to your new virtual host config:Also, all of that
Order allow,deny
andallow from all
jazz in your default config is deprecated Apache 2.2 stuff, and should be changed to use the Apache 2.4 Require directive.For security, your main
apache2.conf
(orsecurity.conf
, or where ever you want to handle these things) should include: