Ubuntu subdomain not working

apache-2.2Ubuntuubuntu-14.04

I'm trying to set up a subdomain with Apache2 on my Ubuntu droplet, I've followed this tutorial to the T but every time I navigate to my subdomain, I get the default apache landing page.

So I've set up separate conf files for both my main and sub domains,

/etc/apache2/sites-available/mainsite.conf:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/mainsite/public_html
    ServerName www.example.com
    ServerAlias example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

/etc/apache2/sites-available/sub.conf:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/submainsite/public_html
    ServerName sub.example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

What am I doing wrong? I don't have much server side experience, so please pardon my ignorance on the matter.

EDIT:

Both sites are linked, I did run a2ensite.

When running apachectl -S I get:

 default server sub.example.com (/etc/apache2/sites-enabled/000-default.conf:1)
 port 80 namevhost sub.example.com (/etc/apache2/sites-enabled/000-default.conf:1)
 port 80 namevhost sub.example.com (/etc/apache2/sites-enabled/sub.conf:1)
 port 80 namevhost www.example.com (/etc/apache2/sites-enabled/mainsite.conf:1)
         alias example.com

Best Answer

You defined the default server in 000-default.conf as sub.example.com and it is pointed to the Apache test page. Because it comes first, it overrides the second sub.example.com virtual host you had defined in sub.conf.

To fix the problem, edit 000-default.conf and remove ServerName sub.example.com from it.

Related Topic