Create Subdomains with Apache – The Easy Way
This will walk through the process of adding sub domains to your local server ( onesite.localhost , twosite.localhost, etc ).
To understand this need you are liekly working on multiple sites at once on your local development machine. So lets say you have an excellent CakePHP application, a gnarly JS sandbox, and your personal start page all belonging to different directories. Problem is, there is only one localhost. But just like your live server can have subdomains, you can skip DNS and use virtualhosts in Apache do the work. Imagine, unlimited sub domains all live at one on your localhost pointing to folders all across your machine.
First off I will be be clear that this is instructions for a Linux device. If your not running Linux, then start today. You’ll be happy you made the switch to the ubiquitous LAMP architecture.
First thing to do is choose a subdomain for each folder, in my case I have two sub domains;
- digbiz.localhost - /home/eddie/workspace/Digital_Business/
- phpmyadmin.loclahost - /srv/www/phpMyAdmin
These are in additions to my usual localhost path;
- localhost - /srv/www/htdocs/
Setting your top domain – localhost
Most of you have already done this step long ago, but just to be certain, you have set your documentroot and settings in
/etc/apache2/default-server.conf
# # Global configuration that will be applicable for all virtual hosts, unless # deleted here, or overriden elswhere. # DocumentRoot "/srv/www/htdocs" # # Configure the DocumentRoot Properties # <Directory "/srv/www/htdocs"> Options All # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit AllowOverride All # Controls who can get stuff from this server. Order allow,deny Allow from all </Directory> # # Configure Sub-Domain Properties. This prevents those nasty 403 errors # # mysql administration tool <Directory "/srv/www/phpMyAdmin/"> Options Indexes MultiViews AllowOverride All Order allow,deny Allow from all </Directory> # a client web site built with CakePHP <Directory "/home/eddie/workspace/Digital_Business/app/webroot/"> Options All AllowOverride All Order allow,deny Allow from all </Directory>
Setting your sub domain’s paths
In order for this to work we’ll need to be specific about which sub domain points where, easy enough. You’ll notice I am not using httpd.conf, but rather a configuration file in a sub direcotry that is referenced in the main configuration file. This is the typical setup, and any *.conf file in most of the *.d directoriess should be read. If the folder vhosts.d does not exist, add this code directly to the end of httpd.conf.
/etc/apache2/vhosts.d/subdoms.conf
NameVirtualHost localhost:80 # the mysql tool's url <VirtualHost phpmyadmin.localhost:80> # and absolute path DocumentRoot "/srv/www/phpMyAdmin/" </VirtualHost> #Same for the Client Site <VirtualHost digbiz.localhost:80> DocumentRoot "/home/eddie/workspace/Digital_Business/app/webroot/" </VirtualHost>
You may add as many as you want ( to a limit I Imagine) by adding more of the < through> blocks. The very first line of the code should only be used once. The names you use here are the host names we’ll need below, so keep note.
Setting your new sub domains as valid hosts
For this part you need to edit your you can either edit /etc/hosts directly, or for those who are unsure, use the systems administration panel > network (services) > host(name)s. I’m running suse so my system panel is Yast, for you it may differ.
Once your inside the Host configuration module (or hosts file) just add a new record for every sub domain. In my example I use ::1 as the IP address only because IPv6 is enabled on my server. You may need to use 127.0.0.1.
If you open that image up you’ll see I have already added ‘digbiz.localhost’ and was in the process of adding ‘phpmyadmin.localhosts.’ Remember, these are the virtual hosts we setup just before.
Restart Apache
Once your done adding the sub-domains clcik finish and the settings will be saved. You can now restart apache and test it out.
# /etc/init.d/apache2 restartNote: I was curious if you could set up completely new domains, mylocalhost. I didn’t have much luck though. If anyone has a reason, or has in fact succeeded I would love to hear about it.



Thanks its really Helps. Thanks a lot
@Saqib
THank you, I am glad this helped.
The article is very helpful, though I still don’t understand how to create a subdomain.
@Vitebsk
..Well based on that comment I would say it is not as helpful as I hoped.
Are you trying to create a sub-domain locally, or on a public server(if public, shared or dedicated)?
Eddie, I have VPS. I don’t know how to create a subdomain. What should I edit? httpd.conf? What else? Will you help me, please?
Oh, I forgot, I don’t have any control panels. Just ssh. Thank you!
@VItebsk
Your hosting provider will need to provide some means to edit the DNS records for your server. Potentially you may need to edit some files in the /etc/bind directory to setup A and CNAME records, but this depends on your hosting provider and server setup.
I would contact their support # to understand how they provide domain name resolution.
Ок. Thanks a lot for your help and answers!
The only way I know to do that is using the HOSTS file in Windows. I don’t know the Linux equivalent but I’ll assume you do.
Oh, I almost forgot. Thanks for the guide.
@Adz.
Thanks for the mental kick…
I am not sure what I was thinking, or actually attempted when I wrote that statement.
Just as you can add sub.localhost as an alias under hostnames you can also add mylocalhost. this can be done through the systems configuration panel, or directly within /etc/hosts.
(::1 is just the ipv6 equivalent of 127.0.0.1)
You can then configure that host name to point to any directory by adding additional virtual hosts.
do i create cname records by making subdomains for my site, or is that a different process altogether?
I setup a sub-domains in BIND, It work.
Thank you very much,
Thank you for the article, found that different local domains works for me when I add .local to the extension through Firefox
ex: I call the server solar2
so I get to it with solar2.local
this is on Ubuntu, running apache – good luck
@Alvin
Thanks for the great tip.
hey thanks for this tut. It got me off in the right direction…
If you’re like me and want to make both domain.tld and http://www.domain.tld work.
You can use this tut
create a virtual host as in the tut and inside
#without the port! if it doesn’t work put domain.tld:80
Serveralias domain.tld *.domain.tld
#rest of your config here
if that set up doesn’t work then create two virtual hosts. one for domain.tld and another for http://www.domain.tld, in this setup it only worked for me without the port.
so when a user visits domain.tld it loads the same as http://www.domain.tld but its not a good idea to duplicate content to decide which one u want and then do a 301 redirect from domain.tld to http://www.domain.tld or the otherway round.
thanks again
@Courtney
Thanks for the great info. Your right, users should be careful not to duplicate configuration information, and instead redirect users as needed. This may also enhance some search engine results (though Google typically ignores the www. as duplicate without effecting rating)
Hey
I am still confused after reading the entrier article and comment, how can i point my subdomain, I am using linux dedicated server. I know abt virtual host, but i am not sure from where i have to point sudomain, is it from domain control panel? or to add anywhere in server? Pls help
@Anil
the fact that your mentioning a “control panel” leads me to believe you are using a hosting provider. This article was focused on self-managed servers.
There should be a place to setup cnames records (DNS) within the panel. And each provider may be a little different. You should start by contacting their support.
Good Luck!
When I do this, it just directs me to the main domain.
Just like nico, when I do this on Ubuntu 10.10 I get the main server. I have the first set to test.localhost which should go to /var/www2/, but it goes to /var/www/ just like all other things I put in my /etc/hosts file as 127.0.1.1
EG:
If I put
“127.0.1.1 flying.abomination.seriously.crazy” (which is not my hostname)
in /etc/hosts and go to that from firefox it comes up with my /var/www/index.html page
hi,
thanks for the tutorial, but can u help wiz my issue?
i have a domain hosted on a shared hosting site & i created a subdomain and i wanna point this subdomain to my windows local server? what configurations need to be done to my local server?
thanks in advance.
Hmm, I think in your case you might want to use Apache Proxy/ReverseProxy, which will forward a subdomain to any other address. This way your domain will resolve to your hosting provider, with exception of the subdomain, which points to your local server. Your local server will need a static IP exposed to the world.
http://www.apachetutor.org/admin/reverseproxies
http://httpd.apache.org/docs/2.0/mod/mod_proxy.html
@Anon, @Nico
Strange behavior.. I haven’t tried this on Ubuntu, which always tends to do things just a bit different. But apache configuration should be standard. Howver my guess is they broke up the configuration files a bt difference. Start at httpd.conf and look for “include XX.xx” statements to know here additional config files live.
once you have located all the conifg files apche is loadin, verify some things:
* Make sure both /dir1 and /dir2 are defined as directories,
* and that a “default” or “wildcard” virtual host is not defined in another vhost file
@Anon – anything you define in /etc/hosts with 127.0.0.1 will resolve to localhost, You just need to be sure that whatevr you add there ‘flying.abomination.seriously.crazy’ is listed as a virtual directory, and points to /var/www2 . SOunds like you have duplicate virtual host definitions. And A[pache is relying on the first rules loaded.
Thanks for the tutorial.
But when I try a subdomain on another computer on my network, I get a “server not found” error.
The only way to fix this is to edit the hosts file on that computer to include the subdomains (but my server’s IP instead of ::1), but I am also testing the site on a DS and iPad and it’s impossible to edit hosts on them.
Is there a way to make the devices somehow “detect” the subdomains? Thanks.
Very simple with the original link
http://httpd.apache.org/docs/2.0/vhosts/examples.html
Hope it’s shorter than!