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/subdomain.mydomain.com

-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

127.0.1.1 subdomain.myDomain.com

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/subdomain.mydomain.com
ServerName sub.mydomain.com
ServerAlias www.mydomain.com
ServerAdmin administrator@mydomain.com
    
    <Directory /var/www/sub.domain.com>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
    
    ErrorLog ${APACHELOGDIR}/sub.domain.error.log
    CustomLog ${APACHELOGDIR}/sub.domain.access.log combined
</VirtualHost>

 

Disable the default conf

sudo a2dissite 000-default.conf

Enable new site or subdomain in apache with

sudo a2ensite sub.yourdomain.com.conf

To disable site

sudo a2dissite sub.yourdomain.com.conf

 

 

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 *