sub domain via apache

UPDATE 1:

I now have the following:

<VirtualHost *:80>
ServerName www.companyname.com
DocumentRoot /www/html/www.companyname.com/
</VirtualHost>

<VirtualHost *:80>
ServerName test.companyname.com
DocumentRoot /www/html/www.companyname.com/test/
</VirtualHost>

Which does not work when I type into the browser

http://test.companyname.com

However, when I restart apache, I get a message saying

Stopping httpd:                                            [  OK  ]
Starting httpd: [Tue Apr 05 10:49:48 2011] [warn] _default_ VirtualHost overlap on port 80, the first has precedence
                                                           [  OK  ]

ORIGINAL QUESTION:

How do I configure apache so I can have many sub domains along with my main domain?

for example, google has

www.google.com mail.google.com docs.google.com

how do I do the same, but get the sub domain to go to a different directory and or web page on the website?

We use a LAMP configuration on RedHat linux.


Solution 1:

Set up multiple VirtualHosts with a different ServerName and DocumentRoot for each.

<VirtualHost *:80>
    ServerName   www.example.com
    DocumentRoot /var/www/www.example.com/
    # ... other directives ...
</VirtualHost>

<VirtualHost *:80>
    ServerName   subdomain.example.com
    DocumentRoot /var/www/subdomain.example.com/
    # ... other directives ...
<VirtualHost>

I encourage you to have a look at your existing config files (which are under /etc/httpd/ on RedHat, I believe), and have also to have a look at Apache's documentation.