MediaWiki tips

MediaWiki is the free, open source server software that Wikipedia uses. It is scalable to very large uses. It runs on the LAMP server stack (which uses the MySQL database and is available as an installation option with the (K)ubuntu server), or it can be used with a PostgreSQL database. (Other instructions are also available here.)

Install MediaWiki

Install from the repositories:

sudo apt-get install mediawiki

Edit the configuration file so it recognizes MediaWiki (use the gedit text editor instead of kate if using Ubuntu instead of Kubuntu):

sudo kate /etc/mediawiki/apache.conf

Uncomment (remove the #) the line:

Alias /mediawiki /var/lib/mediawiki

Restart Apache:

sudo /etc/init.d/apache2 restart

Run/install MediaWiki by logging into:

http://localhost/mediawiki

You will be prompted for configuration variables to be set. You can accept the default database name to be created (wikidb) and the default user for the database (wikiuser). Choose a unique password for this wiki database. (You don't need to remember this password for anything later, and, unfortunately, it isn't saved in an encrypted manner, so don't use a sensitive password that you use anywhere else). The trickiest part is the MySQL superuser name/ superuser password. Hopefully you remember your MySQL superuser that you set at the time of LAMP (or MySQL) installation.

Copy your local settings configuration file to /etc/mediawiki (and make a backup of the original):

Edit your configuration variables there (use the gedit text editor instead of kate if using Ubuntu instead of Kubuntu):

sudo kate /etc/mediawiki/LocalSettings.php

If you are using a virtual host server, make a symbolic link (named in this example mywiki) from your /usr/share/mediawiki installation folder to your /var/www folder:

sudo ln -s /usr/share/mediawiki /var/www/mywiki

Then make sure you have an Apache virtual hosts configuration file (in /etc/apache2/sites-available) that points to /var/www/mywiki as the DocumentRoot. Make a symbolic link from your virtual host configuration file in /etc/apache2/sites-available to /etc/apache2/sites-enabled to enable it. Restart Apache 2 after enabling the sites. (Warning: MediaWiki is not secure at installation and can be easily hacked by new users. Do not publish your wiki to the Internet before reading all the instructions and changing the configuration file (LocalSettings.php) so that it is more secure.)
You would then access the database at:

http://my.virtualwikihost.org

ReCaptcha

I strongly recommend installing a Captcha mechanism immediately upon installation. See this section.

Editing the LocalSettings.php configuration file

But during the time you are learning how to administer MediaWiki, you ought to take some basic security steps. Otherwise you will be hacked within a few minutes of publishing your wiki to the web. I suggest taking a gander at how to prevent access before you start. Shut down everything and then remove restrictions one by one once you are comfortable that you have taken reasonable security precautions. (I have seen a lot of hacked wikis on the web lately).

Disallow user creation by anyone other than the sysop. (There are a lot of clever hackers out there who can change the settings faster than you can, if you let them create an account.) Add these lines somewhere to LocalSettings.php:

Determine uploads privileges. Initially, I restrict allowed file uploads to .jpg, .gif, and .png images. My first day I found .xls, extensions, and others stuff uploaded by hackers, before I figured out how to stop this. I further restricted uploads by anonymous users.

As a further precaution, I made a separate images folder ( /etc/mediawiki/images ) that I use for my uploads. I then make an Alias to this folder in /etc/mediawiki/apache.conf:

sudo nano /etc/mediawiki/apache.conf

and adding the line:

Alias /images /etc/mediawiki/images

Lastly, I add a configuration file into the 'images' folder that prevents any scripts that (somehow) get uploaded from executing:

cd /etc/mediawiki/images
sudo nano .htaccess

and adding the lines:

Options -Indexes
# No php execution in the upload area
php_admin_flag engine off

Note: If you are using multiple wikis (as outline below), images are accessed through a script. Using the php_admin_flag engine off option will disable this ability. Therefore, do not use this line in .htaccess when creating multiple wikis. (For security options when using a multiple wiki farm, read about img_ath.php.)

See this section to see why I have taken these steps. It might seem paranoid, but I was hacked within one hour of installing MediaWiki the first time I installed it. Now that I've learned some basics about security, I've not been hacked again (to my knowledge!)

Increase PHP memory limits

If insufficient memory is allocated for PHP to process the wiki, an error ("memory exhausted") will result. This line in LocalSettings.php is pretty important, therefore, and should be near the beginning:

# If PHP's memory limit is very low, some operations may fail.
ini_set( 'memory_limit', '96M' );

Increase PHP uploaded file size limits

The default for filesize uploads in MediaWiki is only 2 Mb, which is entirely insufficient for most purposes. Add these lines to LocalSettings.php to increase the maximum filesize for uploads:

In addition, the global maximum file sizes allowed in PHP5 for Apache 2 must be increased as well. The PHP scripting language is used for uploads. Absolute upload limits for the Apache webserver are set in a PHP configuration file and must be changed there.

Your uploads are probably larger than the default upload limits of PHP (set at 2 Mb, or "2M", by default), so we will need to increase those. In the example below, I will change the upload limit to 100 Mb ("100M"). Two parameters must be changed in the php.ini configuration file in /etc/php5/apache2 (use the gedit text editor instead of kate if using Ubuntu instead of Kubuntu):

cd /etc/php5/apache2
sudo kate php.ini

Change:

post_max_size = 8M

to

post_max_size = 100M

Change:

upload_max_filesize = 2M

to

upload_max_filesize = 100M

Save the file and restart Apache:

sudo /etc/init.d/apache2 restart

Change the default logo

I tried to use the $wglogo setting in the LocalSettings.php file, but in some versions this did not work for me. Instead, I backed up the original logo file and replaced it with the file (135x135 image) I want to use as the logo image (in this example WikiLogo.png):

A transparent background for the logo image is desirable. (You can use Gimp to create an alpha transparency layer for any photo. See these instructions.) You can use Gimp or Gwenview to resize the image. Note that .jpg images do not accept background transparency, but .png images do.

To change the link to which the site-logo image points (when clicked), see this Mediawiki FAQ.

At the same time I set the "Favicon" (the small icon (16x16 image) that appears in a browser's address bar or bookmark list) using the $wgFavicon setting in the LocalSettings.php file. (While a .png image will work, it will not display in all browsers (i.e. Internet Explorer), so I converted it (using Gimp) to an .ico image named favicon.ico and placed it in the root Mediawiki folder.)

Make backups

MediaWiki saves its content files in whichever database you are using as a backend (MySQL or PostgreSQL). For a full backup, you would have to backup the MediaWiki database.

XML dump

It is easiest, however, to backup content with an XML dump, which can then be imported to future (or even past) versions of MediaWiki. See these instructions. In brief:

Import XML dump

Note that when you import XML dumps, it maintains revision dates. if you have pages that are more recent than the imported pages, then the more recent pages will be retained. If you want to promote an imported page to the most recent page, you must do this in the page history section (like usual).

This drove me nuts until I figured this out, because, of course, when you upgrade or reinstall a wiki, the newly created Main Page will be the most recent (not the old Main Page from the imported wiki). The imported Main Page does not show up unless you promote the old version from the history file.

Export individual pages to XML

If you have sufficient privileges, you can export a page (or multiple pages) from within the wiki. For example, to export Ubuntuguide:Precise :

Wiki -> toolbox -> Special pages -> Page tools -> Export pages ->

Large text box: Ubuntu:Precise

Include only the current revision, not the full history: (ticked)

Include templates: (ticked)

Save as file: (ticked)

The saved XML file can then be imported into another MediaWiki wiki using

What I often do is look at the list of Special:AllPages and either copy the entire list or just the pages I want to back up into the Special:Export list.

Full system backup

To backup images, user settings, and other settings, you would also back up the file system (contained variably in the folders /etc/mediawiki, /var/lib/mediawiki, and usr/share/mediawiki). See the MediaWiki backup instructions.

Note that the username and password should be the username and password that were used to create the specific database (not the MySQL root username/password). (If you can't remember what they were, check the LocalSettings.php file for the $wgDBname, $wgDBuser, and $wgDBpassword values).

The database should be restored to an empty database in the new site, because if you re-install a new database in the new site and then attempt to restore your old backed-up database on top of it, there is likely to be incompatibilities between the two. Here the username and password are those for the new empty database just created. (It probably is best to make them the same as those of the imported database.)

mysql -u username -p databasename < wikidatabasebackupfile.sql

Notes: This was successful for me only if backing up and restoring to exactly the same version of MediaWiki. I could not back up the database from one version of MediaWiki then restore to an upgraded version of MediaWiki, because the scripts of the upgraded version of MediaWiki did not access the database in the same manner. I therefore performed upgrades only after moving the database.

Empty a database

I hesitate to put these instructions here. Be careful. This erases your database. Use it only if you are confident that you have made good backups. I use this only if I have created a database by accident (during the MediaWiki installation process) and wish to erase/empty it.

Moving a MediaWiki installation to a new site

Install mediawiki on the new site (sudo apt-get install mediawiki). When creating the database, use the same values as used on the old site. If you can't remember what they were, look at the /etc/mediawiki/LocalSettings.php (or similar) file for the old site (which contains the values for the old site).

On the new site, rename the newly created folders

/etc/mediawiki

/usr/share/mediawiki

/var/lib/mediawiki

to

/etc/mediawiki.bak

/usr/share/mediawiki.bak

/var/lib/mediawiki.bak

Copy the /etc/mediawiki , /usr/share/mediawiki , and /var/lib/mediawiki folders from the old site to the new site. (This needs to be done as the root user, which can be done with sudo dolphin).

Check the LocalSettings.php file (and other folder) permissions to make sure they match the permissions of the original system. (Sometimes during the copy process the ownership of all files and folders will be set to root.)

Notes: I have never been successful in performing an upgrade in the middle of this process. I recommend moving the site exactly, and then performing any upgrades after it is moved.

Install multiple MediaWiki sites

Multiple wikis

This method allows the installation of more than one wiki using different databases (on a single server using the same source code). This setup is transparent to users and is reasonably secure in terms of the images/files directory. This method also allows nested subwikis. Similar methods can be found at the Mediawiki wiki.

Install MediaWiki from packages as usual (if not already done). MediaWiki is installed by default to /usr/share/mediawiki. (If you wish to upgrade to a more recent version, extract the latest MediaWiki tar.gz archive into this folder.) A directory /etc/mediawiki is also created when installing from the package.

In this method, it is not necessary (nor recommended) to edit the /etc/mediawiki/apache.conf file (as is done in the single wiki installation).

Create a folder for each wiki (in this example named mywiki_1 and mywiki_2).

Copy each 135x135 image that you wish to use as a wiki logo (in the upper left corner) into the /etc/mediawiki/mywiki_x/images folder of each of the wikis. Rename it WikiLogo.png in that folder.

Copy the LocalSettings.php configuration file for any existing wiki (if you have already created one) as a backup (just in case something goes wrong):

sudo cp /etc/mediawiki/LocalSettings.php LocalSettings_backup.php

Note that you can also use this LocalSettings.php file for one of the wikis by copying it into one of the wiki subfolders and then editing the appropriate lines in the LocalSettings.php file once it is copied there (see below).

Rename the LocalSettings.php file in the original installation configuration folder to a backup, as well:

In addition, a private wiki page should only be able to be read by registered users, so add these lines to LocalSettings.php for any private wiki:

#This example will disable viewing of all pages not listed in $wgWhitelistRead, then re-enable for registered users only:
$wgGroupPermissions['*']['read'] = false;
# The following line is not actually necessary, since it's in the defaults. Setting
# '*' to false doesn't disable rights for groups that have the right separately set
# to true!
$wgGroupPermissions['user']['read'] = true;

This is a complex rule that means that as long as the REQUEST_URI (which is the part after the server name, i.e. http://mywiki_x.mydomain.org/REQUEST_URI) does not match subwiki1 or subwiki2 (the symbol ! means not), then use mywiki_1 as the default directory. This rule allows the use not only of a primary wiki but also subwikis (see the next section) for each of the primary wikis.

Remember that your virtual host configuration files won't be active until you make symbolic links:

Multiple subwikis

MediaWiki is a very powerful system, but you must choose whether to make it completely public or completely private (it does not yet have fine-grained per-page access controls). One solution is to create one subwiki for private usage and another subwiki for public display.

Below is outlined a method for creating multiple subwikis. Each subwiki will have its own database and its own LocalSettings.php configuration file as well as Images (/Files) directory. However, all the subwikis will share the underlying MediaWiki code (stored in the installation directory). When it is time to upgrade, the files in the installation directory can be upgraded without risking the loss of the files in each subwiki folder.

I have adapted information originally posted here and here. (None of the independent instructions on those sites worked for me, however, so I used a combination of all of them.)

The instructions below worked for me on (K)Ubuntu 9.04 (Jaunty), 9.10 (Karmic), and 10.04 (Lucid) using MediaWiki 1.13 and 1.15 and PHP5.

Install MediaWiki from packages as usual (if not already done). MediaWiki is installed by default to /usr/share/mediawiki. (If you wish to upgrade to a more recent version, extract the latest MediaWiki tar.gz archive into this folder.) A directory /etc/mediawiki is also created when installing from the package.

In this method, it is not necessary (nor recommended) to edit the /etc/mediawiki/apache.conf file (as is done in the single wiki installation).

Create a folder for each subsite (in this example named subwiki1 and subwiki2).

Copy each 135x135 image that you wish to use as a wiki logo (in the upper left corner) into the /etc/mediawiki/subwikix/images folder of each of the subwikis. Rename it WikiLogo.png in that folder.

Copy the LocalSettings.php configuration file for any existing wiki (if you have already created one) as a backup (just in case something goes wrong):

sudo cp /etc/mediawiki/LocalSettings.php LocalSettings_backup.php

Note that you can also use this LocalSettings.php file for one of the subwikis by copying it into a subwiki folder and then changing the appropriate lines in the LocalSettings.php file once it is copied there (see below).

Rename the LocalSettings.php file in the original installation config folder to a backup, as well:

In addition, a private wiki page should only be able to be read by registered users, so add these lines to LocalSettings.php for any private subwiki:

#This example will disable viewing of all pages not listed in $wgWhitelistRead, then re-enable for registered users only:
$wgGroupPermissions['*']['read'] = false;
# The following line is not actually necessary, since it's in the defaults. Setting
# '*' to false doesn't disable rights for groups that have the right separately set
# to true!
$wgGroupPermissions['user']['read'] = true;

Create a subfolder in the Apache folder /var/www.

sudo mkdir /var/www/Mywiki

Note: It is possible to create multiple primary wikis, each with several subwikis. Each primary wiki should have its own subfolder in the Apache folder /var/www. See this section.

Create symbolic links from the Apache subfolder to the subwiki folders:

This is a complex rule that means that as long as the REQUEST_URI (which is the part after the server name, i.e. http://mywiki.mydomain.org/REQUEST_URI) does not match subwiki1 or subwiki2 (the symbol ! means not), then use subwiki1 as the default directory.

Remember that your virtual host configuration file won't be active until you make a symbolic link:

Troubleshooting

If you are trying to add more subsites to an existing multi-site installation, then you must have only one Apache virtual host configuration file pointing to MediaWiki. That virtual host configuration file must point to the main installation files (/usr/share/mediawiki) as if you were installing a single user for the first time. (This is necessary for the installation scripts to create the new database properly.)

This is most easily done by deleting the links in the /etc/apache2/sites-enabled folder that correspond to the already existing subsite virtual hosts configuration files located in the /etc/apache2/sites-available folder. (You can leave the files in the /etc/apache2/sites-available folder alone). Then restart Apache ( sudo /etc/init.d/apache2 restart).

Then make sure that you have a single virtual host file that points to /usr/share/mediawiki in the /etc/apache2/sites-available folder, as well as a link to it in the /etc/apache2/sites-enabled folder. Then restart Apache (sudo /etc/init.d/apache2 restart) again.

After you have finished an additional installation, you can re-enable the specific subsite virtual host configuration files in the /etc/apache2/sites-available folder by again making links from them into the /etc/apache2/sites-enabled folder (and, of course, restarting Apache).

Pay close attention to the variable in LocalSettings.php:

$wgScriptPath = "/mediawiki";

If using multiple wikis, this should be changed.

If your virtual hosts and /var/www symbolic links point the URL directly to the folder in which the subwiki resides, then the variable should be:

$wgScriptPath = "";

For example, if subwiki_1 is located at /etc/mediawiki/subwiki_1 and the virtual host file points the URL to the directory /var/www/subwiki_1, which is symbolically linked to /etc/mediawiki/subwiki_1 using

sudo ln -s /etc/mediawiki/subwiki_1 /var/www/subwiki_1

then in LocalSettings.php the $wgScriptPath variable should be:

$wgScriptPath = "";

If you are using multiple subwikis of the format www.mydomain.org/Subwiki_2 and www.mydomain.org/Subwiki_3, and there is a folder /var/www/Wikis with symbolic links to the various subwikis: