I have setup my website on a new hosting (virtual cloud), however I am looking at the opcache and the scripts not being used for say a minutes or so are removed from the cache.
So is there a way to stop it? or is it a normal behaviour?
Thanks a lot.
There is configuration for that actually.
opcache.revalidate_freq=2, the default value is 2 seconds, opcache will try to check for timestamps every 2 seconds and if the files are changed it will revalidate.
You can change the value to match your needs, or you can just turn timestamp check off using this conf opcache.validate_timestamps=0, but in this case each time you deploy code to production you have to restart php-fpm (if you are using php-fpm) and web server
opcache.revalidate_freq=2 or any integer value
opcache.validate_timestamps=1 or opcache.validate_timestamps=0
Sounds like you need to define validate_timestamps=0 in php.ini. Beware of this though - if you upload any changes to your PHP files you will need to restart either Apache (if you use mod_php5) or PHP5-FPM, or clear the opcache manually.
For details on how to clear the opcache manually you basically have to create a PHP file with opcache_reset() and run it, but this has to be in the same SAPI as your other files - i.e. run by PHP5-FPM if that is what is serving the rest of your files.
http://ihaveabackup.net/2013/10/19/invalidating-the-opcache-in-php-5-5/
Related
I'm trying to get opcache working on my server that is hosting a website in apache2. I'm running debian 8.
However it seems to be hitting zero of the scripts.
I've already tried to change the php mode to cgi only but this didn't change anything. I've also tried to disable suPHP with the following command
a2endismod suPHP
again this didn't do anything for the problem.
Does anyone happen to know what could be causing this?
This was already answered here
To quote:
This looks like you are using cgi (FastCGI) rather than mod_php5. The shared memory area (SMA) is used for both, but it only persists request-to-request for the latter.
However, I should add that enabling following:
opcache.file_cache=/var/tmp/php/opcache
opcache.file_cache_only=1
made OPcache work with my setup - which is Apache+php as CGI/FastCGI (NOT php-fpm). Obviously this caching mechanism is filebased, not SHM (make sure
/var/tmp/php/opcache is accessible by webserver, might need 777 permissions).
You won't be seeing any cache hits/misses in the statistics after enabling the above, since - again - we disabled SHM Cache.
PS. sorry for necroposting, was just looking for a solution for a similar issue.
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?
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!
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).
A few of my sites seems to have server-side cache and I keep having to restart the httpd server every time I make changes to the PHP code
Is there a way not to restart Apache but have PHP code changes show up instantly?
7 years ago..
I'm not sure you would read this or not.
But I think what you want was changing opcache.enable option.
Go to php/etc/php.ini and try to change opcache.enable=0
But I think you'd better do that for development, not real services.
when we deploy our app, we simply create a new folder and point a symbolic link to it, so apache will always find its way to the latest build.
However, we get strange errors when we deploy and continue testing without first rebooting the apache server. We also have APC running and have a feeling that caching has something to do with this.
Is it normal that an apache restart is required when deploying a new version of our php application when APC is active? Or is there a better way, e.g. clearing the APC cache using a shell script?
You can use apc_clear_cache().
See related questions:
How to clear APC cache entries?
How to clear APC cache without crashing Apache?
depends if you have the apc.stat setting in php.ini On or Off. If Off (typical for a production site) then you need to clear the code cache or restart apache; if On, then it should pick up the new code automatically
Normally, APC will 'stat' each PHP file to see if it has been changed since it was last cached. So restarting Apache is not required for all application upgrades.
BUT if your application uses apc_store() to store application data in the cache and some of that data might change after an upgrade, then restarting Apache is an easy way to flush the entire APC cache.
I believe apache2ctl graceful would work, too.
Also, APC performs a little better if you turn off the 'stat' checking; so if you disable that feature, then you'll want to restart Apache anyway.
I asked this question on serverfault but I didn't get any response. I try here...
I developped a site on my web server at home.
When I modify the translation files, I have to restart the web server Apache.
/etc/init.d/httpd graceful
Easy...
Suppose that my site is hosted on shared host.
Suppose now I need to modify the translation files.
I can't restart the server...
How do you use gettext in this context?
Are you allow to restart the server with the option graceful only?
Does the share host restart the server once a day to resolve this kind of problem?
How do you work with such constraints?
call clearstatcache();
after making translation from *.po to *.mo
the post here on the gettext function has some information for making gettext work without restarting apache:
http://www.php.net/manual/en/function.gettext.php#58310
You can rename your *.mo file and your gettext domain to flush gettext cache without restarting apache server.But in production environment,it's unsuitable.
There is no other solution than to brutally force a cache miss for each and every *.mo file every time one of them changes. Change a single string? New cache era!
I do this by writing middleware that copies all *.mo files to a timestamp-named directory (the timestamp being that of the newest *.mo file) and updates the application config with the new message path - if the newest timestamp actually changed, that is. Thus textdomains are now loaded from completely different paths, and this is what finally manages to kill the stupid gettext cache stinking dead.