MediaWiki Performance Tweaks

From wiki.mikejung.biz
Jump to: navigation, search

Liquidweb 728x90.jpg

Overview

This page contains the configuration I use to run this wiki. I'm using MediaWiki 1.24 with mod_pagespeed, Memcached, PHP-FPM (zend OPcache on), Apache 2.4 with the Event MPM, and MySQL 5.6. I can't say that these are the best settings to use with MediaWiki, or that they offer the best performance, but I will say that these settings work and do make MediaWiki relatively fast. Recently I've removed Varnish from the equation to see if it was better to stick with just Apache and let mod_pagespeed do it's thing. Turns out this is a much better setup than using Varnish if you configure mod_pagespeed to use Memcached and raise the LRU cache sizes.

I've also disabled MediaWiki's File Cache so I'm letting mod_pagespeed and Apache handle just about everything. I found that my site loaded slightly faster than before and the First Byte response time remained about the same as before. It does take pagespeed a little while to warm up all the caches, so give your site about 2 or 3 hours to warm up (depending on how busy it is).

LocalSettings.php Performance Tweaks for Apache with mod_pagespeed

I'm currently using these settings in my LocalSettings.php configuration file. These settings will disable the pageview counter, which reduces database hits. I'm also configuring all the caches to use Memcached. Pretty simple stuff, especially if you don't have Varnish involved. I chose not to use MediaWiki's File cache since mod_pagespeed likes to optimize pages based on user agent, caching pages doesn't really help make that happen, so I left it out of the configuration.

$wgDisableCounters = true;
$wgMainCacheType = CACHE_MEMCACHED;
$wgMemCachedServers = array("127.0.0.1:11211");
$wgSessionsInObjectCache = true;
$wgSessionCacheType = CACHE_MEMCACHED;
$wgSessionsInMemcached = true;
$wgShowIPinHeader = false;
$wgParserCacheType = CACHE_MEMCACHED;
$wgMessageCacheType = CACHE_MEMCACHED;
$wgUseLocalMessageCache = true;
$wgUseGzip = true;

LocalSettings.php Performance Tweaks (If using Varnish and File Cache)

I've been using these optimizations / tweaks for a while now and so far I have been happy with performance. I am using Memcached and Varnish, so I've enabled CACHE_MEMCACHED whenever possible. I am not 100% if using the file cache with Varnish is necessary, I will need to do some more testing when I get the chance. In general most requests get handled in under 150ms and webpagetest.org usually shows a First Byte Response time of under 300ms most of the time.

$wgDisableCounters = true;
$wgEnableSidebarCache = true;
$wgMainCacheType = CACHE_MEMCACHED;
$wgMemCachedServers = array("127.0.0.1:11211");
$wgSessionsInObjectCache = true;
$wgSessionCacheType = CACHE_MEMCACHED;
$wgUseSquid = true;
$wgSquidServers = array( '127.0.0.1', '$IP_of_httpd' );
$wgUsePrivateIPs = true;
$wgUseFileCache = true;
$wgFileCacheDirectory = "$IP/cache";
$wgShowIPinHeader = false;
$wgParserCacheType = CACHE_MEMCACHED;
$wgMessageCacheType = CACHE_MEMCACHED;
$wgUseLocalMessageCache = true;

Rebuilding MediaWiki's File Cache

If you choose to use File Cache with Varnish, you might need to delete cache/ from time to time and rebuild the file cache after you make changes. I've noticed that if I make edits to the sidebar, log out, or use another browser the changes do not show up right away. If I do the following, the updates take effect right away:

cd $Media_Wiki_dir

##BE CAREFUL WITH THIS COMMAND!##

rm -rf cache/

php maintenance/rebuildFileCache.php

chown -R $user:$group cache/

service memcached restart; service varnish restart; service php-fpm restart

Not exactly the most elegant solution, but you could just setup a cron to run once a day or whenever seems reasonable to clear the cache, rebuild it, and restart services to make sure the cache is fresh.

Varnish Configuration

I'm running CentOS 6.6 with Varnish 4.0, this is what I am using in /etc/sysconfig/varnish haven't had any Varnish issues with this configuration. May not be the best configuration out there, but it does what I need it to do. I'm using a 1GB LiquidWeb SSD VPS to run my wiki on.

vim /etc/sysconfig/varnish

VARNISH_MIN_THREADS=100
VARNISH_MAX_THREADS=5000
VARNISH_THREAD_TIMEOUT=240
VARNISH_STORAGE_SIZE=256M
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"
VARNISH_TTL=240


I'm not using anything special in terms of VCL in /etc/varnish/default.vcl. I'm sure there is much better VCL out there for Mediawiki but all the default.vcl examples I've seen always cause varnish to fail to start, at least on my server so I just make sure Varnish passes the IP along to apache and call it good.


vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    # 
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.

if (req.restarts == 0) {
    if (req.http.X-Forwarded-For) {
        set req.http.X-Forwarded-For = req.http.X-Forwarded-For;
    } else {
        set req.http.X-Forwarded-For = client.ip;
    }
}
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    # 
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    # 
    # You can do accounting or modifying the final object here.
}