Bennett Hollstein

Web developer

[Ubuntu][Apache 2][Contabo VPS] Add Subdomain

Hello,

in this tutorial I just wanted to show you how you can add a subdomain to a Ubuntu Sever with Apache 2 (but it will also work other Linux distros). I am using a Contabo VPS vServer with .de domain for this tutorial.

First, go to your domain settings. When you use Contabo, you will find it in the user webinterface at http://my.contabo.com. Now add a new DNS Entry to your domain with the name “[Subdomain].[Domain]”, you of course have to replace it so it looks like this:

mysubdomain.example.com

The type for this is “A” and the data is the ip address of your server.

Now, the DNS entry is ready and you just have to set up the subdomain on your server.

For that, go into the terminal and make a new directory for the subdomain, for example /var/www/subdomain/

mkdir /var/www/subdomain

Now, edit the hosts file via

nano /etc/hosts

and add the following entry

[IP ADDRESS] [SUBDOMAIN].[DOMAIN]

Of course edit everything to your data, so it looks like this:

123.456.7.890 mysubdomain.example.com

Please note, that between the IP and the domain there is a TAB but I couldn’t add it in this article.

Now create and edit the apache config for the subdomain

nano /etc/apache2/sites-available/mysubdomain.example.com.conf

(replace the mysubdomain.example.com)

And add the following:

<VirtualHost *:80>
 ServerAdmin mail@localhost
 ServerName mysubdomain.example.com
 ServerAlias mysubdomain.example.com
 DocumentRoot /var/www/subdomain
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

(replace the mysubdomain.example.com)

Next, activate the newly created subdomain conf via

a2ensite mysubdomain.example.com

Now you just have to restart the apache2 server

service apache2 restart

And your subdomain should be ready. Please note that it sometimes takes up to 15 minutes until the new DNS entry is registered, so don’t panic if it doesn’t work directly.

I hope you have working subdomain now,

Bennett

Leave a Reply