In the documentation it says "mostly used for debugging" which would lead me think "never enable it unless you've a problem and need to do some debugging," however reading mostly everything that I could find about it says to enable it "opcache.enable_cli 1" but why? I could not find any information concerning this matter, so if anybody knows, why should I enable it if the documentation basically says to keep it on 0?
Leave it off. It's primarily there for use while debugging issues with OPcache itself.
The opcache.enable_cli
option enables PHP OPcache when running PHP scripts from the command line (using the php
command). However, keep in mind that for PHP 5.x the OPcache extension works by storing cached opcodes in the memory of the current process. This is only useful when the process that's running PHP is going to be handling multiple requests that can reuse these opcodes, like in a web server or under FastCGI. For a process like the PHP CLI, which runs one "request" and exits, it just wastes memory and time.
With PHP7 and file-based caching, it can now make sense to enable opcache for CLI. The best possibility would be to have a separate php.ini for CLI with the following configuration:
opcache.enable=1
opcache.enable_cli=1
opcache.file_cache="/tmp/php-file-cache"
opcache.file_cache_only=1
opcache.file_cache_consistency_checks=1
opcache.file_cache_only=1
makes sure that the in-memory opcache is disabled and only files are used, which is what you want for CLI. This should boost execution time by quite a bit.
In the php.ini for FPM, you will want to have the same settings but use opcache.file_cache_only=0
, so in-memory opcache is used and the file cache is used as a fallback (which also makes FPM faster, because the file cache reduces warmup time when FPM is restarted or opcache is reset, because the cached files remain).
This way, CLI and FPM share the file cache, and FPM has the in-memory cache as a second primary cache for maximum speed. A great improvement in PHP7! Just make sure to choose a directory for opcache.file_cache
that both CLI and FPM can write to, and that the same user does the writing/reading.
UPDATE 2017
I would not recommend to use the file cache with FPM anymore (only use it for CLI), because there is no way to reset the cache when setting opcache.validate_timestamps=0
- the file cache prevents PHP-FPM from recognizing any changes, because opcache_reset()
or even a complete PHP-FPM restart does not affect the file cache and there is no equivalent for the file cache, so changed scripts are never noticed. I reported this as a "bug"/"feature request" in March 2016, but this is currently not seen as an issue. Just beware if you use opcache.validate_timestamps=0
!
opcache.file_cache_only=0
, what should the value of opcache.memory_consumption
be, on a 8GB RAM server? –
Gadroon rm
work? –
Glaikit opcache_reset()
. Race conditions can easily occur: between the rm
and opcache_reset()
new files could have already been created. Especially when you deploy an application some files will have changed and there is no "atomic" way to delete the file cache and the opcache in sync. PHP-FPM might also have issues if it tries to read a file that has just been deleted by rm
. –
Jena Fatal Error opcache.file_cache_only is set without a proper setting of opcache.file_cache
, try opcache.file_cache="/tmp/"
–
Hydrophilous mv
or rm
the file-based OPCache directory itself, not its content during deployment. This would make it unusable to PHP process (because it does not like to create it!). Thus when the directory is missing you are safe to run opcache_reset()
, and then recreate the directory in place. There would be no race conditions. –
Glaikit Leave it off. It's primarily there for use while debugging issues with OPcache itself.
The opcache.enable_cli
option enables PHP OPcache when running PHP scripts from the command line (using the php
command). However, keep in mind that for PHP 5.x the OPcache extension works by storing cached opcodes in the memory of the current process. This is only useful when the process that's running PHP is going to be handling multiple requests that can reuse these opcodes, like in a web server or under FastCGI. For a process like the PHP CLI, which runs one "request" and exits, it just wastes memory and time.
As per PHP docs:
opcache.enable_cli boolean
enables the opcode cache for the CLI version of PHP. This is mostly useful for testing and debugging.
Therefore it should be disabled unless you're really need this.
This can be useful when you've some long-term migration process running from the command-line (personally I've tested OPcache v7.0.3 for CLI by running some extensive migration script and I didn't see much performance improvements).
© 2022 - 2024 — McMap. All rights reserved.