Is it possible to check if a PHP opcode cache is installed and running on a server without having SSH access to the server? I've checked phpinfo() and can see no reference to APC or eAccelerator, but I'm aware there are other opcode cache systems and am not totally sure that checking phpinfo() is enough to let me know one way or the other.
var_dump(function_exists('apc_store')); or equivalent for the library of your choice.
If using a web server SAPI, as per usual, make sure you restart the server process after editing your php.ini to see the new additions reflected.
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).
Sometimes I like to use PHP's built-in development server to work on quick stuff like so:
php -S 127.0.0.1:8888
However I seem to run into heavy caching issues from time to time where I need to restart the server in order to see even simple HTML changes.
I haven't been able to find any options to disable this or otherwise pinpoint what might be causing this frustration.
Note: I usually have Chrome's Developer Tools open with browser caching disabled while I'm doing this. I don't think it's a browser caching issue.
Is there anything I can try?
If modifying your php scripts isn't resulting in a change, it could be a problem with opcode caching as opposed to browser caching (browser caching should be easily overridden by CTRL-F5 or CTRL-R, depending on your OS). This happens when the server is pre-compiling your php code to speed up responses.
This was the case with me, so I went in to /etc/php5/cli/conf.d/ and found the symlink to 05-opcache.ini and deleted that symlink, and restarted the builtin server. The problem where pages wouldn't change when I made edits to php code was resolved from that point onwards.
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'm using APC to cache user variables (with the apc_store/apc_fetch commands). I've also enabled APC for the CLI with the option "apc.enable_cli = 1". However, the CLI version of PHP seems to access a different APC cache from the version used by Apache.
Is it possible to configure APC to use the same cache for both CLI and web invocations?
Not possible.. The only way to accomplish something like what your asking is to use something like memcacheD. Or run what you need to run through your webserver. What's running CLI that you cannot run via a web script with a cronjob?
You can use shm. This technology lend to access to Unix Shared memory. You can put some variable in shm and then in another scritp, even programmed in another languaje you can get the shared variables.
shm_put_var and shm_get_var.
It's slower than APC, but it's faster than memcached, redis, etc.
I hope It will help you, and I'm sorry for my English....
call your CLI as a CGI
/path-to/cgi-sys/php5.cgi /home/name/crons/engine.php
you would need a web server written in php -- the APC cache is shared only by forked child processes. If you had a php webserver, you could start a master cli, init apc, fork and load/run the web server in one child process, and fork and run your php cli script in another. Kind of a gross hack, huh. Fork and require(), I don't think the apc cache would survive an exec()