2

I'm trying to use a virtualhost in my Apache configuration to setup a subdomain. My settings are:

NameVirtualHost *:80

<VirtualHost *>
    ServerName www.website.com
    DocumentRoot "/var/www/html"
</VirtualHost>

<VirtualHost *>
    ServerName wiki.website.com
    DocumentRoot "/var/www/dekiwiki"
</VirtualHost>

When I restart apache, both www.website.com and wiki.website.com go to the main website in /var/www/html.

Any help appreciated.

3
  • 1
    That looks ok to me. The config file you are using is definitely the one that Apache is using right? You've cleared the cache in your browser? You haven't set ServerName or ServerAlias anywhere else in the config?
    – dunxd
    Feb 11, 2010 at 14:18
  • Yes, because if I alter it to just having the wiki virtualhost, it appears. And if I change the order to having the wiki first, it only shows the wiki. No, the servername/alias are commented out of the main config. I can only get it to show either the wiki or the normal website at the moment.
    – VirtualMe
    Feb 11, 2010 at 14:58
  • VirtualMe, did you try setting it up as in my example? Be sure to reboot apache2 of course!
    – Urda
    Feb 11, 2010 at 15:15

4 Answers 4

2

In VirtualHost have to use the same signature as in NameVirtualHost:

<VirtualHost *:80>
1
  • Thanks very much! I discovered this a few moments ago through searching a different forum. A relief that it's working now. I realised that the virtualhost wasn't being picked up when running httpd -S
    – VirtualMe
    Feb 11, 2010 at 15:24
1

Here is how I do subdomains on my apache2 server...

NameVirtualHost *:80

# Many other hosts...

<VirtualHost servers.international-anarchy.com>
    ServerName servers.international-anarchy.com
    DocumentRoot "/http/international-anarchy.com/servers/"

    <Directory "/http/international-anarchy.com/servers/">
        Options +Indexes FollowSymLinks
        AllowOverride None
        Allow from all
        Order allow,deny
    </Directory>
</VirtualHost>

<VirtualHost status.international-anarchy.com>
    ServerName status.international-anarchy.com
    DocumentRoot "/http/international-anarchy.com/status/"

    <Directory "/http/international-anarchy.com/status/">
        Options +Indexes FollowSymLinks
        AllowOverride All
        Allow from all
        Order allow,deny
    </Directory>
</VirtualHost>

That is how I get apache2 to render the subdomains for each host correctly, and I place all of these settings in a virtual hosts file that is included directly from the main apache config.

1
  • I prefer this method, as each virtual host declaration has the hostname built right into it, instead of a dozen <VirtualHost *> lines all over your config files. I also group the V-Hosts by domain name, then subdomain, all in alphabetical order... asite.com wiki.asite.com bsite.com wiki.bsite.com and so on!
    – Urda
    Feb 11, 2010 at 15:15
0

I found the problem. As the virtualhost was set to:

NameVirtualHost *:80

I needed to set the port in the VirtualHost directives also. So

<VirtualHost *:80>

rather than just

<VirtualHost *:80>

Using 'httpd -S' command showed me that the virtualhost wasn't being picked up

0

Do you have any Listen directives after the one for port 80?

If so, you may need to change <VirtualHost *> to <VirtualHost *:80>

You can specify a :port to change the port that is matched. If unspecified then it defaults to the same port as the most recent Listen statement of the main server. You may also specify :* to match all ports on that address

from http://httpd.apache.org/docs/2.2/mod/core.html#virtualhost

You must log in to answer this question.