Jump to content
Linux - Tutorials And How To's
Sign in to follow this  

Creating subdomains in Apache

Recommended Posts

Using sub-domains is a good way to organize your website.  example.com is what we will call the parent domain. You could have many different parts to  your website. For example you could have forums, books, music, really anything. so instead of having all your components to your website lay in the same directory /var/www  you could go a step further and organize it. Here is an example of how you could structure /var/www using sub-domains


/var/www/example.com   { Main domain }

/var/www/forums              { forums }

/var/www/music                {if you have a music site or streaming}

/var/www/books                { digital books site}




By using this type of structure to set up sub-domains all your website components eg: forums, music, books wont have to all be in the root directory of the website. Do you like www.example.com/forums? or would you rather see forums.example.com? 


Here is how we accomplish this.


 We need to navigate to

cd /etc/apache2/sites-available/



Now we are going to create our sub-domain file.

sudo nano forums.example.com




We will use a virtual host here is what it will look like.

<VirtualHost *:80>
ServerName forums.example.com
ServerAlias www.forums.example.com
DocumentRoot /var/www/forums/
<Directory /var/www/forums/>
                Options -Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all




Next we will need to enable the site with apache2.

sudo a2ensite forums.example.com



Now we need to reload apache for the changes to take place.

sudo /etc/init.d/apache2 reload


Now all you need to do is create a folder in /var/www called forums eg; /var/www/forums


In order for you to be able to access the forums.example.com you will need to modify DNS and add an A record. You Can go to the registar where you got the domain and register the sub-domain and point it to your server, you could use a third party dns such as zoneedit to handle all your dns needs for your domain

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this