[Ubuntu][Apache]Setting up subdomain on Ubuntu Server

Important note: This is not original article, I got it from TungShien & Codespace69 (at Medium.com). The article helped me a lot in setting up my website, but due to some reason the account and the article is banned due to some violation. Here is the original article I managed to recover.

Apache Virtual Host With Dynamic subdomains

How to Create sub domains in apache on Ubuntu?


I have a server at mysite.no-ip.org. Everything is working fine and I have a blog and owncloud that I access writing mysite.no-ip.org/blog and mysite.no-ip.org/owncloud. Now, I want to create subdomains so that I can write blog.mysite.no-ip.org and owncloud.mysite.no-ip.org.

I have looked a lot into it and the simplest tutorial was this one which I followed. http://www.bcat.eu/blog/local-subdomains-under-ubuntu-linux-and-apache-2-4-tutorial/ Except that I am not on my server but ssh into it remotely so if I use localhost I cannot see if this works I believe. This is my first obstacle, can I follow this tutorial without being present next to the server?

I also just discovered CNAME records which (I am not completely sure) seem necessary to what I want to do. I use no-ip.org and I have tried to follow something like this but this is not really clear either https://support.uberflip.com/entries/235780-4-4-5-how-to-setup-a-cname-redirect#noip

Does anyone have any pointers? This is quite shady to me and I have no idea if I am far from the solution or really close (at least if I could try localhost that would be a first step in order to check my Apache conf but I can’t even do it since I ssh).

How to handle this ?

This is actually really easy.

First make sure your DNS is working. You can use a AAAAA or CNAME record as long as it points to your IP address.

Please check if it’s working before you continue! This will save you much trouble. You can check using ping subdomain.mydomain.com or nslookup subdomain.mydomain.com. If it’s pointing to your IP address you’re good.

Note that DNS changes may take up to 24 hours. If you want to continue now, you can edit your hosts file to manually add the sub domain. Note that this will only work for your current PC so you should check your DNS again 24 hours later to make sure it’s working elsewhere as well (don’t forget to revert your changes to your hosts file before you check).

Now update your Apache2 configuration.

1st way

Just adding this VirtualHost will do:

<VirtualHost *:80>
    ServerName subdomain.mydomain.com
    DocumentRoot /var/www/subdomain.mydomain.com

Now create /var/www/subdomain.mydomain.com and restart Apache2:

mkdir -p /var/www/subdomain.mydomain.com
sudo service apache2 restart

enable the site:

sudo a2ensite subdomain.mydomain.com

Repeat this for every sub domain you want to add.

2nd way

Apache interprets its configuration in the order in which it is recorded. For example we put on first place the deny directives and on the second the allow directives.

That you need is to put your virtual host configurations in a correct order. For example place the test virtual host configuration in a conf file named 001-test.myapp.local.conf and the regular virtual host in a file named 002-myapp.local.conf. Or if the both configurations are in one file place the test one first:

$ sudo nano /etc/apache2/sites-available/myapp.local.conf 
# sudo nano /etc/apache2/sites-available/http
<VirtualHost *:80>
        ServerName test.myapp.local
        ServerAlias *.test.myapp.local
        DocumentRoot /var/www/test.myapp/web
        <Directory /var/www/test.myapp/web/>
            AllowOverride All
            Require local
<VirtualHost *:80>
        ServerName myapp.local
        ServerAlias *.myapp.local
        DocumentRoot /var/www/myapp/web
        <Directory /var/www/myapp/web/>
            AllowOverride All
            Require local

Thus the requests that doesn’t match to the first virtual host will fall into the second. Here is how it’s working on Ubuntu 18.04 with Apache/2.4.29:

Leave a comment

Your email address will not be published. Required fields are marked *