I've got a few sites configured as VirtualHosts using Apache that have been working fine for a while. I recently changed the hostname of the server to one of the domain names that was being served, and also added it to /etc/hosts pointing to the server's external IP address.

Since I did that, accessing the domain from my laptop takes me to Apache's default "It works!" page. If I remove 000-default from /etc/apache2/sites-enabled/, it displays the correct site. Trying to access the site with the 'www.' prefix takes me to the correct site whether or not 000-default is in sites-enabled.

Based on all of that, I'm guessing the default config is serving up the default page because of how I've configured the hostname or /etc/hosts (or both), but I can't figure out how to disable that while leaving those settings as they are. Any suggestions welcome!

Edit - My current VirtualHost config looks like this:

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    ServerAdmin me@domain.com

    DocumentRoot /srv/www/domain

    ErrorLog /srv/www/domain/logs/error.log
    CustomLog /srv/www/domain/logs/access.log combined
</VirtualHost>

To summarize: I have several VirtualHosts set up, and this is the only one not working properly. I can access http://www.domain.com just fine, but http://domain.com takes me to Apache's default "It works!" page. If I disable the 000-default config it shows the proper page. This only started happening after I set the hostname of the server to domain.com and added a line to /etc/hosts pointing domain.com to the server's external IP address. I'd like to leave those that way if possible.

Edit: I ran apache2ctl -S since /usr/sbin/apache2 -S gave me "apache2: bad user name ${APACHE_RUN_USER}". Here's the output:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server domain.com (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost domain.com (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost other.com (/etc/apache2/sites-enabled/other:1)
         port 80 namevhost another.com (/etc/apache2/sites-enabled/another:1)
         port 80 namevhost domain.com (/etc/apache2/sites-enabled/domain:1)
Syntax OK
link|improve this question
is domain.com being used on the main httpd.conf file as ServerName ? or apache2ctl.conf you seem to be using ubuntu so it might differ the name. – Prix Oct 19 '10 at 17:34
feedback

3 Answers

up vote 1 down vote accepted

Lost this question the other day.

When you have a <VirtualHost> container without a ServerName component, it will use the default hostname. As 000-default probably doesn't have a ServerName component, and is the first alphabetically to be included, that is why it answers for hostname.com and doesn't allow your explicitly declared hostname to work.

link|improve this answer
I see, any way to fix that? I want the default config to handle any unhandled domains, but I don't want it to handle that particular domain. – John Debs Oct 19 '10 at 18:24
give it a domain other than one that is used in a config, or rename it to zzz-default so that it is called last. – karmawhore Oct 19 '10 at 18:59
Okay, I think I understand it all now. I tried zzz-default but it's not the exact behavior I wanted. Setting ServerName example.com in the default config works great, though. Thanks! – John Debs Oct 19 '10 at 20:15
feedback

Do you have a NameVirtualHost *:80 directive somewhere?

Try:

/usr/sbin/apache2 -S

That will tell you how apache is parsing the config file. Do you have configs that have actual IPs or all have the *? Do you have any missing the :80? Apache doesn't like mixing those.

Do you have a ServerName domain.com or ServerAlias domain.com in default?

link|improve this answer
Yup, I've got it in my /etc/apache2/posts.conf. I'll add a summary to the question that hopefully makes it more clear. – John Debs Oct 15 '10 at 5:05
Thanks, updated question with output. – John Debs Oct 15 '10 at 5:17
My 000-default config is untouched, it has no ServerName or ServerAlias directives. – John Debs Oct 15 '10 at 5:24
feedback

Check ServerName and ServerAlias in VirtualHost directive:

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

Read: http://httpd.apache.org/docs/2.2/mod/core.html#serveralias, http://httpd.apache.org/docs/2.2/vhosts/

link|improve this answer
That's basically how things are set up now. I posted my config in the question. – John Debs Oct 15 '10 at 4:59
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.