10

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 [email protected]

    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
2
  • 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, 2010 at 17:34
  • we had a very similar issue, apache2ctl -S showed us that our default server for *.443 was our troublesome domain, but we also found it listed in /etc/resolv.conf under search troublesome-domain.com
    – Pete
    Feb 3, 2021 at 11:27

5 Answers 5

14

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.

4
  • 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, 2010 at 18:24
  • 1
    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, 2010 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, 2010 at 20:15
  • @karmawhore - renaming to zzz is great. Let's me keep default as a fallback but stops it from eclipsing all of the other vhosts with ServerName defined. Thanks for your 13 year old comment :-) Nov 29, 2023 at 2:30
4

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?

3
  • 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, 2010 at 5:05
  • My 000-default config is untouched, it has no ServerName or ServerAlias directives.
    – John Debs
    Oct 15, 2010 at 5:24
  • NameVirtualHost is deprecated.
    – Sam
    Jan 16, 2015 at 6:31
0

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/

1
  • 1
    That's basically how things are set up now. I posted my config in the question.
    – John Debs
    Oct 15, 2010 at 4:59
0

This worked:

<VirtualHost *:80>
    DocumentRoot /var/www/www.mydomain.com
    ServerName mydomain.com
    ServerAlias www.mydomain.com
</VirtualHost>

when I had a typo, it did NOT work (the ServerName directive twice):

<VirtualHost *:80>
    DocumentRoot /var/www/www.mydomain.com
    ServerName mydomain.com
    ServerName www.mydomain.com
</VirtualHost>

When I had the ServerName directive twice, it kept resolving to the first site listed (not shown here).

hope this helps somebody...

0

I had a similar problem, and fixed it by explicitly setting a ServerName in the main httpd.conf file, rather than letting apache automatically figure out it was. I assume that what apache decided was the same as my vhost (defined later), so the default one "won", overriding the vhost setting.

I just set the httpd.conf ServerName to a hostname that machine has, I'll never serve webpages for.

You must log in to answer this question.

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