Manual:Performance tuning/zh

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

概要
MediaWiki 能够扩展以满足大型维基农场的需求，例如 Wikimedia Foundation、WikiHow 和 Fandom，并且可以利用多种方法，包括多个负载平衡数据库服务器， Memcached 对象缓存，Varnish 缓存（参见 手册:Varnish 缓存）和多个应用服务器. 对于大多数较小的安装，这有点矫枉过正，只需启用对象缓存和优化 PHP 性能就足够了.



快速开始
简而言之，我们推荐 PHP 使用字节码缓存，APCu 作为本地对象缓存，Memcached 作为主缓存；这就是维基媒体基金会用于维基百科等的配置.

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



使用 Puppet 快速开始
此页面上的大部分调整已收集在 Puppet 清单中（ 和 ）. 如果你安装 Puppet，你可以用一个命令将它们应用到你的服务器.

PHP


字节码缓存

 * 请参阅 PHP 配置#Opcode 缓存.

PHP 通过将 PHP 文件编译成字节码然后执行该字节码来工作. 编译大型应用程序（如 MediaWiki）的过程需要相当长的时间. PHP 加速器通过存储编译的字节码并直接执行它来工作，减少了编译代码所花费的时间.

OPcache 包含在 PHP 5.5.0 及更高版本中，是 MediaWiki 推荐的加速器. 其他支持的操作码缓存有：WinCache.

Opcode 缓存存储 PHP 脚本的编译输出，大大减少了多次运行脚本所需的时间. MediaWiki 不需要特别配置来执行 PHP 字节码缓存，一旦安装并启用它们就会“正常工作”.

从 MediaWiki 1.36 开始，在没有字节码缓存的系统上它可能会变慢. 参见.



对象缓存
有关本地服务器、主缓存和其他缓存接口的更多信息，请参阅手册:缓存.



本地服务器
此接口用于直接在 Web 服务器上进行轻量级缓存. 此接口可以跨 Web 请求持久保存存储的值.

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

对于 PHP 7+，您应该安装 APCu 或 WinCache. (在 PHP 5 上，已知 APCu 在某些情况下不稳定. )

要安装 APCu，请使用：

脚本  与 APCu 包捆绑在一起，可用于检查缓存的状态，并检查用户缓存的内容以验证 MediaWiki 是否正确使用它.



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

主缓存默认是禁用的，需要手动配置. To enable it, set to a key in. 有为Memcached、APC和MySQL预先配置的接口. 你可以通过 配置其他类型的后端 (例如 Redis).



单个Web服务器
If you have APC installed is strongly recommended to use that by setting the following in :

一旦设置，用户会话存储和解析器输出缓存也将继承这个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 to CACHE_DB, which will move those keys out to the database instead.

If using  and users are unable to login due to "session hijacking" errors, consider overriding  to. See task T147161 for more info.

If you can't use APC, consider installing (requires at least 80MB of RAM). 虽然安装 Memcached 要复杂得多，但它却非常奏效.

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



多个Web服务器
If your MediaWiki site is served by multiple web servers, you should use a central Memcached server. Detailed instructions are on the  page.

It is important that you do not use APC as your main cache if you have multiple web servers. This cache must 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  database table. See for how to enable caching.



本地化缓存
By default, interface message translations are cached in the database table. Ensure in  is set to a valid path to use a local caching instead. See Help:System message#Caching for more details.

Sidebar cache
A small save in DB queries can be obtained by caching the sidebar (disabled by default). See  and.



页面访问缓存
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年之前使用的.
 * Apache's mod_cache_disk, see this article for instructions with MediaWiki.



文件缓存

 * 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.



配置设置
Large sites running MediaWiki 1.6 or later should set to a low number, say 0.01. See for more information.

Composer
MediaWiki uses composer for organizing library dependencies. By default these are included from the  directory using a dynamic autoloader. 这个自动加载器需要搜索目录，这一过程可能会比较缓慢. 建议使用 Composer 生成静态的自动加载器，这将使您的 wiki 响应更快.

使用静态的自动加载器是所有从 tarball 下载或 Git 安装的 MediaWiki 的默认设置. 如果由于某种原因导致不是这种情况，请使用以下命令生成静态自动加载器：

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.

<span id="Multiple_servers">

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


 * check that MySQL has query cache enabled and enough memory;
 * give most memory to innodb_buffer_pool;
 * add cores for MySQL if maxed out at peak times;
 * give memcached even more RAM for in-memory cache.

基準測試
一些工具可以帮助快速评估性能调整的效果.


 * http://webpagetest.org is "real life" testing, commanded in your browser.
 * ab is a command line tool which quickly produces some nice stats.
 * PageSpeed

<span id="See_also">

参见

 * http://dammit.lt/2007/01/26/mediawiki-performance-tuning/ : APC and a few simple settings that boost performance
 * 更激进的牺牲部分功能的修改
 * How to make MediaWiki fast
 * Comprehensive MediaWiki performance optimisation (mostly redundant with this page and what above)
 * User:Ilmari Karonen/Performance tuning, focusing on small wikis
 * Use cases:
 * Wikipedia: Site internals, configuration, code examples and management issues [PDF, 2007]
 * Caching, minification, domain-sharing and compression techniques used by WikiFur
 * For developers:
 * Logging and
 * North's Performance chapter
 * Wikimedia specific documentation:
 * Backend performance guide of the Wikimedia Performance Team
 * Frontend performance guide of the Wikimedia Performance Team