Template:MediaWiki tips

From

Jump to: navigation, search

Contents

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):
sudo cp /var/lib/mediawiki/config/LocalSettings.php /etc/mediawiki
sudo mv /var/lib/mediawiki/config/LocalSettings.php /var/lib/mediawiki/config/LocalSettings_at_install.php

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

There are lots of configuration settings that can be set in this file. (See the MediaWiki manual.)

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:
#User restrictions
#Account creation by anonymous users
$wgGroupPermissions['*']['createaccount'] = false;
#Account creation by registered users
$wgGroupPermissions['user']['createaccount'] = false;
#Account creation by sysops
$wgGroupPermissions['sysop']['createaccount'] = true;
Note: * stands for anonymous users, user stands for confirmed users, and sysop obviously stands for sysops.
  • Disallow page editing, page creation, or talk page creation by anonymous users. Add the lines:
#Anonymous user permissions
$wgGroupPermissions['*']['edit'] = false;
$wgGroupPermissions['*']['createpage'] = false;
$wgGroupPermissions['*']['createtalk'] = false;
  • 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.
#Uploads rules
## To enable image uploads, make sure the 'images' directory
## is writable, then set this to true:
#$wgEnableUploads       = false;
$wgEnableUploads = true;
#Only allow restricted uploads
$wgCheckFileExtensions = true;
$wgStrictFileExtensions = true;
$wgFileExtensions = array('png', 'gif', 'jpg'); 
#Permissions for uploads
#Not for Anonymous
$wgGroupPermissions['*']['upload'] = false;
$wgGroupPermissions['*']['reupload'] = false;
$wgGroupPermissions['*']['reupload-shared'] = false;
#Uploads (but not re-uploads) for Users
$wgGroupPermissions['user']['upload'] = true;
$wgGroupPermissions['user']['reupload']        = false;
$wgGroupPermissions['user']['reupload-shared'] = false;
#Sysops
$wgGroupPermissions['sysop']['upload'] = true;
$wgGroupPermissions['sysop']['reupload'] = true;
$wgGroupPermissions['sysop']['reupload-shared'] = true;
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:
# Increase the maximum allowed filesize for uploads (in Mb)
ini_set( 'post_max_size', '50M' );
ini_set( 'upload_max_filesize', '50M' );
  • 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):
sudo mv /usr/share/mediawiki/skins/common/images/wiki.png /usr/share/mediawiki/skins/common/images/wikioriginal.png
sudo cp /home/user/WikiLogo.png /usr/share/mediawiki/skins/common/images/wiki.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:

  • If you do not have a backup folder, make one now:
sudo mkdir /etc/mediawiki/backups
sudo chmod -R 777 /etc/mediawiki/backups
  • Edit your LocalSettings.php file:
sudo nano /etc/mediawiki/LocalSettings.php

and add the lines:

##Database administrative user/password
$wgDBadminuser     = $wgDBuser;
$wgDBadminpassword = $wgDBpassword;
  • then run the XML dump script from a command-line terminal:
sudo php /usr/share/mediawiki/maintenance/dumpBackup.php --current > /etc/mediawiki/backups/MediaWikiBackup_DateToday
Note: I usually specify today's date in place of DateToday.
Note: To use this, php5-cli must already have been installed:
sudo apt-get install php5-cli
  • If you wish to protect this backup folder, you can change the permissions.
sudo chmod -R 444 /etc/mediawiki/backups
Import XML dump

To import the XML dump you made:

sudo php /usr/share/mediawiki/maintenance/importDump.php /etc/mediawiki/backups/MediaWikiBackup_DateToday

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

Wiki -> toolbox -> Special pages -> Page tools -> Import pages -> saved_export.xml

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.

Upgrading

See Upgrading MediaWiki.

  • The primary installation folder is at /usr/share/mediawiki, but user files are also stored in /etc/mediawiki and /var/lib/mediawiki.

Backup and restore the MySQL database

  • This is an alternative that is necessary if you wish to backup during a migration of your wiki. The best way is to backup the original database with a MySQL dump:
mysqldump -u user -p databasename > wikidatabasebackupfile.sql
or, if on a remote host:
mysqldump -h hostname -u username -p databasename > wikidatabasebackupfile.sql
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.

mysql -u root -p
mysql> DROP DATABASE mysqlexampledatabase;
mysql> quit

If your MySQL superuser name is something other than root, then use that, of course. Don't forget the semicolon ( ; ) at the end of each MySQL command.

Of course, once you erase the database, you must re-create a blank one for use with MediaWiki.

sudo dpkg-reconfigure mediawiki

Then you can restore the backup (as created above with mysqldump) into the newly recreated (but still empty) database.

mysql -u username -p databasename < wikidatabasebackupfile.sql

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).
  • Copy the database dumpfile from the old site to the new site.
  • 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).
sudo mkdir /etc/mediawiki/mywiki_1
sudo mkdir /etc/mediawiki/mywiki_2
  • Create an upload folder for images/files in each wiki folder:
sudo mkdir /etc/mediawiki/mywiki_1/images
sudo mkdir /etc/mediawiki/mywiki_2/images

You can add an .htaccess file to each images folder (as described above) for better security.

  • The images folders should belong to the group www-data (the Apache 2 group), and the group should have "Can View & Modify Content" permissions.
sudo chown root:www-data /etc/mediawiki/mywiki_1/images
sudo chown root:www-data /etc/mediawiki/mywiki_2/images
sudo chmod 664 /etc/mediawiki/mywiki_1/images
sudo chmod 664 /etc/mediawiki/mywiki_2/images
  • 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:
 sudo mv /var/lib/mediawiki/config/LocalSettings.php LocalSettings_original.php
  • Edit the configuration file so it recognizes MediaWiki:
sudo nano /etc/mediawiki/apache.conf

Uncomment (remove the #) the line:

Alias /mediawiki /var/lib/mediawiki
  • Restart Apache:
sudo /etc/init.d/apache2 restart
  • Give the /var/lib/mediawiki/config folder read/write permissions during installation:
sudo chmod 777 /var/lib/mediawiki/config
  • Run/install MediaWiki from the (Konqueror/Firefox) web browser by logging into:
http://localhost/mediawiki
  • Wiki name: My Wiki 1
  • Contact e-mail: webmaster@mydomain.org
  • Admin username: wiki1_admin -> Password: wiki1_admin_pw
  • Object caching: No caching
  • E-mail features (all): disabled (optional)
  • Database configuration: MySQL -> Database host: localhost -> Database name: wiki1db -> DB username: wiki1user -> DB password: wiki1pw -> Superuser account: Use superuser account (ticked) -> Superuser name: root -> Superuser password: root_mysql_pw -> Database table prefix: wiki1_
  • Copy your LocalSettings.php configuration file to /etc/mediawiki/mywiki_1 (and make a backup of the original):
sudo cp /var/lib/mediawiki/config/LocalSettings.php /etc/mediawiki/mywiki_1
sudo mv /var/lib/mediawiki/config/LocalSettings.php /var/lib/mediawiki/config/LocalSettings_wiki1_install.php
  • Repeat the MediaWiki installation from the (Konqueror/Firefox) web browser by again logging into:
http://localhost/mediawiki
  • Wiki name: My Wiki 2
  • Contact e-mail: webmaster@mydomain.org
  • Admin username: wiki2_admin -> Password: wiki2_admin_pw
  • Object caching: No caching
  • E-mail features (all): disabled (optional)
  • Database configuration: MySQL -> Database host: localhost -> Database name: wiki2db -> DB username: wiki2user -> DB password: wiki2pw -> Superuser account: Use superuser account (ticked) -> Superuser name: root -> Superuser password: root_mysql_pw -> Database table prefix: wiki2_
  • Copy your LocalSettings.php configuration file to /etc/mediawiki/mywiki_2 (and make a backup of the original):
sudo cp /var/lib/mediawiki/config/LocalSettings.php /etc/mediawiki/mywiki_2
sudo mv /var/lib/mediawiki/config/LocalSettings.php /var/lib/mediawiki/config/LocalSettings_wiki2_install.php
  • You can repeat this multiple times if you want more wikis.
  • Re-edit the configuration file:
sudo nano /etc/mediawiki/apache.conf

Re-comment (add the #) the line:

#Alias /mediawiki /var/lib/mediawiki
  • Restart Apache:
sudo /etc/init.d/apache2 restart
  • Edit your LocalSettings.php configuration file for each wiki (use the gedit text editor instead of kate if using Ubuntu instead of Kubuntu):
sudo kate /etc/mediawiki/mywiki_1/LocalSettings.php
sudo kate /etc/mediawiki/mywiki_2/LocalSettings.php
Make sure the following lines are included in the LocalSettings.php file, replacing similar lines that already exist in the file and substituting mywiki_1 or mywiki_2 where appropriate:
# If PHP's memory limit is very low, some operations may fail.
ini_set( 'memory_limit', '96M' );
#
#$wgScriptPath             = "/mediawiki";
$wgScriptPath              = "/mywiki_1";
$wgLogo                    = "$wgScriptPath/images/WikiLogo.png";
#
$wgUploadDirectory         = $_SERVER['DOCUMENT_ROOT'].'/mywiki_1/images';
$wgUploadPath              = "$wgScriptPath/images";
#
#Database administrative user/password
$wgDBadminuser             = $wgDBuser;
$wgDBadminpassword         = $wgDBpassword;
#
#These are set for initial maximum security. They can be changed later.
#
#User restrictions
#Account creation by anonymous users
$wgGroupPermissions['*']['createaccount']       = false;
#Account creation by registered users
$wgGroupPermissions['user']['createaccount']    = false;
#Account creation by sysops
$wgGroupPermissions['sysop']['createaccount']   = true;
#
#Anonymous user permissions
$wgGroupPermissions['*']['edit']                = false;
$wgGroupPermissions['*']['createpage']          = false;
$wgGroupPermissions['*']['createtalk']          = false;
#
#Uploads rules
## To enable image uploads, make sure the 'images' directory
## is writable, then set this to true:
#$wgEnableUploads                               = false;
$wgEnableUploads                                = true;
#Only allow restricted uploads
$wgCheckFileExtensions                          = true;
$wgStrictFileExtensions                         = true;
$wgFileExtensions          = array('png', 'gif', 'jpg'); 
#Permissions for uploads
#Not for Anonymous
$wgGroupPermissions['*']['upload']              = false;
$wgGroupPermissions['*']['reupload']            = false;
$wgGroupPermissions['*']['reupload-shared']     = false;
#Uploads (but not re-uploads) for Users
$wgGroupPermissions['user']['upload']           = true;
$wgGroupPermissions['user']['reupload']         = false;
$wgGroupPermissions['user']['reupload-shared']  = false;
#Sysops
$wgGroupPermissions['sysop']['upload']          = true;
$wgGroupPermissions['sysop']['reupload']        = true;
$wgGroupPermissions['sysop']['reupload-shared'] = true;
#
#For ReCaptcha -- this requires installing the Recaptcha extension
#
#require_once( "$IP/extensions/recaptcha/ReCaptcha.php" );
# Sign up for these at http://recaptcha.net/api/getkey
#$recaptcha_public_key = ' xyxyxyxyxyxyxyxyx ';
#$recaptcha_private_key = ' ababababababababa ';
#
#The clears the cache daily, which I use to change rotating content (pictures, fortunes, etc.) daily.
#
require("includes/GlobalFunctions.php");
$wgCacheEpoch = wfTimestamp( TS_MW, time() - 86400 ); # 60*60*24 = 1 day
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;
  • Link the files from your installation directory to each wiki folder:
sudo ln -s /usr/share/mediawiki/* /etc/mediawiki/mywiki_1/.
sudo ln -s /usr/share/mediawiki/* /etc/mediawiki/mywiki_2/.
  • For each wiki, create a subfolder in the Apache 2 folder /var/www.
sudo mkdir /var/www/Mywiki_1
sudo mkdir /var/www/MyWiki_2
  • For each wiki, create a symbolic link from the Apache 2 subfolder to the main wiki folder:
sudo ln -s /etc/mediawiki/mywiki_1 /var/www/MyWiki_1/mywiki_1
sudo ln -s /etc/mediawiki/mywiki_2 /var/www/MyWiki_2/mywiki_2
Note: It is possible to create nested subwikis for each of the primary wikis. See the next section.
  • For each wiki, create and edit a virtual host (vhost) Apache 2 configuration file (for example, /etc/apache2/sites-available/mywiki_1vhost). (Use the gedit text editor instead of kate if using Ubuntu instead of Kubuntu.):
sudo kate /etc/apache2/sites-available/mywiki_1vhost
so that the lines are similar to:
<VirtualHost *:80>
UseCanonicalName off
#
DocumentRoot /var/www/MyWiki_1
DirectoryIndex index.php index.html
#
ServerName mywiki_1.mydomain.org
ServerAlias *.mywiki_1.mydomain.org
# 
RewriteEngine On
RewriteCond %{REQUEST_URI}  !^subwiki1*
RewriteCond %{REQUEST_URI}  !^subwiki2*
RewriteRule   ^/(/.*|)$  /mywiki_1/$1  [R]
#
<Directory /var/www/MyWiki_1>
Options Indexes FollowSymLinks MultiViews
Options FollowSymLinks MultiViews
#AllowOverride None
Order allow,deny
allow from all
</Directory>
# 
</VirtualHost>
Create a virtual host file for mywiki_2 as well.
Pay attention to the rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_URI}  !^subwiki1*
RewriteCond %{REQUEST_URI}  !^subwiki2*
RewriteRule   ^/(/.*|)$  /mywiki_1/$1  [R]

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:
sudo ln -s /etc/apache2/sites-available/mywiki_1vhost /etc/apache2/sites-enabled
sudo ln -s /etc/apache2/sites-available/mywiki_2vhost /etc/apache2/sites-enabled
  • Make sure the rewrite engine is enabled:
sudo a2enmod rewrite
  • Restart Apache:
sudo /etc/init.d/apache2 restart
  • The two separate wiki sites will now be available:
http://mywiki_1.mydomain.org
and
http://mywiki_2.mydomain.org

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).
sudo mkdir /etc/mediawiki/subwiki1
sudo mkdir /etc/mediawiki/subwiki2
  • Create an upload folder for images/files in each subwiki folder:
sudo mkdir /etc/mediawiki/subwiki1/images
sudo mkdir /etc/mediawiki/subwiki2/images

You can add an .htaccess file to each images folder (as described above) for better security.

  • The images folders should belong to the group www-data (the Apache2 group), and the group should have "Can View & Modify Content" permissions.
sudo chown root:www-data /etc/mediawiki/subwiki1/images
sudo chown root:www-data /etc/mediawiki/subwiki2/images
sudo chmod 664 /etc/mediawiki/subwiki1/images
sudo chmod 664 /etc/mediawiki/subwiki2/images
  • 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:
 sudo mv /var/lib/mediawiki/config/LocalSettings.php LocalSettings_original.php
  • Run/install MediaWiki from the (Konqueror/Firefox) web browser by logging into:
http://localhost/mediawiki
  • Wiki name: My Subwiki 1
  • Contact e-mail: webmaster@mydomain.org
  • Admin username: wiki1_admin -> Password: wiki1_admin_pw
  • Object caching: No caching
  • E-mail features (all): disabled (optional)
  • Database config: MySQL -> Database host: localhost -> Database name: subwiki1db -> DB username: subwiki1user -> DB password: subwiki1pw -> Superuser account: Use superuser account (ticked) -> Superuser name: root -> Superuser password: root_mysql_pw -> Database table prefix: subwiki1_
  • Copy your LocalSettings.php configuration file to /etc/mediawiki/subwiki1 (and make a backup of the original):
sudo cp /var/lib/mediawiki/config/LocalSettings.php /etc/mediawiki/subwiki1
sudo mv /var/lib/mediawiki/config/LocalSettings.php /var/lib/mediawiki/config/LocalSettings_subwiki1_install.php
  • Repeat the MediaWiki installation from the (Konqueror/Firefox) web browser by again logging into:
http://localhost/mediawiki
  • Wiki name: My Subwiki 2
  • Contact e-mail: webmaster@mydomain.org
  • Admin username: wiki2_admin -> Password: wiki2_admin_pw
  • Object caching: No caching
  • E-mail features (all): disabled (optional)
  • Database config: MySQL -> Database host: localhost -> Database name: subwiki2db -> DB username: subwiki2user -> DB password: subwiki2pw -> Superuser account: Use superuser account (ticked) -> Superuser name: root -> Superuser password: root_mysql_pw -> Database table prefix: subwiki2_
  • Copy your LocalSettings.php configuration file to /etc/mediawiki/subwiki2 (and make a backup of the original):
sudo cp /var/lib/mediawiki/config/LocalSettings.php /etc/mediawiki/subwiki2
sudo mv /var/lib/mediawiki/config/LocalSettings.php /var/lib/mediawiki/config/LocalSettings_subwiki2_install.php
  • You can repeat this multiple times if you want more wikis.
  • The LocalSettings.php configuration file for each wiki must be edited. See this tutorial. There are many security settings that must be changed before going live, or the site will certainly be hacked.
Edit your LocalSettings.php configuration file for each subwiki (use the gedit text editor instead of kate if using Ubuntu instead of Kubuntu):
sudo kate /etc/mediawiki/subwiki1/LocalSettings.php
sudo kate /etc/mediawiki/subwiki2/LocalSettings.php
Make sure the following lines are included in the LocalSettings.php file, replacing similar lines that already exist in the file and substituting subwiki1 or subwiki2 where appropriate:
# If PHP's memory limit is very low, some operations may fail.
ini_set( 'memory_limit', '96M' );
#
#$wgScriptPath             = "/mediawiki";
$wgScriptPath              = "/subwiki1";
$wgLogo                    = "$wgScriptPath/images/WikiLogo.png";
#
$wgUploadDirectory         = $_SERVER['DOCUMENT_ROOT'].'/subwiki1/images';
$wgUploadPath              = "$wgScriptPath/images";
#
#Database administrative user/password
$wgDBadminuser             = $wgDBuser;
$wgDBadminpassword         = $wgDBpassword;
#
#These are set for initial maximum security. They can be changed later.
#
#User restrictions
#Account creation by anonymous users
$wgGroupPermissions['*']['createaccount']       = false;
#Account creation by registered users
$wgGroupPermissions['user']['createaccount']    = false;
#Account creation by sysops
$wgGroupPermissions['sysop']['createaccount']   = true;
#
#Anonymous user permissions
$wgGroupPermissions['*']['edit']                = false;
$wgGroupPermissions['*']['createpage']          = false;
$wgGroupPermissions['*']['createtalk']          = false;
#
#Uploads rules
## To enable image uploads, make sure the 'images' directory
## is writable, then set this to true:
#$wgEnableUploads                               = false;
$wgEnableUploads                                = true;
#Only allow restricted uploads
$wgCheckFileExtensions                          = true;
$wgStrictFileExtensions                         = true;
$wgFileExtensions          = array('png', 'gif', 'jpg'); 
#Permissions for uploads
#Not for Anonymous
$wgGroupPermissions['*']['upload']              = false;
$wgGroupPermissions['*']['reupload']            = false;
$wgGroupPermissions['*']['reupload-shared']     = false;
#Uploads (but not re-uploads) for Users
$wgGroupPermissions['user']['upload']           = true;
$wgGroupPermissions['user']['reupload']         = false;
$wgGroupPermissions['user']['reupload-shared']  = false;
#Sysops
$wgGroupPermissions['sysop']['upload']          = true;
$wgGroupPermissions['sysop']['reupload']        = true;
$wgGroupPermissions['sysop']['reupload-shared'] = true;
#
#For ReCaptcha -- this requires installing the Recaptcha extension
#
#require_once( "$IP/extensions/recaptcha/ReCaptcha.php" );
# Sign up for these at http://recaptcha.net/api/getkey
#$recaptcha_public_key = ' xyxyxyxyxyxyxyxyx ';
#$recaptcha_private_key = ' ababababababababa ';
#
#The clears the cache daily, which I use to change rotating content (pictures, fortunes, etc.) daily.
#
require("includes/GlobalFunctions.php");
$wgCacheEpoch = wfTimestamp( TS_MW, time() - 86400 ); # 60*60*24 = 1 day
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:
sudo mkdir /var/www/MyWiki
sudo ln -s /etc/mediawiki/subwiki1 /var/www/MyWiki/subwiki1
sudo ln -s /etc/mediawiki/subwiki2 /var/www/MyWiki/subwiki2
  • Link the files from your installation directory to each subwiki folder:
sudo ln -s /usr/share/mediawiki/* /etc/mediawiki/subwiki1/.
sudo ln -s /usr/share/mediawiki/* /etc/mediawiki/subwiki2/.
  • Create and edit a virtual host (vhost) Apache configuration file (for example, /etc/apache2/sites-available/mywikivhost). (Use the gedit text editor instead of kate if using Ubuntu instead of Kubuntu.):
sudo kate /etc/apache2/sites-available/mywikivhost
so that the lines are similar to:
<VirtualHost *:80>
UseCanonicalName off
#
DocumentRoot /var/www/MyWiki
DirectoryIndex index.php index.html
#
ServerName mywiki.mydomain.org
ServerAlias *.mywiki.mydomain.org
# 
RewriteEngine On
RewriteCond %{REQUEST_URI}  !^subwiki1*
RewriteCond %{REQUEST_URI}  !^subwiki2*
RewriteRule   ^/(/.*|)$  /subwiki1/$1  [R]
#
<Directory /var/www/MyWiki>
Options Indexes FollowSymLinks MultiViews
Options FollowSymLinks MultiViews
#AllowOverride None
Order allow,deny
allow from all
</Directory>
# 
</VirtualHost>
Pay attention to the rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_URI}  !^subwiki1*
RewriteCond %{REQUEST_URI}  !^subwiki2*
RewriteRule   ^/(/.*|)$  /subwiki1/$1  [R]

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:
sudo ln -s /etc/apache2/sites-available/mywikivhost /etc/apache2/sites-enabled
  • Make sure the rewrite engine is enabled:
sudo a2enmod rewrite
  • Restart Apache:
sudo /etc/init.d/apache2 restart
  • The two separate wiki sites will now be available:
http://mywiki.mydomain.org or http://mywiki.mydomain.org/subwiki1
and
http://mywiki.mydomain.org/subwiki2

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:
sudo ln -s  sudo ln -s /etc/mediawiki/subwiki_2 /var/www/Wikis/subwiki_2
sudo ln -s  sudo ln -s /etc/mediawiki/subwiki_3 /var/www/Wikis/subwiki_3
and the virtual host file for www.mydomain.org points only to the directory /var/www/Wikis, then in LocalSettings.php the $wgScriptPath variables should be:
$wgScriptPath = "/subwiki_2";
or
$wgScriptPath = "/subwiki_3";

Build your site

You should be ready to go. Start building your site.

Personal tools
Sponsor
     Asus X200CA