Install MediaWiki
MediaWiki is the most popular wiki package and is the open source software that powers Wikipedia, among a great many other sites. This article covers installing MediaWiki to a UNPM Server.
This article assumes that MediaWiki is to be installed to a sub-directory called /wiki/
, though it can be installed to a sub-directory of any name or depth (additional sub-directories) or even to the site's root directory.
Nginx configuration
Package-configs files
Create the package-configs
files mediadwiki.conf
and mediawiki_https.conf
:
username@servername:~$ sudo nano /etc/nginx/package-configs/mediawiki.conf
Add to the new file:
location /wiki/ { rewrite ^/wiki/([^?]*)(?:\?(.*))? /wiki/index.php?title=$1&$2 last; location ~* ^/images/.*.(html|htm|shtml|php|js)$ { types { } default_type text/plain; } location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { try_files $uri /wiki/index.php; expires max; log_not_found off; } location ~ (index|load|api).*\.php$ { include global-configs/php.conf; } location ~ \.php$ { deny all; } }
username@servername:~$ sudo nano /etc/nginx/package-configs/mediawiki_https.conf
Add to the new file:
location /wiki/ { rewrite ^/wiki/([^?]*)(?:\?(.*))? /wiki/index.php?title=$1&$2 last; location ~* ^/images/.*.(html|htm|shtml|php|js)$ { types { } default_type text/plain; } location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { try_files $uri /wiki/index.php; expires max; log_not_found off; } location ~ (index|load|api).*\.php$ { include global-configs/php_https.conf; } location ~ \.php$ { deny all; } }
Sites-available file
Open the sites-available file for the domain:
username@servername:~$ sudo nano /etc/nginx/sites-available/example.com
In the HTTP server block, add:
include package-configs/mediawiki.conf;
In the HTTPS server block, add:
include package-configs/mediawiki_https.conf;
404 error_page
Note that if the error_page
directive is being used to redirect 404 errors, be sure it is in the location /
block:
location / { try_files $uri $uri/ =404; error_page 404 /404.html; }
If it is directly in the server
block, MediaWiki will not attempt to create pages when a wrong page is requested.
Create MediaWiki database and database user
Create a MediaWiki database and database user:
username@servername:~$ sudo mysql -uroot -p MariaDB [(none)]> create database databasename default character set utf8 default collate utf8_general_ci; MariaDB [(none)]> grant all on databasename.* to 'databasenameusername'@'localhost' identified by 'databasenameusernamepassword'; MariaDB [(none)]> exit
Note that the databasename
, databasenameusername
and databasenameusernamepassword
will be required for the MediaWiki configuration process.
Install Mediawiki
Mediawiki will require a few packages to be installed to run:
username@servername:~$ sudo aptitude install imagemagick php5-intl
Make a couple of PHP configuration edits:
username@servername:~$ sudo nano /etc/php5/fpm/conf.d/suhosin.ini
Uncomment (by removing ;
) and change:
suhosin.get.max_value_length = 1024
username@servername:~$ sudo nano /etc/php5/fpm/php-fpm.conf
Add to the file:
security.limit_extensions = .php .html .php5
Test nginx and restart services:
username@servername:~$ sudo nginx -t username@servername:~$ sudo service php5-fpm restart username@servername:~$ sudo service nginx restart
Download Mediawiki:
username@servername:~$ wget http://download.wikimedia.org/mediawiki/1.22/mediawiki-1.22.2.tar.gz username@servername:~$ tar -xzf mediawiki-1.22.2.tar.gz username@servername:~$ mv mediawiki-1.22.2/ /var/www/example.com/public/wiki username@servername:~$ find /var/www/example.com/public/wiki/ -type d | xargs chmod 775 username@servername:~$ find /var/www/example.com/public/wiki/ -type f | xargs chmod 664 username@servername:~$ sudo chown -R www-data:www-data /var/www/example.com/public/wiki/
Navigate to secure https://www.example.com/wiki and complete the Mediawiki installation. Be sure to select 'PHP object caching (APC, XCache or WinCache)' in the Advanced Configuration options near the end of the installation process.
LocalSettings.php
After completing the online setup, MediaWiki will prompt to download the LocalSettings.php
file. This is the main configuration file for MediaWiki. It is easiest to open the file in a text editor and then copy and paste the contents into the LocalSettings.php
file on the server. However, before doing that, the file still requires at least a few edits.
To allow MediaWiki to load in non-secure sessions, but require secure session for logging in, change:
$wgserver = "//www.example.com";
To create a user rights management system better suited to smaller wikis, change:
$wgGroupPermissions['*']['createaccount'] = true;
Then, just below the permissions created by MediaWiki, add:
# The following permissions were added to allow user account creation without # granting new users the ability to edit. The admin accounts may elevate new # users to the added editor role to grant permission to edit $wgGroupPermissions['*']['createtalk'] = false; $wgGroupPermissions['*']['createpage'] = false; $wgGroupPermissions['*']['writeapi'] = false; $wgGroupPermissions['user']['edit'] = false; $wgGroupPermissions['user']['createtalk'] = false; $wgGroupPermissions['user']['createpage'] = false; $wgGroupPermissions['user']['minoredit'] = false; $wgGroupPermissions['user']['move'] = false; $wgGroupPermissions['user']['movefile'] = false; $wgGroupPermissions['user']['move-subpages'] = false; $wgGroupPermissions['user']['move-rootuserpages'] = false; $wgGroupPermissions['user']['reupload-shared'] = false; $wgGroupPermissions['user']['reupload'] = false; $wgGroupPermissions['user']['purge'] = false; $wgGroupPermissions['user']['sendemail'] = false; $wgGroupPermissions['user']['upload'] = false; $wgGroupPermissions['user']['writeapi'] = false; $wgGroupPermissions['editor'] = $wgGroupPermissions['user']; $wgGroupPermissions['editor']['edit'] = true; $wgGroupPermissions['editor']['createtalk'] = true; $wgGroupPermissions['editor']['createpage'] = true; $wgGroupPermissions['editor']['minoredit'] = true; $wgGroupPermissions['editor']['move'] = true; $wgGroupPermissions['editor']['move-subpages'] = true; $wgGroupPermissions['editor']['upload'] = true; $wgGroupPermissions['editor']['reupload'] = true; $wgGroupPermissions['editor']['sendemail'] = true; $wgGroupPermissions['sysop']['edit'] = true; $wgGroupPermissions['sysop']['createpage'] = true; $wgGroupPermissions['sysop']['createtalk'] = true; $wgGroupPermissions['sysop']['minoredit'] = true; $wgWikiEditorModules['toolbar']['global'] = true; $wgWikiEditorModules['toolbar']['user'] = false; $wgGroupPermissions['bureaucrat']['usermerge'] = true;
The *
user group means all users, including anonymous users.
Below that, add:
# Enable requirement for secure login: $wgSecureLogin = true; # Enable pretty URLs $wgUsePathInfo = true; $wgArticlePath = "/wiki/$1";
Now paste the entire contents from the computer's text editor to the LocalSettings.php
file on the server:
username@servername:~$ nano /var/www/example.com/public/wiki/LocalSettings.php
Note that depending on the operating system and editor used, the code may paste in without things such as spaces between paragraphs. This does happen with Notepad++ in Windows 7, but can easily be fixed by first pasting into a new Notepad++ tab and copying from there.
Configuring MediaWiki
MediaWiki configuration is different from most of the popular PHP packages. Configuring is done through the LocalSettings.php
file, extensions, which must be installed server-side with accompanied LocalSettings.php
entries, and various different hard-to-find pages in the web interface. The MediaWiki Manual is an invaluable resource. Below are some simple and common extension and configuration settings.
Sidebar
To edit the MediaWiki Sidebar, while logged in with an admin account, simply navigate to https://www.example.com/wiki/MediaWiki:Sidebar.
Disable page counts
By default, MediaWiki will display page counts at the bottom of every page. This can be disabled with the following:
username@servername:~$ nano /var/www/example.com/public/wiki/LocalSettings.php
Add to the bottom of the file:
# Disables page counters $wgDisableCounters = true;
Change logo image
The logo image location is set in LocalSettings.php
with the $wgLogo
directive, for which the default is "$wgStylePath/common/images/wiki.png";
. Replacing wiki.png
with a different image while using the same name will be overwritten when updating MediaWiki, so it is better to use a file in a different location. MediaWiki updates generally ignore anything in the /images
directory, so putting the logo there and setting $wgLogo = "$wgScriptPath/images/filename";
is a reasonable solution, though other locations may work just as well.
MobileFrontend extension
Although there are options to automatically detect mobile devices and display a mobile-friendly theme, the disadvantage of this feature is it requires disabling caching. A compromise is to install the MobileFrontend extension which creates a Mobile view link at the bottom of every page.
username@servername:~$ wget https://codeload.github.com/wikimedia/mediawiki-extensions-MobileFrontend/legacy.tar.gz/REL1_22 username@servername:~$ tar -xzf REL1_22 username@servername:~$ mv wikimedia-mediawiki-extensions-MobileFrontend-2e0af84/ /var/www/example.com/public/wiki/extensions/MobileFrontend username@servername:~$ find /var/www/example.com/public/wiki/extensions/MobileFrontend/ -type d | xargs chmod 775 username@servername:~$ find /var/www/example.com/public/wiki/extensions/MobileFrontend/ -type f | xargs chmod 664 username@servername:~$ sudo chown -R www-data:www-data /var/www/example.com/public/wiki/extensions/MobileFrontend/ username@servername:~$ rm REL1_22 username@servername:~$ nano /var/www/example.com/public/wiki/LocalSettings.php
Add to the bottom of the file:
# Enables MobileFrontend extension require_once __DIR__ . "/extensions/MobileFrontend/MobileFrontend.php";
Tracking users in Mobile view with Piwik
Due to a bug in MediaWiki, the Piwik Integration extension cannot be used to track users who view MediaWiki in Mobile view. Instead, the MediaWiki:Common.js functionality must be used. Navigate to https://www.example.com/wiki/MediaWiki:Common.js and enter the site's Piwik javascript tracking code. Note that the image tracking code cannot be placed here. Edit LocalSettings.php
.
username@servername:~$ nano /var/www/example.com/wiki/LocalSettings.php
Disable the Piwik Integration Extension, if installed, by commenting out the entries.
# Use the site's JavaScript (MediaWiki:Common.js) $wgUseSiteJS = true;
Piwik Integration extension
The configuration below is intended to work on servers running Piwik as configured in this wiki's Piwik article. Due to the aforementioned bug in MediaWiki, this extension will not track users in Mobile view.
username@servername:~$ wget https://codeload.github.com/DaSchTour/piwik-mediawiki-extension/zip/master username@servername:~$ unzip master username@servername:~$ mv piwik-mediawiki-extension-master/ /var/www/example.com/public/wiki/extensions/Piwik username@servername:~$ find /var/www/example.com/public/wiki/extensions/Piwik/ -type d | xargs chmod 775 username@servername:~$ find /var/www/example.com/public/wiki/extensions/Piwik/ -type f | xargs chmod 664 username@servername:~$ sudo chown -R www-data:www-data /var/www/example.com/public/wiki/extensions/Piwik username@servername:~$ rm master username@servername:~$ nano /var/www/example.com/public/wiki/extensions/Piwik/Piwik.hooks.php
Change these 3 lines:
_paq.push(["setTrackerUrl", u+"js/"]);
g.defer=true; g.async=true; g.src=u+"js/"; s.parentNode.insertBefore(g,s);
<noscript></noscript>
username@servername:~$ nano /var/www/example.com/public/wiki/LocalSettings.php
Add to the bottom of the file:
# Enable Piwik Integration extension require_once __DIR__ . "/extensions/Piwik/Piwik.php"; $wgPiwikURL = "www.example.com/randomsymlink"; $wgPiwikIDSite = "1";
Note that if the path to Piwik has no subdomain or a different subdomain than the example, that should be used and $wgPiwikIDSite
should be set to the site ID of the site being tracked.