Set up MediaWiki with PHP7.4, nginx and MySQL

The MediaWiki software is used by tens of thousands of websites and thousands of companies and organizations. It powers Wikipedia and also this website. MediaWiki helps you collect and organize knowledge and make it available to people. It’s powerful, multilingualfree and open, extensible, customizable, reliable, and free of charge. Find out more and if MediaWiki is right for you.

This article shows you how to set up MediaWiki on local network (accessed by IP address, without domain and SSL configuration).

Environment

  • Debian 10

Reference

Steps

Read the installation requirements before going through the following steps.

For those who doesn’t want to open another page, here’s the hardware requirements.

  • Minimal requirements
    • 256MB of RAM
    • 85MB of storage

All the following commands are executed as root.

Install the latest PHP

MediaWiki requires at least PHP 7.29.

  1. Add repository
    • Download GPG key, execute
      apt -y install lsb-release apt-transport-https ca-certificates 
      wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    • Add repo
      echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list
  2. Install PHP
    • Execute
      apt update 
      apt install php7.4 php7.4-fpm php-mysql php-xml php-mbstring
  3. Check whether the service is running
    systemctl status php7.4-fpm.service

Setup MySQL

Since the default MySQL that Debian apt installed has a little problem (see ERROR 1698 (28000): Access denied for user ‘root’@’localhost’ for detail, it needs some post-install configuration), and MediaWiki needs at least MySQL 5.5.8, so I chose to go with the latest version.

The following steps will install MySQL 8.0.21.

  1. Install mysql-apt-config
    • Get the dpkg package
      wget https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
    • Install via dpkg
      dpkg -i mysql-apt-config*
  2. Install MySQL server
    apt update
    apt install mysql-server

    During installation, the process will ask you to enter the password for root user, and confirm the MySQL version you are about to install.

  3. Check MySQL status
    systemctl status mysql.service
  4. Add MediaWiki database
    • Log in to MySQL
      mysql -u root -p

      then type in your password that set up before.

    • Do query
      CREATE DATABASE wikidb; 
      CREATE USER 'wikiuser'@'localhost' IDENTIFIED BY 'password';
      GRANT ALL PRIVILEGES ON wikidb.* TO 'wikiuser'@'localhost' WITH GRANT OPTION;
      exit;

      You can change the database name, the username and password by yourself.

Get MediaWiki

  1. Download release file
    # replace link if there's newer version 
    wget https://releases.wikimedia.org/mediawiki/1.34/mediawiki-1.34.2.tar.gz
  2. Decompression
    tar zxvf mediawiki-1.34.2.tar.gz
  3. If you downloaded and untared MediaWiki as root as I did, you need to change the ownership and the privilege of MediaWiki files
    chown -R www-data:www-data mediawiki/ 
    chmod -R 755 mediawiki

Nginx Configuration

  1. Install Nginx
    • Debian 10 has the perfect verison of nginx, just install it directly
      apt install nginx
  2. Add MediaWiki config file
    • Create MediaWiki config file under /etc/nginx/sites-available, then add the following contents
      server {
          listen 80;
          server_name [your domain];
          root [your mediawiki directory];
      
          index index.php;
      
          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;
          }
      
          location ~ \.php$ {
              include fastcgi_params;
              fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
              fastcgi_param SCRIPT_FILENAME $request_filename;
          }
      
          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/ {
              deny all;
          }
      
          location /dumps {
              root /var/www/mediawiki/local;
              autoindex on;
          }
      }
    • Make a soft link to /etc/sites-enabled
      ln -s /etc/nginx/sites-available/mediawiki /etc/nginx/sites-enabled/
  3. Start Nginx
    nginx -t # test config file
    nginx -s reload
  4. Check whether nginx is listening on ports
    netstat -plnt

Install MediaWiki

After what’s done above, you can access MediaWiki by opening [your machine’s IP]:[your nginx listening port] in browser, the page will tell you there’s no LocalSettings.php file. You need to finish the web install process.

  1. Follow the install process
    • Choose languages
    • If your database and MediaWiki is hosted on the same machine, use localhost, otherwise use your database machine’s IP. The database name should be the same as the name in your database query above, so does the username and password.
  2. In the end, the installation will generate a file named LocalSettings.php, download it and place it under your MediaWiki root directory.
  3. Don’t forget to change the ownership and the privilege if you use root account as I did.
    cd mediawiki
    chown -R www-data:www-data LocalSettings.php
    chmod 775 LocalSettings.php

Screenshots

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据