How to create subdomains on a name-based virtualhost?
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.
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 [email protected]
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
.