ankit.aki
By:
ankit.aki

How to create a subdomain in Apache , Ubuntu 14.04?

August 21, 2014 68.7k views

I have installed Apache, Mysql, Webmin & also configured DNS settings for the primary domain but however I want to create a staging site first .i.e. staging.domain.com and then want to make the site live.

I already checked so many articles on Digital Ocean's forum. Every one has different approach & this all makes it very confusing.

I read these posts but it all seems to have messed up.
Can some one please provide an easy way to deal with this?

Thanks in Advance.

9 Answers
  1. Create an A record for your sub-domain that points to your droplet
  2. Create a .config file for your sub-domain site in /etc/apache2/sites-available
  3. Create the directory for your site in /var/www (or wherever you said it was in the config file)
  4. Enable the site in apache with: sudo a2ensite sub.yourdomain.com (or whatever you named your site in the config)
  5. reload apache: sudo service apache2 reload

NOTE: the DNS record you create might take awhile to propagate, so be patient..sleep on it before panicking and re-editing all of your files..

  • Hi Adam,

    Thanks for helping out.
    I exactly followed all the steps that you had mentioned.

    But when I try to open the subdomain site: staging.domain.com
    it opens the main site : domain.com

    I had created A record that points to IP Address of my primary droplet.

    In short, domain.com & subdomain.domain.com point to the same IP Address. Is that correct?

    Here are all the Records that i have created on Digital ocean that will help you understand:

    Recordtype
    A-----------------> @ -------------------------------> droplet
    ip
    A----------------->domain.com-----------------> dropletip
    A----------------->staging.domain.com-----> droplet
    ip
    CNAME------->www----------------------------->@
    CNAME------->*------------------------------------>@
    NS-------------->NS1.digitalocean.com
    NS-------------->NS2.digitalocean.com
    NS-------------->NS3.digitalocean.com

    Is this correct? or Am I still missing something?

    I guess I need to remove CName --> * -->@ from that list.

    Thanks in Advance.

  • Hi Adam,
    I want to create record in sub domain can help for this.
    folder is my sql folder,like this i want create 2 more records.

Maybe this if you want to use .htaccess:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/staging
    ServerAlias   www.staging.domain.com  
    ServerName staging.domain.com   (or whatever name you gave it)

    <Directory /var/www/staging>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
                Require all granted
    </Directory>

    ErrorLog ${APACHELOGDIR}/error.log
    CustomLog ${APACHELOGDIR}/error.log/access.log combined

</VirtualHost>
  • Hi Adam,

    I have exactly done as you have mentioned.

    All of that has worked like a charm.

    And Thanks a lot for all of your help bro :)

try using <VirtualHost *:80> rather than: <VirtualHost 104.236.80.93:80>

  • That's how I had it originally, but let me try it.

    EDIT:

    Yupe, same result, are the DNS records ok?

the only other thing I have noticed is that you are using DocumentRoot /var/www/html for your top-level domain, while your other sites are sub-directories. I wonder if that could be causing some conflict.

You probably know this already, but just to check:

files in /etc/apache2/sites-enabled/ are symlinks to the files in sites-available. As such, if you were renaming your config files, you should delete all those symlinks, then rename the actual files in /sites-available/ and then re-enable all of your sites....(as opposed to just renaming symlinks in/etc/apache2/sites-enabled/)

  • I just created a directory for the top-level domain, restarted and everything stills the same.

    Yeah I've been doing a2dissite for each config file then renaming and then re-enabling them.

    It's just that everything seems fine, I really don't have a clue of what is failing.

Maybe use A records instead of CName..

When you create your a records, you just need to put in the sub-domain (calis) rather than the full domain (calis.kludgecruft.com)

  • I have one A record subdomain and a CNAME one.

    The A one is orlybg ---> 104.236.80.93
    The CNAME one calis ---> kludgecruft.com. (dot at the end)

    is it ok for the A record to have an IP? or should it be a hostname?

    gonna add another A record subdomain to test

  • Seems you must have an IP for A records, I added a new subdomain fubar, and switched calis from CNAME to A record, the zone file is like this now:

    $ORIGIN kludgecruft.com.
    $TTL 1800
    kludgecruft.com. IN SOA ns1.digitalocean.com. hostmaster.kludgecruft.com. 1422028092 10800 3600 604800 1800
    kludgecruft.com. 1800 IN NS ns1.digitalocean.com.
    kludgecruft.com. 1800 IN NS ns2.digitalocean.com.
    kludgecruft.com. 1800 IN NS ns3.digitalocean.com.
    kludgecruft.com. 1800 IN A 104.236.80.93
    www.kludgecruft.com. 1800 IN CNAME kludgecruft.com.
    orlybg.kludgecruft.com. 1800 IN A 104.236.80.93
    fubar.kludgecruft.com. 1800 IN A 104.236.80.93
    calis.kludgecruft.com. 1800 IN A 104.236.80.93

    Gonna test them once it propagates

Your A record for a subdomain will be just the one word, like:
A----------------->staging-----> dropletip

because those records are for your domain, so it already knows domain.com

what does your conf file look like for your sub-domain?

Hi Adam,

I deleted the A record & created the new A record as you said.

Here is what the conf file looks like:

<VirtualHost *:80>

ServerAdmin webmaster@localhost
DocumentRoot /var/www/staging

ErrorLog ${APACHELOGDIR}/error.log
CustomLog ${APACHELOGDIR}/error.log/access.log combined

</VirtualHost>

Actually I just copied the 000-default.conf file in sites-available directory & then modified the document root.

Let me know if I have to add something.

Thanks in Advance.

  • Hi Adam,

    I had forgot to add ServerName directive & now I just added that & the staging site opens correctly.

    Do I need to add anything more which may be required in future?

Hello @sierracircle I followed your advice and also a couple of DO tutorials the DNS and the Virtualhost ones, oh and one from symfony2 for server setup. But I am still not being able to make my subdomains work properly. I wanted to ask you for some help.

I tried various DNS configurations, switched DNS from my registar to DO and tried with A and CNAME records, here's how my zone file looks like:

$ORIGIN kludgecruft.com.
$TTL 1800
kludgecruft.com. IN SOA ns1.digitalocean.com. hostmaster.kludgecruft.com. 1421532591 10800 3600 604800 1800
kludgecruft.com. 1800 IN NS ns1.digitalocean.com.
kludgecruft.com. 1800 IN NS ns2.digitalocean.com.
kludgecruft.com. 1800 IN NS ns3.digitalocean.com.
kludgecruft.com. 1800 IN A 104.236.80.93
www.kludgecruft.com. 1800 IN CNAME kludgecruft.com.
calis.kludgecruft.com. 1800 IN CNAME kludgecruft.com.
orlybg.kludgecruft.com. 1800 IN A 104.236.80.93

I have my domain kuldgecruft.com and have 2 test subdomains orlybg (A), calis (CNAME) and WWW as an alias for the main domain so to speak.

Here are my apache vhost files (one for each), all 3 are enabled, I've restarted apache in various ways, I changed the name of the flle of the 'main vhost' to 000main.conf because it wasn't being taken as the first host.

****000main.conf

<VirtualHost 104.236.80.93:80>
    ServerName kludgecruf.com


    DocumentRoot /var/www/html
    <Directory /var/www/html>
        # enable the .htaccess rewrites
        AllowOverride All
        Require all granted

    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

****orlybg

<VirtualHost 104.236.80.93:80>
    ServerName orlybg.kludgecruf.com

    DocumentRoot /var/www/html/myproject
    <Directory /var/www/html/myproject>
        # enable the .htaccess rewrites
        AllowOverride All
        Require all granted

    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error_orlybg.log
    CustomLog ${APACHE_LOG_DIR}/access_orlybg.log combined

</VirtualHost>

****calis

<VirtualHost 104.236.80.93:80>
    ServerName calis.kludgecruf.com


    DocumentRoot /var/www/html/calis
    <Directory /var/www/html/calis>
        # enable the .htaccess rewrites
        AllowOverride All
        Require all granted

    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error_calis.log
    CustomLog ${APACHE_LOG_DIR}/access_calis.log combined

</VirtualHost>

I think maybe this can help:

apachectl -t -D DUMP_VHOSTS
VirtualHost configuration:
104.236.80.93:80       is a NameVirtualHost
         default server kludgecruf.com (/etc/apache2/sites-enabled/000main.conf:1)
         port 80 namevhost kludgecruf.com (/etc/apache2/sites-enabled/000main.conf:1)
         port 80 namevhost calis.kludgecruf.com (/etc/apache2/sites-enabled/calis.conf:1)
         port 80 namevhost orlybg.kludgecruf.com (/etc/apache2/sites-enabled/orlybg.conf:1)

I'm not well versed in apache, I am a PHP developer, and I can find my way in linux, and everything looks find to me, I can't figure out why every URL of my domain responses the same thing.

Thanks in advance! cheers

  • Are you using Ubuntu 14.04?

    also, try renaming config files like this:

    kludgecruft.com.conf
    calis.kludgecruft.com.conf
    orlybg.kludgecruf.com.conf

  • yupe latest ubuntu. I just tried it and restarted apache

    now I am getting calis for everything as a response, instead of kludgebox, I am guessing because calis.kludgecruft.com.conf is the first domain alphabetically.

    apachectl -t -D DUMP_VHOSTS
    VirtualHost configuration:
    *:80 is a NameVirtualHost
    default server calis.kludgecruf.com (/etc/apache2/sites-enabled/calis.kludgecruft.com.conf:1)
    port 80 namevhost calis.kludgecruf.com (/etc/apache2/sites-enabled/calis.kludgecruft.com.conf:1)
    port 80 namevhost kludgecruf.com (/etc/apache2/sites-enabled/kludgecruft.com.conf:1)
    port 80 namevhost orlybg.kludgecruf.com (/etc/apache2/sites-enabled/orlybg.kludgecruf.com.conf:1)

Have another answer? Share your knowledge.