Search code examples
apachednssubdomainvirtual-server

Subdomain not working on Apache2/Debian9


I would like to set up a subdomain on my virtual server with Debian 9 and Apache 2 which points to a directory at /var/www/html/test. SSL and Let's Encrypt is also enabled, so the subdomain should be reached with https too.

My 000-default.conf file looks like:

<VirtualHost _default_:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html


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

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
<VirtualHost _default_:443>
        DocumentRoot /var/www/html
        ServerName www.example.com
        <IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=15768000; includeSubDomains; preload"
        </IfModule>
        SSLEngine on
        ServerAlias www.example.com
        SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

The file default-ssl.conf looks like:

<IfModule mod_ssl.c>
        <VirtualHost *:443>
                ServerAdmin webmaster@localhost

                DocumentRoot /var/www/html

                SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
                SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

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

                SSLEngine on

                <FilesMatch "\.(cgi|shtml|phtml|php)$">
                                SSLOptions +StdEnvVars
                </FilesMatch>
                <Directory /usr/lib/cgi-bin>
                                SSLOptions +StdEnvVars
                </Directory>

                ServerName www.example.com
                ServerAlias example.com
        </VirtualHost>
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

I made a new copy of the 000-default.conf file, named it "test.example.com.conf" and enabled it by means of a2ensite. The file looks like:

<VirtualHost _default_:80>

        ServerAdmin [email protected]
        DocumentRoot /var/www/html/test

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

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

<VirtualHost _default_:443>
        DocumentRoot /var/www/html/test
        ServerName test.example.com
        <IfModule mod_headers.c>
                Header always set Strict-Transport-Security "max-age=15768000; includeSubDomains; preload"
        </IfModule>
        SSLEngine on
        ServerAlias test.example.com
        SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

My hosts file includes the entry SERVER_IP_ADDRESS test.example.com.

The DNS server has an "A"-Entry with test.example.com pointing on SERVER_IP_ADDRESS.

Pinging test.example.com from another machine ends in "Host not found" and a ping on the virtual server results in an response from itself.

So why does the webbrowser only show "Server not found"? Did I forget something? Is something in my config wrong?


Solution

  • After long testing and research I found the mistake! There were 3:

    1. The DNS A-entry took some time to complete
    2. my .htaccess forwarding caused the URL always to be rewritten to www.example.com, no matter which subdomain was typed in
    3. my test.example.com.conf file has a mistake in it which makes the subdomain only available via https: the ServerAlias instruction was missing inside <VirtualHost _default_:80>

    So it works so far and needs some improvements...

    I hope I will help someone else with my mistakes!