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 info@mydomain.com
    DocumentRoot /var/www/mydomain.com/www
    ErrorDocument 404 "/error.php"

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 info@mydomain.com
    DocumentRoot /var/www/mydomain.com/html
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    <Directory /var/www/mydomain.com/html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    ErrorLog /var/log/apache2/error.log
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined

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,

If no ServerName is specified, the server attempts to deduce the client visible hostname by first asking the operating system for the system hostname, and if that fails, performing a reverse lookup on an IP address present on the system.

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:

 <Directory /var/www/mydomain.com/www>
   Require all granted

Also, all of that Order allow,deny and allow 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 (or security.conf, or where ever you want to handle these things) should include:

 <Directory />
   Require all denied

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.