2

I have a VPS, that I have configured to host multiple websites with name based hosting. It is all good while only using example.com, and www.example.com. It also works with example.net, but when I try example.net, it reverts to my default site configuration, which just shows my default (empty) index.html page.

Here's the default file:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Here's a configuration for the example.com site:

    <VirtualHost *:80>
     ServerAdmin [email protected]
     ServerName example.com
     ServerAlias www.example.com
     DocumentRoot /srv/www/example.com/public_html/
     ErrorLog /srv/www/example.com/logs/error.log
     CustomLog /srv/www/example.com/logs/access.log combined
<Directory /srv/www/example.com/public_html/>
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>

And here is the config for the example.net site:

<VirtualHost *:80>
     ServerAdmin [email protected]
     ServerName example.net
     ServerAlias www.example.net
     DocumentRoot /srv/www/example.net/public_html/
     ErrorLog /srv/www/example.net/logs/error.log
     CustomLog /srv/www/example.net/logs/access.log combined
<Directory /srv/www/example.net/public_html/>
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>

Where could the problem be?

I believe, that there is something going wrong with the ServerAlias property.

Could it be because of the way the site's are built? Because example.com is a Joomla site, and example.net is a Zend Framework site. Just in case, I'll also insert the .htaccess files for example.net, since example.com has it's disabled: example.net:

SetEnv APPLICATION_ENV development

RewriteRule  ^(browse|config).*  - [L]
ErrorDocument 500 /error-docs/500.shtml
SetEnv CACHE_OFFSET 2678400


<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Fri, 25 Sep 2037 19:30:32 GMT"
Header unset ETag
FileETag None
</FilesMatch>

RewriteEngine On
RewriteRule ^(adm|statistics) - [L]
RewriteRule ^/public/(.*)$ http://example.net/$1 [R]
RewriteRule ^(.*)$ public/$1 [L]

Any help would be greatly appreciated!

Edit So that my question is ABSOLUTELY clear: The problem is, that one site works with both www prefix as well as without it, and the second one does not. I would like to know how to enable the second site to work with www prefix as well.

4
  • Did you restart apache? What do the error_log and access_log say?
    – dmah
    Dec 13, 2010 at 16:12
  • Your question contains a real emailaddress as well as a domain. You'd better remove it. Please avoid using example2.com and use example.com, example.net or example.org instead. How did you include your configuration files?
    – Lekensteyn
    Dec 13, 2010 at 16:45
  • Sorry, wasn't intentional, fixed! Since I'm using an Ubuntu server, I included them with a2ensite example.net;a2ensite example.com . Did a server restart after that Dec 13, 2010 at 17:08
  • In my case it was because I copied the files and changed everything except ServerName was the same as my other, so the 2nd was redirecting to the first
    – DWils
    Mar 29, 2019 at 5:57

3 Answers 3

3

Make sure that the order of precedence in configuration directives matches how Apache should handle the request - i.e. you won't have much luck if your directives load like this:

  • example.com
  • default/catch-all
  • universal.lv

Doesn't matter whether they are declared in a single file or multiple files - so long as "universal.lv" is last, it will not be reached because the default/catch-all vhost will precede it.

I usually name my default/catch-all config "z-default.conf" (or, if you have a domain that starts with a "z", "zz" ... etc) for this reason, to ensure that it is processed only if no other vhost matches.

Try renaming accordingly.


Updated: So as not to confuse w/the _default_ VirtualHost directive

Using wildcard DNS to direct all requests to example.com to the server's IP (catches mis-keyed subdomains), Apache parses the following configuration files:

  • 000-default.conf - VirtualHost _default_:80 / ServerName <hostname> - catch requests against the server's IP (can redirect to primary domain or simply block with the default Directory specified in conf.d/security)
  • example.conf - VirtualHost *:80 / ServerName www.example.com
  • example2.conf - VirtualHost *:80 / ServerName www.example2.com
  • zzz-example.conf - VirtualHost *:80 / ServerName example.com / ServerAlias *.example.com - redirects to canonical www domain
  • zzz-example2.conf - VirtualHost *:80 / ServerName example2.com / ServerAlias *.example2.com - redirects to canonical www domain

As documented, Apache selects the first matching VirtualHost (i.e. _default_:80) but this behavior is not desirable if you are using a wildcard in a ServerAlias directive.

2
  • Oh, ok, so the precedence is measured by the name of the file? I didn't know this. Will try in a sec. Dec 13, 2010 at 17:21
  • So, this is the right answer. Thanks for showing me the way precedence works on Ubuntu with Apache2, I knew that there was something like this while I was working on Windows, but thought, that there is some clever mechanism in place on Linux, that took care of this by itself. Thanks, helped! Dec 13, 2010 at 17:25
1

I recommend you to read the Apache documentation on Virtual Hosts first.

An excellent example has been given on http://httpd.apache.org/docs/2.2/vhosts/name-based.html:

NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>

<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>

Note the NameVirtualHost part. Your error log (/var/log/apache2/error.log) should have warned you that you've made a possible mistake.

Note: after a change to the configuration file, Apache needs to be restarted.

1
  • Nope, there are no errors in the log file. So practically, I should have made my serverName with the www and have used the *. wildcard and the prefix-less domain name with the ServerAlias? Dec 13, 2010 at 17:03
0

you need to have NameVirtualHost enabled into httpd.conf - then you can define multiple virtual host on the same ip:port.

1
  • This has been done, and is not the issue. I have restated and clarified my problem! Dec 13, 2010 at 16:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .