↓ Skip to main content

Installing MediaWiki on Alpine Linux

·1355 words·7 mins

Overview
#

This guide covers installing MediaWiki on an Alpine Linux container running in Proxmox. The stack is Apache with php-fpm, MariaDB, APCu object caching, and PHP OPcache.

Steps at a glance:

  1. Prepare the container
  2. Install Apache
  3. Install PHP
  4. Install and configure MariaDB
  5. Download MediaWiki
  6. Switch Apache to php-fpm
  7. Install the APCu extension
  8. Secure the upload directory
  9. Run the MediaWiki installer
  10. Enable PHP OPcache

Replace these placeholders throughout:

PlaceholderMeaning
usernameYour Linux admin username
username_wikiThe MariaDB user for the wiki
passwordThe MariaDB wiki user’s password
IP_ADDRESSThe container’s IP address

1. Prepare the Container
#

Turn on the firewall
#

In the Proxmox web interface, assign the container to the appropriate security groups and turn on its firewall.

Create an admin user with doas
#

doas is a lightweight alternative to sudo. As root:

  1. Install doas:

    apk add doas
  2. Create your user (enter a password when prompted) and add it to the wheel group:

    adduser username
    adduser username wheel
  3. Open the doas config:

    vi /etc/doas.d/doas.conf
  4. Add this line at the end so wheel members can use root privileges through doas, then save and close:

    permit persist :wheel
  5. Switch to the new account:

    su - username
  6. Test doas. Enter your password when prompted and confirm that vi opens:

    doas vi test.txt
  7. Type exit twice to leave the new account and then the root session. Confirm you can no longer log in as root, then log in as your new user.

Install updates
#

doas apk update
doas apk upgrade

Lock the root account
#

doas passwd -l root

Set the timezone
#

Install the timezone database, point /etc/localtime at the Central zone (creating the symlink), and write the zone name to /etc/timezone as a plain-text record:

doas apk add tzdata
doas ln -sf /usr/share/zoneinfo/CST6CDT /etc/localtime
echo "CST6CDT" | doas tee /etc/timezone

Use tee for the last command. With doas echo "CST6CDT" > /etc/timezone, the > redirect still runs as your user and fails with “Permission denied.”

Install the OpenRC init system
#

doas apk add openrc

2. Install Apache
#

  1. Install Apache, start it, and enable it at boot:

    doas apk add apache2
    doas rc-service apache2 start
    doas rc-update add apache2
  2. Browse to http://IP_ADDRESS and confirm you see the “It works” page.


3. Install PHP
#

  1. Install PHP, its extensions, curl, ImageMagick, and diffutils:

    doas apk add curl php php-mysqli php-dom php-apache2 php-mbstring php-fpm php-ctype php-json php-curl php-fileinfo php-iconv php-intl php-session php-gd php-xml php-xmlreader php-xmlwriter imagemagick diffutils

    Optional: Older guides also set a PHP version variable as shown below. It isn’t needed for this install.

    export phpverx=$(alpinever=$(cat /etc/alpine-release|cut -d '.' -f1);[ $alpinever -ge 9 ] && echo 7|| echo 5)
  2. Restart Apache:

    doas rc-service apache2 restart
  3. Create a test page in the web root:

    cd /var/www/localhost/htdocs/
    doas vi phpinfo.php

    Press i to insert, then paste:

    <?php echo phpinfo(); ?>
    
  4. Browse to http://IP_ADDRESS/phpinfo.php. A PHP configuration page means Apache and PHP are working together.

  5. Delete the test file:

    doas rm phpinfo.php

4. Install and Configure MariaDB
#

  1. Install MariaDB and run its initial setup:

    doas apk add mysql mysql-client
    doas /etc/init.d/mariadb setup
  2. Start it and enable it at boot:

    doas rc-service mariadb start
    doas rc-update add mariadb default
  3. Secure the installation:

    doas mysql_secure_installation

    When prompted:

    • Switch to unix_socket authentication
    • Change the MariaDB root password
    • Remove anonymous user accounts
    • Disable remote root logins
    • Remove test databases
  4. Connect as root (enter the root password when prompted):

    mariadb -u root -p
  5. Create the wiki database and user, then grant access:

    CREATE DATABASE my_wiki;
    CREATE USER 'username_wiki'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON my_wiki.* TO 'username_wiki'@'localhost' WITH GRANT OPTION;
  6. Type exit to leave MariaDB.


5. Download MediaWiki
#

  1. Check the MediaWiki download page for the current release, and set these two variables to match it. The values below are an example for the 1.43 long-term support (LTS) branch:

    MW_BRANCH=1.43
    MW_VERSION=1.43.6

    MediaWiki 1.45 and later require PHP 8.2 or newer. The PHP 8.3 packages used in this guide meet that requirement.

  2. Create the webapps directory and download MediaWiki from your home directory:

    doas mkdir -p /usr/share/webapps/
    cd /home/username/
    doas curl https://releases.wikimedia.org/mediawiki/${MW_BRANCH}/mediawiki-${MW_VERSION}.tar.gz >mw.tar.gz
  3. Move the archive into place, unpack it, rename the folder, and delete the tarball:

    doas mv mw.tar.gz /usr/share/webapps/
    cd /usr/share/webapps/
    doas tar zxvf mw.tar.gz
    doas mv mediawiki-${MW_VERSION} mediawiki
    doas rm mw.tar.gz
  4. Give the apache user ownership and link MediaWiki into the web root:

    doas chown -R apache /usr/share/webapps/
    doas ln -s /usr/share/webapps/mediawiki/ /var/www/localhost/htdocs/mediawiki
  5. Restart Apache:

    doas rc-service apache2 restart

6. Switch Apache to php-fpm
#

Based mostly on the Alpine Linux wiki article Apache with php-fpm.

  1. Install the Apache proxy module and php-fpm, then start php-fpm and enable it at boot:

    doas apk add apache2-proxy php-fpm
    doas rc-service php-fpm83 start
    doas rc-update add php-fpm83
  2. Edit the main Apache config:

    doas vi /etc/apache2/httpd.conf
    • Enable the event MPM by removing the leading #:

      LoadModule mpm_event_module modules/mod_mpm_event.so
    • Make sure the prefork MPM is commented out:

      #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
    • Add this to the bottom of the file, then save and close:

      <FilesMatch \.php$>
          SetHandler "proxy:fcgi://127.0.0.1:9000"
      </FilesMatch>
  3. Disable mod_php. Open its config:

    doas vi /etc/apache2/conf.d/php83-module.conf

    Comment out these lines, then save and close:

    #LoadModule php_module modules/mod_php8.so
    
    #DirectoryIndex index.php index.html
    
    #<FilesMatch \.php$>
    #    SetHandler application/x-httpd-php
    #</FilesMatch>
  4. Set php-fpm to run as apache. Open the pool config:

    doas vi /etc/php83/php-fpm.d/www.conf

    Set these values, then save and close:

    user = apache
    group = apache
  5. Restart both services:

    doas rc-service php-fpm83 restart && doas rc-service apache2 restart

7. Install the APCu Extension
#

Adapted from “How to Install PHP APCu on Ubuntu 16.04”.

  1. Check whether APCu is already installed. If this returns nothing, continue:

    php -m | grep -i apcu
  2. Install the packages pecl needs:

    doas apk --no-cache add php-pear php83-dev gcc musl-dev make
  3. Install APCu. Answer no when asked about debugging:

    doas pecl install apcu
  4. Open the PHP config:

    doas vi /etc/php83/php.ini

    Add this line to the bottom of the file:

    extension=apcu.so
  5. Verify APCu is enabled, then restart both services:

    php -m | grep -i apc
    doas rc-service php-fpm83 restart
    doas rc-service apache2 restart

8. Secure the Upload Directory
#

Stop scripts from running in MediaWiki’s images upload directory.

  1. Open the Apache config:

    doas vi /etc/apache2/httpd.conf
  2. The Header directive in the block below needs Apache’s headers module. Find this line and make sure it’s uncommented (no leading #):

    LoadModule headers_module modules/mod_headers.so

    Without this module, Apache will fail to start with an “Invalid command ‘Header’” error.

  3. Add this block anywhere in the file:

    <Directory "/var/www/localhost/htdocs/mediawiki/images">
       # Ignore .htaccess files
       AllowOverride None
    
       # Serve HTML as plaintext, don't execute SHTML
       AddType text/plain .html .htm .shtml .phtml .php .php3 .php4 .php5 .php7
    
       # Old way of registering php with AddHandler
       RemoveHandler .php
    
       # Recent way of registering php with SetHandler
       <FilesMatch "\.ph(p[3457]?s?|tml)$">
          SetHandler None
       </FilesMatch>
    
       # Tell browsers to not sniff files
       Header set X-Content-Type-Options nosniff
    </Directory>
  4. Save and close, then restart Apache:

    doas rc-service apache2 restart

9. Run the MediaWiki Installer
#

  1. Browse to:

    http://IP_ADDRESS/mediawiki/mw-config/index.php
  2. Fill out the installation form. Make sure to:

    • Set the wiki type to Private Wiki
    • Select VisualEditor
    • Under Settings for Object Caching, select PHP object caching (APC, APCu or WinCache)
  3. On the final page, the installer downloads LocalSettings.php. Open it in a text editor on your local machine and copy its contents.

  4. On the container, create the file in the MediaWiki directory:

    cd /usr/share/webapps/mediawiki/
    doas vi LocalSettings.php
  5. Press i to insert, paste the contents, and check that everything pasted correctly from top to bottom.

  6. Confirm $wgMainCacheType is set to CACHE_ACCEL, then save and close:

    $wgMainCacheType = CACHE_ACCEL;
  7. Restart both services:

    doas rc-service php-fpm83 restart
    doas rc-service apache2 restart

10. Enable PHP OPcache
#

  1. Install the OPcache extension:

    doas apk add php-opcache
  2. Open the PHP config:

    doas vi /etc/php83/php.ini

    Find and uncomment these lines, then save and close:

    opcache.enable=1
    opcache.memory_consumption=128
    opcache.max_accelerated_files=10000
    opcache.revalidate_freq=200
  3. Restart Apache:

    doas rc-service apache2 restart

Accessing MediaWiki
#

Once setup is complete, MediaWiki is available at:

  • http://IP_ADDRESS/mediawiki
  • http://wiki.home.lan (local DNS name)

Troubleshooting
#

A directory listing appears instead of the wiki
#

If http://IP_ADDRESS/mediawiki shows a mediawiki/ directory listing:

  1. Open the Apache config:

    doas vi /etc/apache2/httpd.conf
  2. Find this block and change index.html to index.php, then save and close:

    <IfModule dir_module>
        DirectoryIndex index.php
    </IfModule>
  3. Restart Apache:

    doas rc-service apache2 restart