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

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

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

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.

19 Responses leave one →
  1. 2009 February 13
    Saqib permalink

    Thanks its really Helps. Thanks a lot

  2. 2009 February 13
    Eddie permalink

    @Saqib
    THank you, I am glad this helped.

  3. 2009 February 21

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

  4. 2009 February 21
    Eddie permalink

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

  5. 2009 February 22

    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. 2009 February 22

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

  7. 2009 February 22
    Eddie permalink

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

  8. 2009 February 22

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

  9. 2009 March 5

    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. 2009 March 5

    Oh, I almost forgot. Thanks for the guide. :D

  11. 2009 March 5
    Eddie permalink

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

    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
    #
    
    127.0.0.1       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
    127.0.0.2       linux-cv2g.OLLI420 linux-cv2g
    

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

  12. 2009 April 20
    Raksha permalink

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

  13. 2009 September 11

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

    Thank you very much,

  14. 2009 October 5

    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. 2009 October 5
    Eddie permalink

    @Alvin
    Thanks for the great tip.

  16. 2010 January 24

    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. 2010 January 28
    Eddie permalink

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

  18. 2010 March 24
    Anil permalink

    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

  19. 2010 March 24
    Eddie permalink

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

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