1

I have a Debian 6 server that was configured by somebody else. I was asked to add a subdomain on that server.

I configured the DNS correctly (checked with nslookup), created the directory in /var/www/ and then added new virtual host to /etc/apache2/sites-available (in fact I copied one of the existing subdomain's file, and edited it):

<VirtualHost *:80>
    DocumentRoot "/var/www/mysub.example.com"
    ServerName mysub.example.com
    ServerAlias www.mysub.example.com

    ErrorLog ...[path to file]
    CustomLog [path to file]

    RewriteEngine On 
    #RewriteCond %{HTTP_HOST} ^mysub.example.com$ [NC]
    #RewriteRule ^(.*)$ http://www.mysub.example.com$1 [R=301,L]

    <Directory /var/www/mysub.example.com>
         AllowOverride All
         Require all granted
    </Directory>
</VirtualHost>

Then I added my host using a2ensite and restarted Apache but domain mysub.example.com is redirecting to beta.example.com which is our beta production site. What is weird, there is no configuration for beta.example.com in /etc/apache2/sites-available.

What else should I do to enable this subdomain? I'm pretty sure that the configuration for example.com host is not blocking anything, because we have also a blog.example.com virtual host configured in that way and it is working perfectly.

EDIT

Some debug outputs:

root@xxx:~# apachectl -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:443                  beta.example.com (/etc/apache2/sites-enabled/01-example.com:18)
*:80                   is a NameVirtualHost
         default server beta.example.com (/etc/apache2/sites-enabled/01-example.com:1)
         port 80 namevhost beta.example.com (/etc/apache2/sites-enabled/01-example.com:1)
         port 80 namevhost blog.example.com (/etc/apache2/sites-enabled/02-blog.example.com:1)
         port 80 namevhost [here comes other unrelated domain name]
         port 80 namevhost forum.example.com (/etc/apache2/sites-enabled/04-forum.example.com:1)
         port 80 namevhost example.com (/etc/apache2/sites-enabled/05-sth.example.com:1)
         port 80 namevhost mysub.example.com (/etc/apache2/sites-enabled/06-mysub.example.com:1)
Syntax OK


root@xxx:~# grep -r 'mysub\.example\.com' /etc/apache2
/etc/apache2/apache2.conf:Alias /mysub /var/www/mysub.example.com
/etc/apache2/sites-available/06-mysub.example.com:        DocumentRoot "/var/www/mysub.example.com"
/etc/apache2/sites-available/06-mysub.example.com:        ServerName mysub.example.com
/etc/apache2/sites-available/06-mysub.example.com:        ServerAlias www.mysub.example.com
/etc/apache2/sites-available/06-mysub.example.com:        ErrorLog "/var/log/apache2/mysub.example.com-error_log"
/etc/apache2/sites-available/06-mysub.example.com:        CustomLog "/var/log/apache2/mysub.example.com-access_log" common
/etc/apache2/sites-available/06-mysub.example.com:        #RewriteCond %{HTTP_HOST} ^mysub.example.com$ [NC]
/etc/apache2/sites-available/06-mysub.example.com:        #RewriteRule ^(.*)$ http://www.mysub.example.com$1 [R=301,L]
/etc/apache2/sites-available/06-mysub.example.com:        <Directory /var/www/mysub.example.com>

root@xxx:~# grep -r 'beta\.example\.com' /etc/apache2
/etc/apache2/sites-available/01-example.com:        ServerName beta.example.com
/etc/apache2/sites-available/01-example.com:        ServerAlias www.beta.example.com
/etc/apache2/sites-available/01-example.com:        ErrorLog "/var/log/apache2/beta.example.com-error_log"
/etc/apache2/sites-available/01-example.com:        CustomLog "/var/log/apache2/beta.example.com-access_log" common
/etc/apache2/sites-available/01-example.com:        ServerName beta.example.com:443
/etc/apache2/sites-available/01-example.com:        ServerAlias www.beta.example.com:443
/etc/apache2/sites-available/01-example.com:        ErrorLog "/var/log/apache2/beta.example.com-error_log"
/etc/apache2/sites-available/01-example.com:        CustomLog "/var/log/apache2/beta.example.com-access_log" common

EDIT 2 I made a quick test. I disabled first virtual host (beta.example.com) with:

a2dissite 01-example.com

And then tried to access mysub.example.com and I was now redirected to blog.example.com - which is second one in order of sites enabled. So it looks like mysub.example.com is not being found and it falls into first bin of *.example.com subdomains it finds.

EDIT 2

Another test: I changed order of my configuration files so that my new subdomain would be the first (default) one. Right now:

root@xxx:~# apachectl -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:443                  beta.example.com (/etc/apache2/sites-enabled/01-example.com:14)
*:80                   is a NameVirtualHost
         default server mysub.example.com (/etc/apache2/sites-enabled/00-mysub.example.com:1)
         port 80 namevhost mysub.example.com (/etc/apache2/sites-enabled/00-mysub.example.com:1)
         port 80 namevhost beta.example.com (/etc/apache2/sites-enabled/01-example.com:1)
         port 80 namevhost blog.example.com (/etc/apache2/sites-enabled/02-blog.example.com:1)
Syntax OK

And still, if I try to enter mysub.example.com I am being redirected to beta.example.com. The blog.example.com still works perfectly fine.

Is there a chance there is a domain configuration defined somewhere else (besides the /etc/apache2/sites-available directory)?

EDIT 3

Additional configurations for hosts: beta.example.com and blog.example.com

beta.example.com (file: /etc/apache2/hosts-available/01-example.com )

<VirtualHost *:80>
        DocumentRoot "/var/www/example.com"
        ServerName beta.example.com
        ServerAlias www.beta.example.com

        ErrorLog "/var/log/apache2/beta.example.com-error_log"
        CustomLog "/var/log/apache2/beta.example.com-access_log" common

        <Directory /var/www/example.com>
                AllowOverride All
        </Directory>
</VirtualHost>

<VirtualHost *:443>
        DocumentRoot "/var/www/example.com"
        ServerName beta.example.com:443
        ServerAlias www.beta.example.com:443

        ErrorLog "/var/log/apache2/beta.example.com-error_log"
        CustomLog "/var/log/apache2/beta.example.com-access_log" common

        RewriteEngine On
        #RewriteCond %{HTTP_HOST} ^example.com$ [NC]
        #RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]

        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key

        <Directory /var/www/example.com>
                AllowOverride All
        </Directory>
</VirtualHost>

blog.example.com (file: /etc/apache2/hosts-available/02-blog.example.com )

<VirtualHost *:80>
    DocumentRoot "/var/www/blog.example.com"
    ServerName blog.example.com
    ServerAlias www.blog.example.com

    ErrorLog ...[path to file]
    CustomLog [path to file]

    RewriteEngine On 
    #RewriteCond %{HTTP_HOST} ^blog.example.com$ [NC]
    #RewriteRule ^(.*)$ http://www.blog.example.com$1 [R=301,L]

    <Directory /var/www/blog.example.com>
         AllowOverride All
         Require all granted
    </Directory>
</VirtualHost>
17
  • 3
    The directory you should be looking in is: /etc/apache2/sites-enabled.
    – EEAA
    May 14 '15 at 14:23
  • and after a2ensite my configuration is enabled there.
    – Gacek
    May 14 '15 at 14:24
  • If you're being redirected to beta.example.com, then it probably has something to do with a rewrite/redirect rule or the default site on your server.
    – Hyppy
    May 14 '15 at 14:50
  • I checked - there's no redirection there.
    – Gacek
    May 15 '15 at 11:30
  • 1
    try apachectl -S or apache2ctl -S
    – c4f4t0r
    May 16 '15 at 23:15
2

I advice you debug all your files, as c4f4t0r said try "apache2ctl -S" and after that try to find rewrite rules inside .htaccess files, for example:

find /var/www/mysub.example.com -name ".htaccess" -exec grep -l -i rewrite {} \;

In your main configuration file you have something "dangerous":

<Directory /var/www/mysub.example.com>
     AllowOverride All # <---- THIS
     Require all granted 
</Directory>
1
  • There's nothing in .htaccess files, in fact these directories contain one html file right now
    – Gacek
    May 17 '15 at 10:23
2

You should check the file /etc/apache2/ports.conf and look at the line NameVirtualHost *:80 near Listen 80

If you don't have this line, you can't use virtualhosts based on ServerName, so it is normal that you got always the first enabled virtualhost in your browser.

6
  • Both lines are located in ports.conf file. And why blog.example.com works, but my new subdomain not?
    – Gacek
    May 19 '15 at 15:02
  • And if you try to disable le vhost of blog.example.com ? Can you also post the content of the vhost for blog.example.com ?
    – lazzio
    May 19 '15 at 15:19
  • Added - se my edit
    – Gacek
    May 19 '15 at 19:16
  • Well, the only thing for me that can explain your problem is that your mysub.example.com subdomain DNS is not well pointing on your server. There are 2 potential reasons of this :
    – lazzio
    May 21 '15 at 12:25
  • - mysub.example.com subdomain is not configured in the right DNS server, which manager your domain example.com - Your server is not using a good DNS server
    – lazzio
    May 21 '15 at 12:37
1

OK, the problem (and solution) came out to be quite simple.

The problem was that the newly created subdomains' folders were empty - no index.php/html file in it. So the server, instead of showing it (as a dir), redirected to default domain - which was beta.example.com

So the configuration of vhosts was correct, and after adding some files to the folders, the subdomains strated to work.

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.