Multiple Subdomains in Apache 2.4 – Configuration Guide

apache-2.4subdomain

I am not much of an admin (nor of an native english speaker, for that matter) and I am struggling with a simple task: add a subdomain on Apache 2.4. Here are my conf files:

<VirtualHost *:80>
    ServerName domain.io
    ServerAlias domain.io
    ServerAlias *.domain.io
    DocumentRoot /var/www/html/david
    ErrorLog /var/log/apache2/error.example.com.log
    CustomLog /var/log/apache2/access.example.com.log combined
</VirtualHost>

And the second:

<VirtualHost *:80>
    ServerName sub.domain.io
    ServerAlias sub.domain.io
    ServerAlias *.sub.domain.io
    DocumentRoot /var/www/html/ness_pro
    <Directory /var/www/html/ness_pro>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
    ErrorLog /var/log/apache2/error.example.com.log
    CustomLog /var/log/apache2/access.example.com.log combined
</VirtualHost>

[the problem] Every time I try to access sub.domain.io with a browser, I end up on the page related to domain.io. I followed the guidelines of apache 2.4 (i.e. edit in sites-available -> a2ensite -> systemctl restart).

[about my DNS conf] I redirected sub.domain.io to domain.io on my DNS provider (Gandi). It points to the right url. I naively thought that apache would analyze the request url to dispatch between the different VHosts but it seems that I've been naive. Is there some easy way to fix this (I always found apache url rewriting pretty obscure)?

[env] Debian 9 (raspbian) // Apache 2.4

[edit] Since everyone is asking. Here is my DNS record

@ 10800 IN SOA ns1.gandi.net. hostmaster.gandi.net. 1542647212 10800 3600 604800 10800
@ 10800 IN A 77.193.111.117
@ 10800 IN MX 10 spool.mail.gandi.net.
@ 10800 IN MX 50 fb.mail.gandi.net.
@ 10800 IN TXT "v=spf1 include:_mailcust.gandi.net ?all"
blog 10800 IN CNAME blogs.vip.gandi.net.
sub 10800 IN CNAME webredir.vip.gandi.net.
webmail 10800 IN CNAME webmail.gandi.net.
www 10800 IN CNAME webredir.vip.gandi.net.

Best Answer

This may not answer your question, but, I prepared your same scenario, used your same files changing only the DocumentRoot and the order is reversed as I told you in the comments:

# /etc/apache2/sites-enabled/000-domain.io.conf
<VirtualHost *:80>
    ServerName sub.domain.io
    ServerAlias sub.domain.io
    ServerAlias *.sub.domain.io
    DocumentRoot /var/www/html/serverfault/sub
    <Directory /var/www/html/serverfault/sub>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
    ErrorLog /var/log/apache2/error.example.com.log
    CustomLog /var/log/apache2/access.example.com.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerName domain.io
    ServerAlias domain.io
    ServerAlias *.domain.io
    DocumentRoot /var/www/html/serverfault/parent
    ErrorLog /var/log/apache2/error.example.com.log
    CustomLog /var/log/apache2/access.example.com.log combined
</VirtualHost>

Created a test pages in each DocumentRoot:

echo "parent" > /var/www/html/serverfault/parent/index.html echo "sub" > /var/www/html/serverfault/sub/index.html chown www-data:www-data /var/www/html/serverfault/ -R

To simulate the DNS I edited the /etc/hosts file including:

# ...
127.0.0.1   domain.io
127.0.0.1   sub.domain.io
127.0.0.1   www.domain.io
127.0.0.1   www.sub.domain.io
# ...

Restarted Apache: systemctl restart apache2

And it worked as expected: Querying the virtualhosts

Moreover, using the "Tamper Data" plugin for firefox I started manipulating the Host header on each request and the response changes in relation to it as expected.

I don't see why yours would not be working. To see if you have any outstanding alerts in the apache service you could use journalctl -u apache2

DNS

I used the DNS from my domain to be more realistic to your scenario and it worked the same way as using the hosts file, using CNAME or using A records, both of them worked correctly.

As a personal preference, I would say that the A record should be a name for the server, and then CNAMEs for the different host headers, something like (your server for me is 1.1.1.1):

webserver01 10800 IN A 1.1.1.1
www 10800 IN CNAME webserver01.domain.io.
sub 10800 IN CNAME webserver01.domain.io.

Or in your case you may point the CNAMES to @, where you set the IP:

@ 10800 IN SOA ns1.gandi.net. hostmaster.gandi.net. 1542647212 10800 3600 604800 10800
@ 10800 IN A 77.193.111.117

sub 10800 IN CNAME @
www 10800 IN CNAME @

I don't know if that syntax is valid, maybe:

sub 10800 IN CNAME domain.io.
www 10800 IN CNAME domain.io.

(mind the trailing .)

A third option would be generating all A records, pointing to the direct IP, maybe just harder to maintain in a bigger environment but equally effective:

sub 10800 IN A 1.1.1.1
www 10800 IN A 1.1.1.1

One thing for you to consider is that sub is not a subdomain of domain.io as you exposed it, it is only a host for domain.io. You should create a zone to make it a subdomain.

Gandi

I am pretty sure that webredir.vip.gandi.net. is the responsible for you not getting the expected behavior, if you see the documentation, the redirection type that you are using is the one that says:

When to use? For traditional web forwarding (ex. to a preexisting website, hosted on another page. This is commonly used to forward a domain to a blog or other free personal page that is already hosted at a given address.)

It looks like a basic redirection for the simplest hosting they offer, if you go to the item 3 in the same page, it says:

When to use? If your host gives you an IP address for your HTTP server (or if you host it yourself using Gandi's hosting), or tells you to make a particular “A”, “MX”, or “CNAME” (etc.) record in your zone file.

Which, from the information we have, I think is your case, isn't it?

I hope this information and testing helps

Related Topic