This page explains how to install MediaWiki on macOS. MediaWiki offers a robust platform that’s perfect for users seeking a personal knowledge management system. The installation process will cover setting up MariaDB, PHP, Apache 2, and finally MediaWiki itself.

Installation

Install MariaDB

Install MariaDB as a service:

# install mariadb as a service
brew install mariadb
brew services start mariadb

Connect to restore the database:

sudo mysql -u root

At the MariaDB prompt:

# alter mariadb root password
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MySecretPassword';
FLUSH PRIVILEGES;
EXIT

Run brew services to verify the service started:

Name    Status  User File       
mysql   started jano ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Install PHP 7.4

Install PHP as a service:

# Install PHP 7.4.3 required by Mediawiki but not supported by homebrew
brew tap shivammathur/php
brew install shivammathur/php/php@7.4
brew link php@7.4

# start the FastCGI daemon for PHP
brew services start php@7.4

Run brew services to verify the service started:

Name    Status  User File       
httpd   started jano ~/Library/LaunchAgents/homebrew.mxcl.httpd.plist
php@7.4 started jano ~/Library/LaunchAgents/homebrew.mxcl.php@7.4.plist
unbound none

For additional configuration, see the php.ini file:

% php --ini

Configuration File (php.ini) Path: /opt/homebrew/etc/php/7.4
Loaded Configuration File:         /opt/homebrew/etc/php/7.4/php.ini
Scan for additional .ini files in: /opt/homebrew/etc/php/7.4/conf.d
Additional .ini files parsed:      /opt/homebrew/etc/php/7.4/conf.d/ext-opcache.ini

Install Apache 2

Install Apache 2 as a service:

brew install apache2
brew services start httpd

Run brew services to verify the service started:

Name    Status  User File       
httpd   started jano ~/Library/LaunchAgents/homebrew.mxcl.httpd.plist
unbound none

For additional configuration, see the conf files at:

/opt/homebrew/etc/httpd/httpd.conf
/opt/homebrew/etc/httpd/extra/httpd-ssl.conf

Install MediaWiki

Download latest version and unzip at /Users/jano/MediaWiki/notes.

cd ~
mkdir MediaWiki
cd MediaWiki
curl -L -o mediawiki-1.40.0.zip https://releases.wikimedia.org/mediawiki/1.40/mediawiki-1.40.0.zip
unzip mediawiki-1.40.0.zip .
mv mediawiki-1.40.0 notes

Install dependencies

brew install imagemagick

Or, if you want to restore a previous version, see Restore backup.

Configure Apache 2

Edit /opt/homebrew/etc/httpd/httpd.conf:

ServerRoot "/opt/homebrew/opt/httpd"
Listen 80

LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so
ServerAdmin jano@jano.dev
ServerName jano.notes:80
DocumentRoot "/Users/jano/MediaWiki/notes"

<Directory "/Users/jano/MediaWiki/notes">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

# replace existing IfModule dir_module 
<IfModule php_module> 
  <FilesMatch .php$>
    SetHandler application/x-httpd-php
  </FilesMatch>
  <IfModule dir_module>
    DirectoryIndex index.html index.php
  </IfModule>
</IfModule>
<IfModule !php_module>
    <IfModule dir_module>
        DirectoryIndex index.html
    </IfModule>
</IfModule>

Create a file /Users/jano/Developer/mediawiki/notes/index.php with the content below and restart Apache 2.

<?php phpinfo(); ?>

Domain indirection

We’ll use the made-up domain jano.notes to serve the website locally. If we need to relocate to another domain, looking for instances of our made-up domain will facilitate the process.

Add the line below to /etc/hosts. You will need sudo.

127.0.0.1 jano.notes

Set these values in /Users/jano/Developer/mediawiki/notes/LocalSettings.php:

$wgServer = "http://jano.notes";
$wgDBserver = "jano.notes";

Set this value in /opt/homebrew/etc/httpd/httpd.conf:

ServerName jano.notes:80

Speed up MediaWiki

File cache

The file cache stores parsed pages, internationalization, and user sessions. This will greatly improve reading unchanged information. Note that if you edit one section of a big page, the other sections remain unchanged, so the edited page will appear faster than without caching. If your MediaWiki shows “Parser profiling data” at the bottom of the page, take a look before and after.

File caching greatly decreases CPU usage using a hosted MediaWiki with a limited CPU quota. The recommended minimum requirement for MediaWiki is 256MB of RAM for a single-computer website and 85MB of storage. Keep this in mind when choosing a hosted installation since increasing your memory may be more effective than increasing your CPU.

The default value of wgMainCacheType is none. Installing Memcached for a personal website is not worth it, and Opcache is not supported, so I will install APCu (APC User Cache).

Because MediaWiki requires PHP 7.4.33 and that version is not maintained in homebrew, I had to install a tap from shivammathur/homebrew-php. This tap lacks the pcre2 header, so I had to install it and add it to the PHP include folder. Be careful here; paths may be different in your machine.

brew install pcre2
find $(brew --prefix)/Cellar/pcre2 -name "pcre2.h"
ln -s /opt/homebrew/Cellar/pcre2/10.42/include/pcre2.h /opt/homebrew/Cellar/php@7.4/7.4.33_4/include/php/ext/pcre/pcre2.h

At this point, I could build APCu using PECL:

pecl install apcu

Configure LocalSettings.php:

# APCu
$wgMainCacheType = CACHE_ACCEL;
$wgSessionCacheType = CACHE_ACCEL;
$wgMessageCacheType = CACHE_ACCEL;
Now load phpinfo.php and see if it contains an apcu extension. The default configuration can be changed in php.ini.

PHP opcode cache

OPCache is a cache for compiled PHP code. It ships with a default configuration of 128 MB, which I found was enough for a single user. I explain here how to configure it, but the default settings are fine.

Check OPcache is enabled.

% php -v | grep OPcache
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

Check the location of the php.ini file:

% php --ini
Configuration File (php.ini) Path: /opt/homebrew/etc/php/7.4
Loaded Configuration File:         /opt/homebrew/etc/php/7.4/php.ini
Scan for additional .ini files in: /opt/homebrew/etc/php/7.4/conf.d
Additional .ini files parsed:      /opt/homebrew/etc/php/7.4/conf.d/ext-opcache.ini

Edit ext-opcache.ini and add the configuration below. Do not add it in php.ini, or it will be ignored.

[opcache]
; load opcache
zend_extension="/opt/homebrew/opt/php@7.4/lib/php/20190902/opcache.so"

; enable opcache
opcache.enable=1

; 64Mb for compiled PHP code
opcache.memory_consumption=64

; 8Mb for interned strings
opcache.interned_strings_buffer=8

; cache up to 2000 pages
opcache.max_accelerated_files=2000

; check changes on php files every 60 seconds
opcache.revalidate_freq=60

; fast shutdown sequence
opcache.fast_shutdown=1

; enable opcache for PHP scrupts run from the command line
opcache.enable_cli=1

Restart Apache

brew services restart httpd

To check the status of OPCache add this opcache.php page to the HTTPD server and load it.