Jump to content

Creating subdomains in Apache


brent

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/html   { Main domain }

/var/www/example.com/logs  {apache logs for site}

/var/www/music.example.com {subdomain}

/var/www/media.example.com {subdomain}

 

 

 

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/
</VirtualHost>
<Directory /var/www/forums/>
                Options -Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
</Directory>
 

If you want your individual site logs to reside in /var/www/subdomain.example.com/logs you need to create the logs/ directory in the root of your sub-domain directory. 

Once directory is created you need to modify your host file to look like the below.

<VirtualHost *:80>
ServerName forums.example.com
ServerAlias www.forums.example.com
DocumentRoot /var/www/forums.example.com
</VirtualHost>
LogLevel warn
  ErrorLog /var/www/subdomain.example.com/logs/error-mysubdomainname.com.log
  CustomLog /var/www/subdomain.example.com/logs/access-mysubdomainname.com.log combined

<Directory /var/www/forums/>
                Options -Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
</Directory>

 

 

Next we will need to enable the site with apache2.

sudo a2ensite subdomain.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

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...