How to setup subdomain on Ubuntu Server 14.04

Create a folder at your desired location to host your subdomain files

mkdir -p /var/www/

-p helps to create parent directories if they don’t already exist

Edit your hosts file

sudo nano /etc/hosts

Add entry to your hosts file

Create a sub.domain.conf file for your sub-domain site in /etc/apache2/sites-available. I prefer to copy from 000-default.conf

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your.sub.domain.conf
sudo nano /etc/apache2/sites-available/your.sub.domain.conf

Update your sub.domain.conf configuration

<VirtualHost *:80>
    DocumentRoot /var/www/
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
        Require all granted
    ErrorLog ${APACHELOGDIR}/sub.domain.error.log
    CustomLog ${APACHELOGDIR}/sub.domain.access.log combined


Disable the default conf

sudo a2dissite 000-default.conf

Enable new site or subdomain in apache with

sudo a2ensite

To disable site

sudo a2dissite



Enable mod_rewrite (optional)

sudo a2enmod rewrite

To disable mod_rewrite

sudo a2dismod rewrite



Restart Apache2

sudo service apache2 restart

Ensure that you have already setup your public DNS pointing to your server.

1 thought on “How to setup subdomain on Ubuntu Server 14.04”

Leave a Comment

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