In this article, I want to show you how to disable the OPCache based caching for certain directories. (If you need a short introduction to the subject of OPCache, you should scroll down to the blue box in this article.) Although OPCache is a good idea in almost all cases, there are, as with many thing, situations where you should make an exception. Especially when it comes to the development or if a bugfix is to be tested, it can make sense to disable OPCache.
However, since other projects that benefit from OPCache are often running on the same web server, a complete deactivation of OPCache for such test cases can not be an option. Therefore, this article is about how to disable OPCache on the directory level.
Since it should be clear what OPCache is doing and why we want to disable only isolated directories, we come to the implementation of our project.
Disable OPCache for single directories and files
To disable OPCache at the directory level, a so-called blacklist can be created. This blacklist is a text file which can contain any number of file- and/or directory paths, which should not be cached by OPCache. For our example, we want to exclude the directory /var/www/developer as well as the file /var/www/dynamic_values.php from caching. To do so, we create a blacklist file with the following content.
(The * wild card must be written, so that all files in the directory are excluded.)
It doesn’t matter where we save the blacklist-file, because we can freely define the location in the OPCache configuration. This is also the next step. Therefore open the OPCache configuration file with a text editor. (On my server, for example, the configuration is located in the directory: /etc/php5/fpm/conf.d)
At the end of the file, add the following parameter including the path:
That’s it. For the blacklist to be attracted, PHP should be restarted once again. In my case, when using PHP-FPM, the following command is required:
service php5-fpm restart
If everything has worked out, the developers directory as well as the dynamic_values.php file are now excluded from OPCache. For questions, criticism or ideas feel free to write a comment.