Question

Adding a subdomain on apache2

  • Posted February 2, 2015

Hello. I’m trying to add a subdomain to my website which is running on ubuntu 14.04

I got a domain: example.com and I want to add a subdomain: recipes.example.com.

I have the following folders: /var/www/example.com and /var/www/recipes.example.com. Up until now I had the main domain folder only and everything worked just as expected.

In /etc/apache2/sites-available I got my example.com.conf as well as recipes.example.com.conf

here is my recipes.example.com.conf:

<VirtualHost *:80>

        ServerAdmin me@example.com
        #referring the user to the recipes application
        DocumentRoot /var/www/recipes.example.com/public

        <Directory /var/www/recipes.example.com/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                # Uncomment this directive is you want to see apache2's
                # default start page (in /apache2-default) when you go to /
                #RedirectMatch ^/$ /apache2-default/
        </Directory>

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

</VirtualHost>

I’ve added a CNAME entry for my domain:

CNAME: name: recipes, host: @

However, up at this point, when I access recipes.example.com I get see my actual website, which means that apache has loaded the main website’s folder instead of the subdomain folder.

What am I doing wrong?

Subscribe
Share

Hello There, I am facing a similar issue My primary domain is hotelshivaay.com

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        <Directory /var/www/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

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

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
        </IfModule>

</VirtualHost>

I want a subdomain with bookings.hotelshivaay.com

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName bookings.hotelshivaay.com
        DocumentRoot /var/www/bookings.hotelshivaay.com

        <Directory /var/www/bookings.hotelshivaay.com/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

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

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
        </IfModule>

</VirtualHost>

But it is not working

helped me too…thanx


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

It looks like your virtualhost configuration is missing a ServerName. In name based virtual hosting this field is key and is what apache uses to decide which site to display. You’ll want to add the following line:

ServerName recipes.example.com

To the main section of this configuration right before or after your ServerAdmin directive.

This comment has been deleted

Gracias me ayudo mucho.

I currently have this in my vhost but I’m getting the same issue where this subdomain is going to the root but the domain is going to the appropriate place via it’s own vhost… any idea what I’m doing incorrectly?

    ServerAdmin webmaster@localhost
    ServerName beta.example.com
    ServerAlias www.beta.examplecom


    DocumentRoot /var/www/example.com
    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /var/www/example.com>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>

thanks for the author of this question Its help me a lot :D