There is a new version of this tutorial available for Ubuntu 20.04 (Focal Fossa).

How to Install MediaWiki with Nginx on Ubuntu 16.04

In this tutorial, I will show you step-by-step how to install MediaWiki with Nginx web server on Ubuntu 16.04. I will guide you on how to install and configure the LEMP stack for MediaWiki installation, including generating and configuring new free SSL Letsencrypt for MediaWiki security.

MediaWiki is one of the most popular wiki software that was originally developed for use on Wikipedia. It's a free and open source software written in the PHP programming language, and has become the most dominant software in the wiki category. Originally developed by Magnus Manske in 2002, the tool is now on version 1.30. MediaWiki has been used by many big companies/organizations including Nginx, Intel, Novell, and NASA.

What we will do:

  1. Install Nginx on Ubuntu 16.04
  2. Install and Configure PHP-FPM
  3. Install and Configure MySQL Database
  4. Download and Configure MediaWiki
  5. Generate new SSL Letsencrypt on Ubuntu 16.04
  6. Configure Nginx Virtual Host for MediaWiki Installation
  7. MediaWiki Web-Based Installation
  8. Configure Default Skin MediaWiki

Prerequisites

  • Ubuntu 16.04 server
  • Root privileges

Step 1 - Install Nginx on Ubuntu 16.04

The first step we must do for the MediaWiki installation is to install the web server. So in this section, we will install the Nginx web server, start the Nginx service, and then enable it to launch automatically at system boot.

Before installing the Web server, connect to your server, update all repositories, and then upgrade.

sudo apt update
sudo apt upgrade -y

Now install Nginx using the following apt command.

sudo apt install nginx -y

After that, start nginx and enable it to launch at system boot.

systemctl start nginx
systemctl enable nginx

Now check the HTTP port using netstat and make sure you see port 80 being used by Nginx.

netstat -plntu

Check that nginx is running

The Nginx web server has been installed on Ubuntu 16.04.

Step 2 - Install and Configure PHP-FPM

Now we will install the PHP-FPM on Ubuntu 16.04. And we will be using PHP 7.0 for MediaWiki installation.

Install PHP and PHP-FPM (along with all required extensions) using the apt command below.

sudo apt install imagemagick php7.0-fpm php7.0-intl php7.0-xml php7.0-curl php7.0-gd php7.0-mbstring php7.0-mysql php-apcu

And after the installation is complete, we need to change some default configuration for PHP-FPM. We need to increase the upload max file size as well as the memory limit configurations.

Go to the PHP configuration directory and edit the php-fpm 'php.ini' file using vim.

cd /etc/php/7.0
vim fpm/php.ini

Increase the 'upload_max_filesize' configuration to '20M', and increase the 'memory_limit' to '128M'.

upload_max_filesize = 20M
memory_limit = 128M

Save and exit.

Now restart the PHP-FPM service and enable it to automatically launch at boot time using the following systemctl commands.

systemctl restart php7.0-fpm
systemctl enable php7.0-fpm

PHP-FPM now is running on Ubuntu 16.04, and it's running under the sock file. Check it using the netstat command below.

netstat -pl | grep php

And you will get the PHP-FPM sock file as shown below.

Configure PHP-FPM socket

Step 3 - Install and Configure MySQL Database

MediaWiki stores all data and content in the database, and it's compatible with a variety of database servers. For this guide, we will be using the MySQL database for MediaWiki installation.

In this step, we will install the MySQL database server and then create a new database and user for MediaWiki.

Install MySQL database on Ubuntu 16.04 using the following apt command.

sudo apt install mysql-server mysql-client -y

You will be asked for the MySQL 'root' password - type your own password and press 'Enter'.

Set MySQL root password

Repeat your MySQL 'root' password.

Retype root password

MySQL installation is complete. Now start MySQL and enable it to launch at boot time using the systemctl commands below.

systemctl start mysql
systemctl enable mysql

Next, we will create a new database and user for MediaWiki. We will create a new database named 'mediawikidb' with the user 'mediawiki' and password 'mypassword'.

Connect to the mysql server using the mysql command below.

mysql -u root -p

Now create the database and user with the following MySQL queries.

create database mediawikidb;
grant all privileges on mediawikidb.* to mediawiki@'localhost' identified by 'mypassword';
flush privileges;
exit;

MySQL server installed on Ubuntu 16.04, and the database for MediaWiki installation has been created.

Create MediaWiki database

Step 4 - Download and Configure MediaWiki

For this guide, we will be using the latest MediaWiki version 1.30. And before we download MediaWiki, we need to install some packages on the server.

Run the apt command below to install new packages.

sudo apt install composer git zip unzip -y

Now create new 'mediawiki' directory and clone mediawiki source code to that directory.

mkdir -p /var/www/mediawiki
git clone https://gerrit.wikimedia.org/r/p/mediawiki/core.git /var/www/mediawiki

Download MediaWiki

Next, go to the '/var/www/mediawiki' directory and install some PHP dependencies needed for the MediaWiki installation using the composer command.

cd /var/www/mediawiki
composer install --no-dev

Run composer

After all PHP dependencies installation is complete, change the owner of the mediawiki directory to 'www-data' user and group.

chown -R www-data:www-data /var/www/mediawiki

The latest version of MediaWiki 1.30 is downloaded in the '/var/www/mediawiki' directory.

Step 5 - Generate SSL Letsencrypt on Ubuntu 16.04

For security reasons, we will run MediaWiki under the HTTPS connection. All HTTP connections will be automatically redirect to HTTPS. And for this purpose, we need SSL certificates. We will be using free SSL from Letsencrypt.

In order to generate new SSL certificates from Letsencrypt, we need to install letsencrypt command line on to the server. Use the following command to do this:

sudo apt install letsencrypt -y

After the installation is complete, stop the Nginx web server.

systemctl stop nginx

Next, generate new SSL certificates using the letsencrypt command below.

letsencrypt certonly

You will be asked for your email address - it's used for certificate renew notification.

So, type your email address and choose 'OK'.

Generate Let's encrypt certificate

For the Letsencrypt TOS (Terms Of Service), choose 'Agree' and press Enter.

Lets encyrpt terms of service

And now type your wiki domain name, mine is 'wiki.hakase-labs.co'.

Enter domain name

And when it's all done, you will get the result as shown below.

SSL Certificate created

SSL certificates for MediaWiki installation has been generated in the '/etc/letsencrypt/live' directory.

Step 6 - Configure MediaWiki Nginx Virtual Host

The LEMP stack (Linux, Nginx, MySQL, and PHP-FPM) for MediaWiki installation has been set up, and the MediaWiki source code has been downloaded in the '/var/www/mediawiki' directory.

In this step, we will create a new Nginx virtual host file 'mediawiki', and then activate the virtual host.

Go the Nginx configuration directory and create a new virtual host file 'mediawiki' using vim.

cd /etc/nginx/
vim sites-available/mediawiki

Paste the following Nginx virtual host for MediaWiki configuration there.

# HTTP Request will be Redirected to the HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name wiki.hakase-labs.co;
    return 301 https://$host$request_uri;
}

# HTTPS Configuration
server {

    listen 443 ssl;
    listen [::]:443;
   
    server_name wiki.hakase-labs.co;
    root /var/www/mediawiki;

    index index.php;
    autoindex off;

    # SSL Certificate Configuration
    ssl_certificate /etc/letsencrypt/live/wiki.hakase-labs.co/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/wiki.hakase-labs.co/privkey.pem;

    client_max_body_size 5m;
    client_body_timeout 60;

    location / {
        try_files $uri $uri/ @rewrite;
    }

    location @rewrite {
        rewrite ^/(.*)$ /index.php?title=$1&$args;
    }

    location ^~ /maintenance/ {
        return 403;
    }

    # PHP Configuration
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        include snippets/fastcgi-php.conf;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        try_files $uri /index.php;
        expires max;
        log_not_found off;
    }

    location = /_.gif {
        expires max;
        empty_gif;
    }

    location ^~ ^/(cache|includes|maintenance|languages|serialized|tests|images/deleted)/ {
        deny all;
    }

    location ^~ ^/(bin|docs|extensions|includes|maintenance|mw-config|resources|serialized|tests)/ {
        internal;
    }

    # Security for 'image' directory
    location ~* ^/images/.*.(html|htm|shtml|php)$ {
        types { }
        default_type text/plain;
    }

    # Security for 'image' directory
    location ^~ /images/ {
        try_files $uri /index.php;
    }

}

Change as per your requirement/case, and then save and exit.

Next, activate a new mediawiki virtual host using the following command.

ln -s /etc/nginx/sites-available/mediawiki /etc/nginx/sites-enabled/

Test nginx configuration and make sure there is no error. Then restart the service.

nginx -t
systemctl restart nginx

A new HTTPS port 443 is opened on your server - check it using the netstat command in the following way.

netstat -plntu

Shown below is the result.

Configure Nginx

Step 7 - MediaWiki Web-Based Installation

Now open your web browser and type your domain name into the address bar.

http://wiki.hakase-labs.co

And you will be redirected to the HTTPS connection.

MediaWiki installer

Click the 'set up the wiki' link on the page to configure MediaWiki.

- Language Configuration

Choose your own language, we will choose 'English' and click 'Continue'.

Language Configuration

- Environment Checking

And now, MediaWiki will perform environment checking. Make sure you get the result as shown below.

Check installation environment

Click 'Continue' for installation.

- Database Configuration

Now for the Database configuration. In the 'Database name' field, type 'mediawikidb' with prefix 'wiki_'. Next, type the database user 'mediawiki' and password 'mypassword'.

Database configuration

And click 'Continue'.

- Database Settings

As for the Database settings, just leave it default and click 'Continue'.

Database settings

- Create an Admin User

Now we need to create the administrator account. Type your own user, password, and email as shown below.

Create admin user

And click 'Continue'.

- MediaWiki Additional Configuration

And you should get to the page about 'MediaWiki Additional Configuration'.

Additional configuration

Leave it with default values, and click 'Continue'.

- MediaWiki Installation

Now click 'Continue' again to install MediaWiki.

MediaWiki Installation

And you will get to the page similar to the one shown below.

Installation continues

Click 'Continue' again.

Now you will see the page says installation is complete. You will be prompted for downloading new file 'LocalSettings.php' - download the file.

Locale settings

And upload the 'LocalSetting.php' file to the server mediawiki directory '/var/www/mediawiki'. Also, do not forget to change the owner of the file to 'www-data' user and group.

scp LocalSettings.php [email protected]:/var/www/mediawiki/
chown -R www-data:www-data /var/www/mediawiki

Now, come back to your web browser MediaWiki installation page, and click the link 'enter your wiki'. You will be told that MediaWiki has been installed with Nginx web server on Ubuntu 16.04 server.

MediaWiki installed

Step 8 - Download and Configure Default Skin

At this stage, mediawiki installation has been completed. And in this step, we will configure the default skin/theme.

Go to the '/var/www/mediawiki/skins' directory and clone the default skin 'Vector'.

cd /var/www/mediawiki/skins/
sudo git clone https://gerrit.wikimedia.org/r/mediawiki/skins/Vector

Download MediaWiki skin

Now edit the 'LocalSettings.php' file using the vim editor.

vim /var/www/mediawiki/LocalSettings.php

And paste the following php code towards the end of file.

wfLoadSkin( 'Vector' );

That's it. Save and exit.

Come back to your web browser and refresh the MediaWiki page. Make sure you get the MediaWiki with 'Vector' skin as shown below.

Vector skin

MediaWiki installation and configuration with Nginx web server on Ubuntu 16.04 has been completed successfully.

Reference

Share this page:

4 Comment(s)