手册:性能调优

This page is a translated version of the page Manual:Performance tuning and the translation is 100% complete.

本页概述了各种改进MediaWiki性能的方法。

语境

MediaWiki is capable of scaling to meet the needs of large wiki farms such as those of Wikimedia Foundation, WikiHow and FANDOM and can take advantage of a wide number of methods including multiple load-balanced database servers, memcached object caching, Varnish caches (see Manual:Varnish caching) and multiple application servers. For most smaller installations, this is overkill though, and simply enabling object caching and optimizing PHP performance should suffice.

快速开始

简短版本。我们推荐PHP使用字节码缓存,APCu作为本地对象缓存,Memcached作为主缓存;这就是维基媒体基金会对维基百科等使用的东西。

在某些情况下,太多层的过度缓存可能会降低性能。

使用Puppet快速开始

Most of the tweaks on this page have been collected in a puppet manifest (puppet/modules/role/manifests/simple_performant.pp and puppet/modules/role/manifests/simple_miser.pp). If you install Puppet, you can apply them to your server with a single command.

PHP

字节码缓存

See PHP configuration#Opcode caching

PHP works by compiling a PHP file into bytecode and then executing that bytecode. 编译像MediaWiki这样的大型应用程序的过程需要相当长的时间。 PHP加速器的工作原理是存储已编译的字节码并直接执行,减少了编译代码的时间。

OPcache is included in PHP 5.5.0 and later and the recommended accelerator for MediaWiki. 其他支持的操作代码缓存有:WinCache。

Opcode缓存存储了PHP脚本的编译输出,大大减少了多次运行一个脚本所需的时间。 MediaWiki不需要配置来做PHP字节码缓存,一旦安装并启用它们,就会 "直接工作"。

对象缓存

For more information about local server, main cache and other cache interfaces, see Manual:Caching.

本地服务器

这个接口用于直接在Web服务器上进行轻量级的缓存。 这个接口可以在Web请求中持久保存存储的值。

MediaWiki会自动检测是否存在支持的后端。 不需要配置MediaWiki。

For PHP 7+, you should install APCu or WinCache. (On PHP 5, APCu was known to be unstable in some cases.[1])

要安装 APCu,请使用:

sudo apt-get install php-apcu php-igbinary

A script, apc.php is bundled with the APCu package which can be used to inspect the status of the cache, and also examine the contents of the user cache to verify that MediaWiki is correctly using it.

主要缓存

这个接口被用作较大对象的主要对象缓存。

主缓存默认是禁用的,需要手动配置。 To enable it, set $wgMainCacheType to a key in $wgObjectCaches . 有为Memcached、APC和MySQL预先配置的接口。 You can configure additional backends via $wgObjectCaches (e.g. for Redis).

// Default:
// $wgMainCacheType = CACHE_NONE;

单个Web服务器

If you have APC installed is strongly recommended to use that by setting the following in LocalSettings.php :

$wgMainCacheType = CACHE_ACCEL;

一旦设置,用户会话存储和解析器输出缓存也将继承这个MainCacheType设置。

When using APC with limited RAM (and no Memcached or other object cache configured), then important objects might be evicted too often due to the size of parser output cache building up. Consider setting $wgParserCacheType to CACHE_DB, which will move those keys out to the database instead.

If using $wgMainCacheType = CACHE_ACCEL; and users are unable to login due to "session hijacking" errors, consider overriding $wgSessionCacheType to CACHE_DB. See task T147161 for more info.

If you can't use APC, consider installing Memcached (requires at least 80MB of RAM). While installing Memcached is considerably more complicated, it is very effective.

如果APC或Memcached二者都不选,您可以退回到将对象缓存存储在你的数据库中。 下面的预设将做到这一点:

$wgMainCacheType = CACHE_DB;

多个Web服务器

If your MediaWiki site is served by multiple web servers, you should use a central Memcached server. Detailed instructions are on the memcached page.

It is important that you do not use APC as the main cache for multiple web servers, as this cache is expected to be coordinated centrally for a single MediaWiki installation. Having each web server use APC as its own MainCache will cause stale values, corruption or other unexpected side-effects. Note that for values that are safe to store in uncoordinated fashion (the "local-server cache"), MediaWiki automatically makes use of APC regardless of this configuration setting.

跨维基缓存

MediaWiki interwiki prefixes are stored in the interwiki database table. See Interwiki cache for how to cache these in a CDB or PHP file.

本地化缓存

By default, interface message translations are cached in the l10n cache database table. Ensure $wgCacheDirectory in LocalSettings.php is set to a valid path to use a local caching instead. See 本地化 for more details.

页面访问缓存

Page view caching increases performance tremendously for anonymous (not logged-in) users. It does not affect performance for logged-in users.

缓存代理

A caching proxy (or "HTTP accelerator") stores a copy of web pages generated by your web server. When such page is requested a second time, then the proxy serves up its local copy, instead of passing the request onto the real web server.

This massively improves the response times for page loads by end users, and also tremendously reduces the computational load on the MediaWiki web server. When a page is edited, MediaWiki can automatically purge the local copy from the cache proxy.

缓存代理的例子。

  • Varnish Cache, this is currently (as of November 2018) used by Wikipedia. See also Manual:Varnish caching.
  • Squid,这是维基百科在2012年之前使用的。

See also Squid on Wikitech.

文件缓存

See 手册:文件缓存 for main article about this.

In absence of a caching proxy or HTTP accelerator, MediaWiki can optionally use the file system to store the output of rendered pages. For larger sites, using an external cache like Varnish is preferable to using the file cache.

Web服务器

  • if you use Apache as web server, use PHP-FPM, not mod_php. PHP-FPM optimizes re-use of PHP processes.
    • 切换Apache以使用事件MPM,而不是prefork MPM。
  • 调整robots.txt,不允许机器人抓取历史页面。 这减少了一般的服务器负载。
  • HTTP/2 protocol can help, even with ResourceLoader.[2]

配置设置

Large sites running MediaWiki 1.6 or later should set $wgJobRunRate to a low number, say 0.01. See 手册:作业队列 for more information.

Composer

This will provide no benefit if you enable opcache in PHP.

MediaWiki uses composer for organizing library dependencies. By default these are included from the /vendor directory using a dynamic autoloader. This autoloader needs to search directories which can be slow. It is recommended to generate a static autoloader with Composer, which will make your wiki respond faster.

Using a static autoloader is the default for all MediaWiki installations from the tarball download or from Git. If for some reason this is not the case, use the following to generate the static autoloader:

composer update -o --no-dev

请记住,这需要在每次MediaWiki更新后重新运行,因为它包括软件中存在哪些库和类的静态拷贝。

数据库配置

MySQL

For a heavy concurrent write load, InnoDB is essential. Use memcached, not the default MySQL-based object cache.

See below for some DB configuration tricks. You can also try and run the mysql-tuning-primer script to get some quick statistics and suggestions.

多个服务器

数据库软件和Web服务器软件将开始争夺位于单台服务器上的繁忙的MediaWiki安装上的RAM。 如果你的维基有一个稳定的流量,一旦其他性能优化完成(并且缓存提供大部分内容),一个合理的步骤是把数据库和Web服务器放在不同的服务器上(或者,在某些情况下,多个独立的服务器,从一个副本开始)。 此外:

基準測試

一些工具可以帮助快速评估性能调整的效果。

参见

参考资料

  1. APCu GitHub issue 19: Hung apaches on pthread wrlocks
  2. Niklas Laxström, Performance is a feature, December 9th, 2013.