Skip to content
[shareaholic app="share_buttons" id="14657658"]
Share this..

Create Subdomains with Apache – The Easy Way

2008 October 10

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


# 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
# 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
# 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

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.


NameVirtualHost localhost:80
# the mysql tool's url
<VirtualHost phpmyadmin.localhost:80>
# and absolute path
DocumentRoot "/srv/www/phpMyAdmin/"
#Same for the Client Site
<VirtualHost digbiz.localhost:80>
DocumentRoot "/home/eddie/workspace/Digital_Business/app/webroot/"

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.

Begin by launching the Adminstration Panel

Begin by launching the Adminstration Panel

Run the Host Configuration module

Run the Host Configuration module

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

Enter each sub domain as a new record

Enter each sub domain as a new record

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 restart

Note: 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.

28 Responses leave one →
  1. Saqib permalink
    February 13, 2009

    Thanks its really Helps. Thanks a lot

  2. Eddie permalink*
    February 13, 2009

    THank you, I am glad this helped.

  3. February 21, 2009

    The article is very helpful, though I still don’t understand how to create a subdomain.

  4. Eddie permalink*
    February 21, 2009

    ..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)?

  5. February 22, 2009

    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?

  6. February 22, 2009

    Oh, I forgot, I don’t have any control panels. Just ssh. Thank you!

  7. Eddie permalink*
    February 22, 2009

    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.

  8. February 22, 2009

    Ок. Thanks a lot for your help and answers!

  9. March 5, 2009

    Note: 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.

    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.

  10. March 5, 2009

    Oh, I almost forgot. Thanks for the guide. 😀

  11. Eddie permalink*
    March 5, 2009

    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.

    caeddie@linux-cv2g:~> cat /etc/hosts
    # hosts         This file describes a number of hostname-to-address
    #               mappings for the TCP/IP subsystem.  It is mostly
    #               used at boot time, when no name servers are running.
    #               On small systems, this file can be used instead of a
    #               "named" name server.
    # Syntax:
    # IP-Address  Full-Qualified-Hostname  Short-Hostname
    #       localhost
    # special IPv6 addresses
    ::1             localhost ipv6-loopback phpmyadmin.localhost digbiz.localhost wishlist.localhost test.localhost mylocalhost
    fe00::0         ipv6-localnet
    ff00::0         ipv6-mcastprefix
    ff02::1         ipv6-allnodes
    ff02::2         ipv6-allrouters
    ff02::3         ipv6-allhosts       linux-cv2g.OLLI420 linux-cv2g

    (::1 is just the ipv6 equivalent of
    You can then configure that host name to point to any directory by adding additional virtual hosts.

  12. Raksha permalink
    April 20, 2009

    do i create cname records by making subdomains for my site, or is that a different process altogether?

  13. September 11, 2009

    I setup a sub-domains in BIND, It work.

    Thank you very much,

  14. October 5, 2009

    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

  15. Eddie permalink*
    October 5, 2009

    Thanks for the great tip.

  16. January 24, 2010

    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

  17. Eddie permalink*
    January 28, 2010


    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)

  18. Anil permalink
    March 24, 2010

    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

  19. Eddie permalink*
    March 24, 2010

    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!

  20. Nico permalink
    November 27, 2010

    When I do this, it just directs me to the main domain.

  21. Anon permalink
    November 29, 2010

    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
    If I put
    “ 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

  22. March 28, 2011

    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.

  23. Eddie permalink*
    March 28, 2011

    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.

  24. Eddie permalink*
    April 1, 2011

    @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 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.

  25. April 1, 2011

    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.

  26. August 18, 2011

    Very simple with the original link

    Hope it’s shorter than!

  27. March 18, 2013

    could you please rewrite this tuto for apache 2 without all the vhosts.d and subdomai.??

    cause i think they dont exist in apache 2 ??

Trackbacks and Pingbacks

  1. Domain name customisation? htaccess? | PHP Developer Resource

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS