0

I am running my site on my VPS. Let's call it example.com. I want to add a subdomain, sub.example.com. I've added an A-record, created the folder /var/www/sub and added the VirtualHost part. My config file in /apache2/sites-available now looks like this:

<VirtualHost *:80>
    ServerName example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html/

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

<VirtualHost *:80>
    ServerName sub.example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/sub/

    <Directory /var/www/sub>
        Allow from all
        Options +Indexes
    </Directory>

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

Unfortunately, this is not working. Whenever I browse to this subdomain I get redirected to the main site. It seems like apache doesn't redirect me to the subfolder and its contents. How can I get this to work?

Oh, here's the output of apache2ctl -S:

VirtualHost configuration:
*:443                  example.com (/etc/apache2/sites-enabled/000-default-le-ssl.conf:2)
*:80                   is a NameVirtualHost
         default server example.com (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost example.com (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost sub.example.com (/etc/apache2/sites-enabled/000-default.conf:36)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl 
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
  • What is the problem you are having? – Michael Hampton Dec 8 '16 at 16:15
  • @MichaelHampton added the problem in my main post, thanks for noticing. – BlackFayah Dec 8 '16 at 22:27
0

I managed to fix this problem by applying the settings to the config file for port 443 as well. I totally forgot to mention this (and the config file itself too).

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html

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

<VirtualHost *:443>
    ServerName sub.example.com
    ServerAlias www.sub.example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/app

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</IfModule>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.