Place MediaWiki inside a Virtual Host

From SaruWiki
Jump to navigation Jump to search

The problem

The Debian installation of MediaWiki is quite simple, and immediately after installation you can visit, and find your new wiki ready for you. However, if you start using VirtualHosts, and create a second site, then a visit to will reveal the same wiki instance. In fact, whatever virtual host you deploy, on the URI path /wiki you'll always find your single wiki instance deployed.

This might be what you want, but usually it is not. Reason for that would of course be that you customize your wiki to show your address in the title bar, or that all subject matter is about "example", and not about "alsoexample".

The cause

So the URI /wiki is pointing to your wiki for every VirtualHost you deploy. The reason that your wiki is behaving like this, is that the Debian installation has created a symlink /etc/apache2/conf.d/mediawiki that's pointing to /etc/mediawiki/apache.conf. The contents of the conf.d directory are included in the Apache configuration, so the MediaWiki configuation is nicely read and understood. However, the apache.conf configuration file is not VirtualHost compatible; it just aliases the web folder /wiki to the /var/lib/mediawiki directory, without regards for the virtual host name that's called upon. In fact, the first few lines of apache.conf admit as much:

# Uncomment this to add an alias.
# This does not work properly with virtual hosts..
Alias /wiki /var/lib/mediawiki

The solution

First make sure the MediaWiki configuration is not loaded by Apache2 outside of the VirtualHost settings

unlink /etc/apache2/conf.d/mediawiki.conf

Next, open the site-configuration for the site that should receive the MediaWiki setting, e.g. /etc/apache2/sites-available/ In this configuration, include the mediawiki setting, by inserting the following line somewhere within the <VirtualHost> declaration:

Include /etc/mediawiki/apache.conf

This means that, if you've not done any customization yet on your Apache2 installation, the site declaration /etc/apache2/sites-available/default could look something like this:

<VirtualHost *:80>
       ServerAdmin webmaster@localhost

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

       ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
       <Directory "/usr/lib/cgi-bin">
               AllowOverride None
               Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
               Order allow,deny
               Allow from all

       Include /etc/mediawiki/apache.conf

       ErrorLog /var/log/apache2/error.log

       # Possible values include: debug, info, notice, warn, error, crit,
       # alert, emerg.
       LogLevel warn

       CustomLog /var/log/apache2/access.log combined

   Alias /doc/ "/usr/share/doc/"
   <Directory "/usr/share/doc/">
       Options Indexes MultiViews FollowSymLinks
       AllowOverride None
       Order deny,allow
       Deny from all
       Allow from ::1/128


To effectuate the change, restart Apache2:

invoke-rc.d apache2 restart

This should ensure that the /wiki path stays active under your main/intended site, and not under any of your other sites.

Note: if you need this solution under the SSL variant of the site as well, repeat this for the relevant config file, e.g.