How to configure your domain, DNS and create virtual servers for subdomains in Apache2

Posted: 11 November, 2013 in Computers, Linux
Tags: , , , , ,

Hello again! There was a long time since i didnt post, sorry but here comes the latest tutorial!

This tutorial contains 3 parts:

  • Create and configure your domain and DNS
  • Creating Virtual hosts with Apache2
  • And creating your websites

Create and configure your domian and DNS


First of all you need to have a domain, i bought one in, the first year is only around 7 euros, but you can buy in any other online domain shop, doesnt matter. My domain is

Once you have it you to configure where the domain is going to point. By default some domains point at the private web space that the seller gives you with the domain. In our case we are going to point at the address where we have the server (Our public IP).
In the field to change is Redirection HTTP: YOUR.IP

You should also change the DNS configuration, In Register A, you should put the address of your server too. This will allow to display the name instead of the IP address in your web browser address bar.

Now the domain will point at your server IP address, allowing you to access to your server using your domain name instead of the IP. Congratulations! But wait, this isn’t over yet. We still have to configure the subdomains and the Virtual Hosts for the domain.

Creating and configuring Subdomains

This part is similar to create a domain. You should find the menu in your domain seller to create a subdomain like ( that will allow you to access to another web that you have in your server directly with this subdomain address. Once you create all the subdomains you want you should also edit the DNS configuration and point in the Register A to your server address,  quite similar process, easy right?

IMPORTANT: All these steps with DNS and domain changes may take a while to be configured (from 0 to 48 hours) depending on your domain name provider and in the DNS you are using and how they spread the new DNS configurations (names, register A, etc). So be patient!

Creating VirtualHosts with Apache2


You will need to have Apache2 installed in your server. To create a VirtualHost you need to create a file and link it in one folder so Apache2 will know that is configured and up. This configuration allows you to stop or start a VirtualHost with only one command without losing any configuration. You will be using mainly 2 folders:

  • /etc/apache2/sites-available – Contains the files of the virtual hosts.
  • /etc/apache2/sites-enabled – Contains links to the files in sites-available

You will also be using 2 commands:

a2ensite <file> – creates a link in sites-enabled to the file in sites-available
a2dissite <file> – removes a link in sites-enabled to the file in sites-available

So lets start.

You can personalize /etc/apache2/sites-avaiable/default to put your server or just copy it and use it as a template.

You will have 1 file per domain/subdomain in this case: one file for and another file for You can have as many as you need but for this example i will show only with 2.

Basically the content of this default file is:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined

The main lines that you will be interested in order to configure your virtual host are going to be the ones in these 2 files:


<VirtualHost *:80>
DocumentRoot /var/www/


<VirtualHost *:80>
DocumentRoot /var/www/

After you can add whatever you need like logs, permissions for directories and so. I am just explaining the basic configuration to have it running.

Now you need to enable this 2 sites in Apache, in other words, make a link to this files in /etc/apache2/sites-enabled/

We also want to disable the default host.

To do this you only need to run these commands:

cd /etc/apache2/sites-avaiable/
a2dissite default

Now we have everything ready, but we are missing the web itself!

Creating the websites


This is pretty easy step. Just place your web in the folders that you specified in DocumentRoot in the VirtualHost file. If you dont have a web yet just create an index.html with some content in the folders so you can test if the virtual hosts are working

cat "This is" > /var/www/
cat "This is" > /var/www/

Now you are ready to test!





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s