Why do we need the <Directory> section in the following virtual host's config file?

Here is the article explaining how to install MediaWiki on Ubuntu Server.

And here's how they tell us to configure the virtual host:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www//mediawiki/
    ServerName wiki.your-domain.com

    <Directory /var/www/html/mediawiki/>
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog /var/log/apache2/mediawiki_error
    CustomLog /var/log/apache2/mediawiki_access common
</VirtualHost>

I don't understand this part:

<Directory /var/www/html/mediawiki/>
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

My wiki's location is /var/www/mediawiki/ as you can see from the DocumentRoot directive. There's no mediawiki folder in /var/www/html/. The only thing that's there is index.html file. So, why do we need that section anyway? What am I missing here?

Also, why do we use // in DocumentRoot /var/www//mediawiki/? According to the official Apache documentation we don't need // here, but if I run apachectl configtest I get Syntax Ok and everything works just fine.


Solution 1:

My wiki's location is /var/www/mediawiki/ as you can see from the DocumentRoot directive.
There's no mediawiki folder in /var/www/html/. The only thing that's there is index.html file.
So, why do we need that section anyway? What am I missing here?

It's actually not needed for /var/www (well, it is, but your standard apache2.conf already has it).

The main point of this configuration is to tell Apache to allow access to that specific location. By default, access to any file is denied unless the configuration says otherwise. In Apache 2.4 this is normally done using Require, while in Apache 2.2 blanket rules (and IP-based rules) were done using Allow.

Another thing done by this section is enabling .htaccess file processing using the AllowOverride option.

Your standard apache2.conf will already contain a <Directory> section that grants everyone access to /var/www (and enables .htaccess) and there is no need to repeat that per-vhost. But if your webapp was stored somewhere else (such as /home or /usr/share) then you would need a <Directory> section for that.

Also, why do we use // in DocumentRoot /var/www//mediawiki/?

Because the author forgot to delete the extra / when editing the path. As the <Directory> section suggests, it was originally "/var/www/html/mediawiki", but then they changed their mind.

In most cases (with a few rare exceptions), multiple consecutive slashes in a path mean the same thing as one /.