Configuring subdomain using apache virtualhost problem

7,706

Solution 1

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

<VirtualHost *:80>

Solution 2

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.

Share:
7,706

Related videos on Youtube

Admin
Author by

Admin

Updated on September 17, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

    • dunxd
      dunxd over 14 years
      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?
    • Admin
      Admin over 14 years
      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.
    • Urda
      Urda over 14 years
      VirtualMe, did you try setting it up as in my example? Be sure to reboot apache2 of course!
  • Urda
    Urda over 14 years
    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!
  • Mattias
    Mattias over 14 years
    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