OPcache: Difference between revisions

From MoodleDocs
(Added 600px)
m (→‎Configuration: Changed <code> to <syntaxhighlight> as line wrapping was wrong following wiki upgrade on 2021-07-14)
 
(8 intermediate revisions by 5 users not shown)
Line 3: Line 3:
The standard OPcache extension is strongly recommended; since Moodle 2.6, it is the only solution officially supported by PHP developers. The benefits are increased performance and significantly lower memory usage. However, opcode caching extensions (including OPcache, eAccelerator and APC) aren't compatible with servers configured to use some common types of high-security PHP handlers such as suPHP (the default on WHM / cPanel Linux servers).
The standard OPcache extension is strongly recommended; since Moodle 2.6, it is the only solution officially supported by PHP developers. The benefits are increased performance and significantly lower memory usage. However, opcode caching extensions (including OPcache, eAccelerator and APC) aren't compatible with servers configured to use some common types of high-security PHP handlers such as suPHP (the default on WHM / cPanel Linux servers).


[[File:Opcache_error.png|600px]]
[[File:Opcache_error.png|800px]]


==Installation==
==Installation==


The OPcache extension is distributed as part of PHP 5.5.0 and later. It is available also for older stable PHP releases from PECL under the original name ZendOPcache.
The OPcache extension is distributed as part of PHP 5.5 and later. It is available also for older stable PHP releases from PECL under the original name ZendOPcache. To check if the extension is loaded and enabled look at [[PHP#Displaying_phpinfo_in_Moodle|the PHP info page]] under the '''Zend OPcache''' heading.


'''NOTE: If you are running PHP 5.3 or 5.4 you can safely ignore the Environment Check message about OpCache. Nonetheless, [https://moodle.org/mod/forum/discuss.php?d=245885 it might be useful] to upgrade Operating System/PHP and get to 5.5 or newer; as there have been all sorts of problems described on PHP 5.2 and 5.3, and upgrading PHP turned out to be the easier solution. '''
===Linux, macOS and other Unix-like platforms===
 
You may need to install a specific package, e.g. on CentOS, Fedora or Red Hat: '''dnf install php-opcache'''. If necessary add the following to '''php.ini''' (package installers may do this automatically):
<code ini>
zend_extension=/full/path/to/opcache.so
</code>
 
===Microsoft Windows===
 
The extension '''php_opcache.dll''' is included in the '''ext''' folder in the [https://windows.php.net/download PHP for Windows binary downloads].
To enable it add the following to '''php.ini''':
 
<code ini>
zend_extension=php_opcache.dll
</code>


==Configuration==
==Configuration==


PHP.ini settings:
'''php.ini''' settings:


<code ini>
<syntaxhighlight lang="ini">
[opcache]
[opcache]
zend_extension = php_opcache.dll
opcache.enable = 1
opcache.enable = 1
opcache.memory_consumption = 128
opcache.memory_consumption = 128
Line 37: Line 50:
;opcache.enable_cli = 1 ; Speeds up CLI cron
;opcache.enable_cli = 1 ; Speeds up CLI cron
;opcache.load_comments = 0 ; May lower memory use, might not be compatible with add-ons and other apps.
;opcache.load_comments = 0 ; May lower memory use, might not be compatible with add-ons and other apps.
</code>
</syntaxhighlight>
 
When using non-Windows platforms, you have to use the ''zend_extension'' configuration to load the OPcache extension into PHP by adding to php.ini.
<code ini>
zend_extension=/full/path/to/opcache.so
</code>
 
When using IIS you will need PHP 5.5 and you will need to add the extension for opcache under the ''ExtensionList'' section of the php.ini file. For PHP 5.3 and 5.4 you can download the binaries separately from [http://windows.php.net/downloads/pecl/releases/opcache] and you will also need to enter full absolute path to the module dll in php.ini.
 
<code ini>
[ExtensionList]
...
zend_extension=php_opcache.dll
</code>


; memory_consumption
; memory_consumption
From: [http://blog.jpauli.tech/2015/03/05/opcache.html PHP's OPCache extension review]
From: [http://blog.jpauli.tech/2015-03-05-opcache-html/ PHP's OPCache extension review]
*''The size of the memory segment can be told using the opcache.memory_consumption INI setting (Megabytes). Size it big, don't hesitate to give space. Never ever run out of shared memory space, if you do, you will lock your processes, we'll get back to that later.''
*''The size of the memory segment can be told using the opcache.memory_consumption INI setting (Megabytes). Size it big, don't hesitate to give space. Never ever run out of shared memory space, if you do, you will lock your processes, we'll get back to that later.''


*''Size the shared memory segment according to your needs, don't forget that a production server dedicated to PHP processes may bundle several dozens of Gigabytes of memory, just for PHP. Having a 1Gb shared memory segment (or more) is not uncommon, it will depend on your needs, but if you use a modern application stack, aka framework based, with lots of dependencies etc... , then use at least 1Gb of shared memory.''
*''Size the shared memory segment according to your needs, don't forget that a production server dedicated to PHP processes may bundle several dozens of Gigabytes of memory, just for PHP. Having a 1Gb shared memory segment (or more) is not uncommon, it will depend on your needs, but if you use a modern application stack, aka framework based, with lots of dependencies etc... , then use at least 1Gb of shared memory.''


Having that in mind, set opcache.memory_consumption to a value high enough to avoid filling it up (as long as your RAM usage allows you to), and then monitor the OPCache to adjust that value to its optimal size. As the total size of the PHP files in a standard Moodle 3.6 is almost 90MB, setting this value higher than that can be a good idea. Take into account that the PHP files of the plugins and those on the MoodleData folder (language pack files...) also count, so these values can be different on each installation. If you have several instances of Moodle you should multiply that value by the number of instances.
Having that in mind, set opcache.memory_consumption to a value high enough to avoid filling it up (as long as your RAM usage allows you to), and then monitor the OPCache to adjust that value to its optimal size. As the total size of the PHP files in a standard Moodle 3.11 is almost 100MB, setting this value higher than that can be a good idea. Take into account that the PHP files of the plugins and those on the MoodleData folder (language pack files...) also count, so these values can be different on each installation. If you have several instances of Moodle you should multiply that value by the number of instances.


Tip: If using Linux, you can know the total size of the PHP files of a folder using this command:
Tip: If using Linux, you can know the total size of the PHP files of a folder using this command:
Line 69: Line 69:
*''The maximum number of keys (and therefore scripts) in the OPcache hash table. The actual value used will be the first number in the set of prime numbers { 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987 } that is greater than or equal to the configured value. The minimum value is 200. The maximum value is 100000 in PHP < 5.5.6, and 1000000 in later versions.''
*''The maximum number of keys (and therefore scripts) in the OPcache hash table. The actual value used will be the first number in the set of prime numbers { 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987 } that is greater than or equal to the configured value. The minimum value is 200. The maximum value is 100000 in PHP < 5.5.6, and 1000000 in later versions.''


As Moodle 3.6 contains almost 10.000 php files it is recommended above that opcache.max_accelerated_files should be set to 10000 to accommodate this (16229 will actually be used as per the explanation above). If you have several instances of Moodle you should multiply that value by the number of instances.
As Moodle 3.11 contains more than 11.600 php files it is recommended above that opcache.max_accelerated_files should be set to 10000 to accommodate this (16229 will actually be used as per the explanation above). If you have several instances of Moodle you should multiply that value by the number of instances.


If many additional plugins are installed so that your total PHP files exceed 16229 then the next most suitable value for max_accelerated_files should be used.
If many additional plugins are installed so that your total PHP files exceed 16229 then the next most suitable value for max_accelerated_files should be used.
Line 78: Line 78:


==Opcache management plugin==
==Opcache management plugin==
[[File:Opcache_management_message.png|400px]]
You may consider installing the additional [https://moodle.org/plugins/tool_opcache Opcache management] - Moodle plugin which adds a PHP Opcache management GUI to Moodle site administration, a CLI tool to reset PHP Opcache and a Nagios check for PHP Opcache.
You may consider installing the additional [https://moodle.org/plugins/tool_opcache Opcache management] - Moodle plugin which adds a PHP Opcache management GUI to Moodle site administration, a CLI tool to reset PHP Opcache and a Nagios check for PHP Opcache.
[[File:Opcache management status.png|800px]]


==See also==
==See also==
* [http://pecl.php.net/package/ZendOpcache PECL ZendOPcache]
* [http://pecl.php.net/package/ZendOpcache PECL ZendOPcache]
* [http://windows.php.net/downloads/pecl/releases/opcache/ Windows builds for PHP 5.3-5.4]


Forum discussions:
Forum discussions:

Latest revision as of 14:48, 19 July 2021


The standard OPcache extension is strongly recommended; since Moodle 2.6, it is the only solution officially supported by PHP developers. The benefits are increased performance and significantly lower memory usage. However, opcode caching extensions (including OPcache, eAccelerator and APC) aren't compatible with servers configured to use some common types of high-security PHP handlers such as suPHP (the default on WHM / cPanel Linux servers).

Opcache error.png

Installation

The OPcache extension is distributed as part of PHP 5.5 and later. It is available also for older stable PHP releases from PECL under the original name ZendOPcache. To check if the extension is loaded and enabled look at the PHP info page under the Zend OPcache heading.

Linux, macOS and other Unix-like platforms

You may need to install a specific package, e.g. on CentOS, Fedora or Red Hat: dnf install php-opcache. If necessary add the following to php.ini (package installers may do this automatically): zend_extension=/full/path/to/opcache.so

Microsoft Windows

The extension php_opcache.dll is included in the ext folder in the PHP for Windows binary downloads. To enable it add the following to php.ini:

zend_extension=php_opcache.dll

Configuration

php.ini settings:

[opcache]
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 60

; Required for Moodle
opcache.use_cwd = 1
opcache.validate_timestamps = 1
opcache.save_comments = 1
opcache.enable_file_override = 0

; If something does not work in Moodle
;opcache.revalidate_path = 1 ; May fix problems with include paths
;opcache.mmap_base = 0x20000000 ; (Windows only) fix OPcache crashes with event id 487

; Experimental for Moodle 2.6 and later
;opcache.fast_shutdown = 1
;opcache.enable_cli = 1 ; Speeds up CLI cron
;opcache.load_comments = 0 ; May lower memory use, might not be compatible with add-ons and other apps.
memory_consumption

From: PHP's OPCache extension review

  • The size of the memory segment can be told using the opcache.memory_consumption INI setting (Megabytes). Size it big, don't hesitate to give space. Never ever run out of shared memory space, if you do, you will lock your processes, we'll get back to that later.
  • Size the shared memory segment according to your needs, don't forget that a production server dedicated to PHP processes may bundle several dozens of Gigabytes of memory, just for PHP. Having a 1Gb shared memory segment (or more) is not uncommon, it will depend on your needs, but if you use a modern application stack, aka framework based, with lots of dependencies etc... , then use at least 1Gb of shared memory.

Having that in mind, set opcache.memory_consumption to a value high enough to avoid filling it up (as long as your RAM usage allows you to), and then monitor the OPCache to adjust that value to its optimal size. As the total size of the PHP files in a standard Moodle 3.11 is almost 100MB, setting this value higher than that can be a good idea. Take into account that the PHP files of the plugins and those on the MoodleData folder (language pack files...) also count, so these values can be different on each installation. If you have several instances of Moodle you should multiply that value by the number of instances.

Tip: If using Linux, you can know the total size of the PHP files of a folder using this command: find ./ -type f -name "*.php" -printf "%s\n" | gawk -M '{t+=$1}END{print t}' | numfmt --to=iec

max_accelerated_files

From: php.net max-accelerated-files

  • The maximum number of keys (and therefore scripts) in the OPcache hash table. The actual value used will be the first number in the set of prime numbers { 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987 } that is greater than or equal to the configured value. The minimum value is 200. The maximum value is 100000 in PHP < 5.5.6, and 1000000 in later versions.

As Moodle 3.11 contains more than 11.600 php files it is recommended above that opcache.max_accelerated_files should be set to 10000 to accommodate this (16229 will actually be used as per the explanation above). If you have several instances of Moodle you should multiply that value by the number of instances.

If many additional plugins are installed so that your total PHP files exceed 16229 then the next most suitable value for max_accelerated_files should be used. Tip: If using Linux, you can know the total PHP files of your Moodle using this command: find ./ -type f | grep -E ".*\.php$" | sed -e 's/.*\(\.[a-zA-Z0-9]*\)$/\1/' | sort | uniq -c | sort -n

Opcache management plugin

Opcache management message.png

You may consider installing the additional Opcache management - Moodle plugin which adds a PHP Opcache management GUI to Moodle site administration, a CLI tool to reset PHP Opcache and a Nagios check for PHP Opcache.

Opcache management status.png

See also

Forum discussions: