Server rental store

PHP Optimization

# PHP Optimization for MediaWiki 1.40

This article details techniques for optimizing PHP performance on a MediaWiki 1.40 installation. Proper PHP configuration is crucial for a responsive and efficient wiki, especially under heavy load. This guide assumes a basic understanding of server administration and PHP.

Understanding PHP's Impact on MediaWiki

MediaWiki is a PHP-based application. The majority of its processing is done by the PHP interpreter. Therefore, optimizing PHP significantly impacts the wiki's overall performance. Key areas to focus on include memory allocation, opcode caching, and process management. Slow PHP execution leads to increased page load times, database bottlenecks, and a degraded user experience. Regularly monitoring Server Performance is recommended.

PHP Configuration File (php.ini)

The primary method for controlling PHP's behavior is through the `php.ini` file. The location of this file varies depending on your operating system and PHP installation method. Common locations include `/etc/php/7.4/cli/php.ini` (for command-line usage) and `/etc/php/7.4/apache2/php.ini` (for Apache web server integration). Always back up your `php.ini` file before making any changes. After modifications, restart your web server (e.g., Apache or Nginx) or PHP-FPM to apply the new settings. See PHP Documentation for more details.

Recommended php.ini Settings

The following settings are generally beneficial for MediaWiki performance. Values may need adjustments based on your server's resources.

Setting Recommended Value Description
`memory_limit` `256M` or higher Maximum amount of memory a script may consume. Increase if you encounter memory exhaustion errors.
`max_execution_time` `60` Maximum time a script is allowed to run, in seconds.
`upload_max_filesize` `100M` Maximum allowed size for uploaded files.
`post_max_size` `100M` Maximum size of POST data accepted.
`max_input_vars` `3000` Maximum number of input variables accepted.
`error_reporting` `E_ALL & ~E_NOTICE & ~E_DEPRECATED` Controls the level of error reporting.
`display_errors` `Off` (in production) Whether to display errors to the browser. Disable in production for security.

Opcode Caching

Opcode caching significantly improves PHP performance by storing compiled PHP code in memory. This avoids the need to recompile the code on every request. Opcode Cache is essential.

Popular Opcode Caches

Opcode Cache Description Installation (Example - Debian/Ubuntu)
OPcache (PHP 5.5+) Built-in opcode cache. Generally the easiest to configure. Typically enabled by default. Check `php.ini` for `opcache.enable=1`.
APCu (Alternative PHP Cache, user-space) A user-space opcode cache. Useful for shared hosting environments. `sudo apt-get install php-apcu`
Zend OPcache Another popular, high-performance opcode cache. Often bundled with PHP distributions.

To enable OPcache, ensure the following settings are present in your `php.ini`:

```ini [opcache] opcache.enable=1 opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4096 opcache.revalidate_freq=60 ```

Adjust the `opcache.memory_consumption` value based on your wiki's size and traffic.

Process Management with PHP-FPM

For high-traffic wikis, using PHP-FPM (FastCGI Process Manager) is highly recommended. PHP-FPM manages a pool of PHP processes, which improves performance and stability compared to traditional mod_php. See the PHP-FPM Configuration page for details.

PHP-FPM Pool Configuration

Configure PHP-FPM pools to dedicate resources to your MediaWiki installation. A sample pool configuration (`/etc/php/7.4/fpm/pool.d/mediawiki.conf`):

Parameter Value
`user` `www-data` The user that PHP-FPM processes will run as.
`group` `www-data` The group that PHP-FPM processes will run as.
`listen` `/run/php/php7.4-fpm-mediawiki.sock` The Unix socket that PHP-FPM will listen on.
`listen.owner` `www-data` The owner of the Unix socket.
`listen.group` `www-data` The group of the Unix socket.
`pm` `dynamic` Process manager type.
`pm.max_children` `5` Maximum number of child processes.
`pm.start_servers` `2` Number of processes started on startup.
`pm.min_spare_servers` `1` Minimum number of idle processes.
`pm.max_spare_servers` `3` Maximum number of idle processes.
`php_admin_value[error_log]` `/var/log/mediawiki-php-fpm.log` Error log file.

Adjust the `pm.max_children`, `pm.start_servers`, `pm.min_spare_servers`, and `pm.max_spare_servers` values based on your server's resources and traffic patterns. Monitor PHP-FPM Statistics to optimize these settings.

Additional Optimization Techniques

⚠️ *Note: All benchmark scores are approximate and may vary based on configuration. Server availability subject to stock.* ⚠️