Apache2 VirtualHost with same name as machine

apache-2.4virtualhost

I am using Debian have server with canonical name (defined in /etc/hostname and in reverse DNS) server.example.com

root@server:~# uname -a
Linux server.example.com 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1 (2015-05-24) x86_64 GNU/Linux

Apache/2.4.10 (Debian) Server at server.example.com Port 80

Now I installed apache2 and did some basic configuration. Then I added VirtualHosts to /etc/apache2/sites-available with symlinks in sites-enabled. Everything is working fine except VirtualHost with ServerName server.example.com. When I try to access that URI in explorer, I get content of default virutal host (with no ServerName specified). If I change server.example.com.conf's ServerName to something different (like test.example.com) and reload apache configuration, then I can get to the URI with no problems. I would like to know what is wrong in having VirtualHost's ServerName same as canonical name of the machine.

Moreover, server.example.com.conf has DocumentRoot /var/www/subs/server which contains index.html and munin folder. Default VirtualHost has DocumentRoot /var/www/html which has no munin folder in it. However, when I try to access http://server.example.com/munin I can get into /var/www/subs/server/munin, while http://server.example.com/index.html brings me /var/www/html/index.html.

Lastly, when I put Redirect 404 / in default VirtualHost, I will get 404 when I try to access both http://server.example.com/ and http://server.example.com/munin

Added configuration of VirtualHosts

/etc/apache2/sites-available/forum.example.com.conf :

# Working subdomain configuration
<VirtualHost *:80>
        ServerName forum.example.com
        ServerAdmin zereges@example.com
        DocumentRoot /var/www/subs/forum
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

/etc/apache2/sites-available/server.example.com.conf :

# Not working server.example.com configuration
<VirtualHost *:80>
        ServerName server.example.com
        ServerAdmin zereges@example.com
        DocumentRoot /var/www/subs/server
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

/etc/apache2/sites-available/default.conf :

# Default VirtualHost configuration 
<VirtualHost *:80>
        ServerAdmin zereges@example.com
        DocumentRoot /var/www/html
        #Redirect 404 /
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

root@server:~# apache2ctl -t
Syntax OK

Added ouptut of root@server:~# apache2 -S

[Tue Aug 11 11:07:32.705684 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_LOCK_DIR} is not defined
[Tue Aug 11 11:07:32.706119 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_PID_FILE} is not defined
[Tue Aug 11 11:07:32.706250 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_RUN_USER} is not defined
[Tue Aug 11 11:07:32.706406 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_RUN_GROUP} is not defined
[Tue Aug 11 11:07:32.706566 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
(... repeating few more times)
[Tue Aug 11 11:07:32.744292 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_LOG_DIR} is not defined

AH00526: Syntax error on line 74 of /etc/apache2/apache2.conf:
Invalid Mutex directory in argument file:${APACHE_LOCK_DIR}

While,

/etc/apache2/apache2.conf:74:Mutex file:${APACHE_LOCK_DIR} default
/etc/apache2/envvars:21:export APACHE_LOCK_DIR=/var/lock/apache2$SUFFIX

After running /etc/apache2/envvar I get this by executing apache2 -S

VirtualHost configuration:
*:80     is a NameVirtualHost
         default server server.example.com (/etc/apache2/sites-enabled/default.conf:1)
         port 80 namevhost server.example.com (/etc/apache2/sites-enabled/default.conf:1)
         port 80 namevhost download.example.com (/etc/apache2/sites-enabled/download.example.com.conf:1)
         port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:1)
                 alias www.example.com
         port 80 namevhost forum.example.com (/etc/apache2/sites-enabled/forum.example.com.conf:1)
         port 80 namevhost forumold.example.com (/etc/apache2/sites-enabled/forumold.example.com.conf:1)
         port 80 namevhost forumtest.example.com (/etc/apache2/sites-enabled/forumtest.example.com.conf:1)
         port 80 namevhost mail.example.com (/etc/apache2/sites-enabled/mail.example.com.conf:1)
         port 80 namevhost ro.example.com (/etc/apache2/sites-enabled/ro.example.com.conf:1)
         port 80 namevhost server.example.com (/etc/apache2/sites-enabled/server.example.com.conf:1)
         port 80 namevhost anotherexample.com (/etc/apache2/sites-enabled/anotherexample.com.conf:1)
                 alias www.anotherexample.com
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

Best Answer

On debian based distributions, the default virtualhost specifies no ServerName at all, which effectively means it ends up with the systems hostname as its ServerName.

Since it's listed first, it's going to cover up any later vhost with that same ServerName.

I'd suggest disabling the shipped default virtual host and adding your own that speecifies a servername but doesn't use the ServerName you're interested in.