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 1and1.es, the first year is only around 7 euros, but you can buy in any other online domain shop, doesnt matter. My domain is roisoftstudio.com.

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 1and1.es 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 (menu.roisoftstudio.com) 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 roisoftstudio.com and another file for menu.roisoftstudio.com. 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>
<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
</VirtualHost>

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:

/etc/apache2/sites-available/roisoftstudio.com

<VirtualHost *:80>
ServerAdmin roisoftstudio@gmail.com
ServerName roisoftstudio.com
ServerAlias www.roisoftstudio.com
DocumentRoot /var/www/roisoftstudio.com/roisoftstudio/htdocs
</VirtualHost>

/etc/apache2/sites-available/menu.roisoftstudio.com

<VirtualHost *:80>
ServerAdmin roisoftstudio@gmail.com
ServerName menu.roisoftstudio.com
ServerAlias www.menu.roisoftstudio.com
DocumentRoot /var/www/roisoftstudio.com/menu/htdocs
</VirtualHost>

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/
a2ensite roisoftstudio.com
a2ensite menu.roisoftstudio.com
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 roisoftstudio.com" > /var/www/roisoftstudio.com/roisoftstudio/htdocs/index.html
cat "This is menu.roisoftstudio.com" > /var/www/roisoftstudio.com/menu/htdocs/index.html

Now you are ready to test!

Enjoy!

 

 

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s