How to create subdomains on a name-based virtualhost?

27

13

I have name-based hosting, lets call it my.address.com. My friend has the 'plain' address address.com and gave me a virtual machine that I can access via SSH.

He also made some magic with ports and now, after installing Apache, I could access /var/www through my.address.com.

So, the question is, how can I make a subdomain like test.my.address.com?

There should be some magic with Apache's Listen Directive, but I cant figure it out. Also, I know how to create local-available sites with Apache, but it is not the same.

scythargon

Posted 2012-07-29T11:46:47.987

Reputation: 646

You won't need Listen, look at Apaches virtual hosts: http://httpd.apache.org/docs/2.2/vhosts/

– Der Hochstapler – 2012-07-29T11:49:40.020

the main question is remained - what to write in ServerName directive? – scythargon – 2012-07-29T12:16:13.233

Answers

33

To be able to create subdomains, you'll have to make sure of several things:

DNS

So other's can reach test.my.address.com, they first have to be able to resolve that name to the IP address of your virtual machine. How can that be done?

What your friend most likely already did, was to create an A record in the address.com zone, which points my to the IP address of your VM.

So far, so good. But what about test.my? It might not have an entry yet.

Your friend could simply add *.my to the zone as well (and point it to the same IP address). Then all request to something.my.address.com would be send to your VM. Great!

Apache Virtual Hosts

Once your VM can be reached, Apache has to know how to handle the request. This is where we use the Virtual Hosts feature of the Apache HTTP server.

I usually run on Debian, so I'll explain this with an example configuration in /etc/apache2/sites-available. Let's have a file there called test.my.address.com and fill it with information.

<VirtualHost *:80>
        ServerName test.my.address.com
        ServerAdmin webmaster@my.address.com

        DocumentRoot /var/www/test.my.address.com/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/test.my.address.com/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/test.my.address.com.error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/test.my.address.com.access.log combined
</VirtualHost>

The key setting inside this file is the ServerName directive. This tells Apache under which name this server should be available. Additional names can be given with the ServerAlias directive.

In case you're using Debian as well, don't forget to sudo a2ensite test.my.address.com after you created the file and sudo invoke-rc.d apache2 restart.

Der Hochstapler

Posted 2012-07-29T11:46:47.987

Reputation: 77 228

Thanks a lot!:) I made my part, and wrote email to friend about his part. Thank again:) – scythargon – 2012-07-29T12:32:19.407

@scythargon: My pleasure. We're here to help :) – Der Hochstapler – 2012-07-29T12:54:40.453

While my friend did not answer my, can you, please? What I was thought about how it works: http-request for test.my.address.com goes to my.address.com and then my Apache server on VM handles it. But you said that DNS-server at address.com should know about test.my before all of this and independently of knowledge about my, why? – scythargon – 2012-07-29T13:07:47.857

@scythargon: For any HTTP request to reach your server, any client first has to figure out to what server the request has to be sent. So the hostname has to be valid. Otherwise they'll just be told "that name does not exist". – Der Hochstapler – 2012-07-29T13:15:39.087