How to disable apache cache

Hi,

I am running the LAMP stack on my dietpi raspi. All files from the webserver will use a mounted NAS drive for serving any website request.
The issue that I am currently facing is that when I do only change very small parts of any php file, like only a couple of bytes, the results will not be served to the clients from my apache.
I figured out that the NAS will not do any caching but obviouls my apache will do so. Can any one here tell me how to disable caching in the apache configuration? I found some hints on the web like “mod-cache” and “CacheDisable” but I do not know where to apply these kind of changes. What are the defaults for caching from a dietpi based apache installation and into which file do I need to apply my own configuration?

Best regards,
Olaf

Hi Olaf,

DietPi installs APC and OPcache for all webserver stacks and PHP. You’ll need to disable those modules, simply remove the files:

rm /etc/php5/mods-available/apcu.ini
rm /etc/php5/mods-available/opcache.ini
dietpi-services restart

Thanks for that reply. I really do not understand the two files and what they are exactly good for.
Is it also possible to just exclude caching for certain URLs?
And if I want to reenable the caching at a later time, how do I get back?

I renamed the files, I think that does the same work. Much quicker to read php-files no.

sudo mv file-name file-name-original

JohanXtian

Disabling APCu and OPcache is definitely the wrong way to solve such issues, as it dramatically decreases performance for requests when PHP is involved, or beaks the application completely.

APCu is only used when the application is actively configured to do so, and then there is usually an option to clear the cache manually, when required. When the PHP module is disabled (by (re)moving the file), the application is broken, will throw an error page as fast as it attempts to read or write to the cache. So before ever doing something, first the application needs to be configured to not use APCu anymore, but that is not recommended. When the application still works after disabling the PHP module, then it was not used and it had no effect that the module was disabled :wink:.

OPcache is automatically used, when enabled, and disabling it won’t break anything, but on each request, PHP has to read and compile the original PHP source file, which is a large overhead, compared to the optimised RAM-cached version, which OPcache serves. It has a mechanism to verify that the cached file still matches the original PHP file after a defined time, and we set that to 60 seconds indeed, which means that changes to local PHP files take up to 60 seconds before clients will see them. This is good for performance reasons, as also the validation takes time, but it is still much faster than parsing the origin file each time. If you need changes to take effect in under a minute, then I suggest doing the following (instead of disabling the OPcache):

echo -e '; Custom PHP overrides\n; priority=99\nopcache.revalidate_freq=1' > /etc/php/7.3/mods-available/custom.ini
phpenmod custom

This assumes PHP7.3 to be used, and the path hence needs to be changed on Debian Bullseye systems, which use PHP7.4 by default. It sets the re-validation to 1 second, so local PHP file changes are seen by clients after at max a second.

This way you have a transparent override for default PHP settings, which you can easily enable (phpenmod) and disable (phpdismod) or remove again without loosing the defaults or having custom changes reverted on an upgrade or reinstall.