Different subdomains, apache responding with the same vhost

apache-2.4domain-name-systemsubdomainubuntu-14.04virtualhost

I have a VPS in Digital Ocean, running ubuntu 14.04 and apache 2.4.7, DNS is also managed in DO.

I have one domain let's call it thedomain.com I've been trying to configure 3 subdomains along with the main domain (thedomain.com, calis.thedomain.com, orlybg.thedomain.com, fubar.thedomain.com).

I haven't been able to correctly configure my apache's virtualhosts, in the browser no matter to what domain/subdomain, I go I always get responded by the same vhost (I can tell by the apache logs) and the otput in the browser.

The main domain and the 3 subdomains are A records www is a CNAME, here's my zonefile:

$ORIGIN thedomain.com.
$TTL 1800
thedomain.com. IN SOA ns1.digitalocean.com. hostmaster.thedomain.com. 1422028092 10800 3600 604800 1800
thedomain.com. 1800 IN NS ns1.digitalocean.com.
thedomain.com. 1800 IN NS ns2.digitalocean.com.
thedomain.com. 1800 IN NS ns3.digitalocean.com.
thedomain.com. 1800 IN A 104.236.80.93
www.thedomain.com. 1800 IN CNAME thedomain.com.
orlybg.thedomain.com. 1800 IN A XXX.XXX.XX.XX
fubar.thedomain.com. 1800 IN A XXX.XXX.XX.XX
calis.thedomain.com. 1800 IN A XXX.XXX.XX.XX

I have the main host and the 3 subdomains as 4 separated virtualhost files, I have these 3 enabled:

calis.thedomain.com.conf

<VirtualHost *:80>
    ServerName calis.thedomain.com


    DocumentRoot /var/www/html/calis
    <Directory /var/www/html/calis>
        # enable the .htaccess rewrites
        AllowOverride All
        Require all granted

    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error_calis.log
    CustomLog ${APACHE_LOG_DIR}/access_calis.log combined

</VirtualHost>

thedomain.com.conf

<VirtualHost *:80>
    ServerName thedomain.com


    DocumentRoot /var/www/html/thedomain
    <Directory /var/www/html/thedomain>
        # enable the .htaccess rewrites
        AllowOverride All
        Require all granted

    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

orlybg.thedomain.com.conf

<VirtualHost *:80>
    ServerName orlybg.thedomain.com

    DocumentRoot /var/www/html/myproject
    <Directory /var/www/html/myproject>
        # enable the .htaccess rewrites
        AllowOverride All
        Require all granted

    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error_orlybg.log
    CustomLog ${APACHE_LOG_DIR}/access_orlybg.log combined

</VirtualHost>

I use a2ensite to enable the vhosts, I have vhost_alias.load enabled, I restart/reload apache in several ways, and no luck. I see tha the NameVirtualHost directive is not used in my version of apache, I used to have a conf file with ServerName localhost to not see the " Could not reliably determine the server's fully qualified domain name" error but I disabled it.

apachectl -S

VirtualHost configuration:
*:80                   is a NameVirtualHost
     default server calis.thedomain.com (/etc/apache2/sites-enabled/calis.thedomain.com.conf:1)
     port 80 namevhost calis.thedomain.com (/etc/apache2/sites-enabled/calis.thedomain.com.conf:1)
     port 80 namevhost thedomain.com (/etc/apache2/sites-enabled/thedomain.com.conf:1)
     port 80 namevhost orlybg.thedomain.com (/etc/apache2/sites-enabled/orlybg.thedomain.com.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
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

I don't have a clue on what to try next, I've been trying for days. I'd really appreciate any help, thanks!

Best Answer

I have two suggestions apart from this issue

  1. Add ServerName with the FQDN of the host in which the Apache is serving the request. This will help you avoid lots of trouble.
  2. If all of the sub-domains have different IP address then use IP based virtualization instead of Name based.

Your VirtualHost configuration looks perfect and it doesn't look creating the issue.

Apache document states that.

If no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used.

Generally different VirtualHost sections are selected based on the Host: HTTP header and if no VirtualHost section matches the Host: header then the first section will be considered the default and it will be served. And which inturn depends on how you make a request in the browser. You check it with curl command to be sure what is causing the issue like below.

$ curl -L http://localhost -H 'Host: thedomain.com'

This should return documents from /var/www/html/thedomain.

$ curl -L http://localhost -H 'Host: orlybg.thedomain.com'

This should return documents from /var/www/html/myproject.

Here with -H option we can send custom http header and so here we are using it to send Host: http header.