How to disable PHP OPCache for certain directories

Deactivate OPCache per folderIn 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.

In short: What is PHP OPCache? OPCache is an extension for PHP, which accelerates the execution of PHP scripts by caching their bytecode. If a PHP script is called, it is first translated into platform-independent bytecode and after that this bytecode is translated into machine code by the Zend Engine. The translation from script to bytecode is usually done each time the script is called. At this point, OPCache comes into play. OPCache stores the precompiled bytecode in the main memory (RAM). When a script is called again, it is not translated again into bytecode, but the already translated bycode is loaded from the working memory by OPCache.

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.


/var/www/developer/*
/var/www/dynamic_values.php

(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)


nano /etc/php5/fpm/conf.d/05-opcache.ini

At the end of the file, add the following parameter including the path:


opcache.blacklist_filename=/etc/php5/fpm/opcache-blacklist.txt

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.

1 Comments

  1. A little addition about OPCache blacklist file — it seems that if you working under Windows, you can not use wildcards (“*” and “?” symbols) inside it.
    More info.

Leave a Reply to scorp13 Cancel reply

Please be polite. We appreciate that. Your email address will not be published and required fields are marked