Sunday, May 24, 2009

Apache ignores DocumentRoot in VirtualHost

Lasts night I got stuck in configuring a virtual host on Apache 2.2. Regardless of which port I set (including the port 80), the Apache server ignored my DocumentRoot setting for the virtual host. Until I read this post, it reminded me to check all wildcard settings. The problem I had was slightly different from the one in the post.

In that post, the problem was from the wildcard setting on the ServerName. For me, I was missing a port number next to the wildcard when I was setting it at the VirtualHost directive for localhost. Everything works fine after the port number had been added.

Please don't make the same mistake as I did. Even you are using port 80, you should always put the port number next to it. Otherwise, your virtual DocumentRoot will NOT be recognized.

The followings are my virtual host settings in httpd-vhost.conf.

Incorrect Correct
NameVirtualHost *:9090

# Missing port number to localhost or the default site
<VirtualHost *>
  DocumentRoot "C:\servers\Apache Software Foundation\Apache2.2\htdocs"
  ServerName localhost
</VirtualHost>

<Directory "h:/vhosts">
  Order Deny,Allow
  Allow from all
  Options Indexes FollowSymLinks
</Directory>

<VirtualHost *:9090>
   # Note: no wildcard supported in ServerName
   ServerName examples.local
   ErrorLog "logs/examples.local-error.log"
   CustomLog "logs/examples.local-access.log" common
   DocumentRoot "h:/vhosts/examples"
</virtualHost>
NameVirtualHost *:9090

# Port number is given to the localhost or the default site
<VirtualHost *:9090>
  DocumentRoot "C:\servers\Apache Software Foundation\Apache2.2\htdocs"
  ServerName localhost
</VirtualHost>

<Directory "h:/vhosts">
  Order Deny,Allow
  Allow from all
  Options Indexes FollowSymLinks
</Directory>

<VirtualHost *:9090>
  # Note: no wildcard supported in ServerName
  ServerName examples.local
  ErrorLog "logs/examples.local-error.log"
  CustomLog "logs/examples.local-access.log" common
  DocumentRoot "h:/vhosts/examples"
</virtualHost>

No comments:

Post a Comment