Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share APC cache between several PHP processes when running under FastCGI?

Tags:

I'm currently running several copies of PHP/FastCGI, with APC enabled (under Apache+mod_fastcgi, if that matters). Can I share cache between the processes? How can I check if it's shared already? (I think the apc.mmap_file_mask ini setting might be involved, but I don't know how to use it.)

(One of the reasons I think its not shared at the moment is that the apc.mmap_file_mask, as reported by the apc.php web interface flips between about 3 different values as I reload.)

like image 841
mjs Avatar asked Feb 28 '09 18:02

mjs


2 Answers

APC does not currently share its cache between multiple php-cgi workers running under fastcgi or fcgid. See this feature request for details: "this behaviour is the intended one as of now".

One workaround is to allow PHP to manage its own workers. You can do this using the PHP_FCGI_CHILDREN environment variable in your wrapper script (plenty of examples all over the web for that). You should also stop fastcgi/fcgid from spawning more than one PHP process if you want to use this method.

The disadvantage with PHP_FCGI_CHILDREN is that its management of the workers is not as good as that provided by fcgid/fastcgi.

So, there we are. APC in a fcgid/fastcgi environment means giving each PHP worker their own cache, or disabling fcgid/fastcgi's process spawning in favor of PHP's built-in management. Let's hope this changes in the future.

like image 115
Dominic Scheirlinck Avatar answered Sep 18 '22 12:09

Dominic Scheirlinck


While it's not perfect the method Domster suggested is the best. I've been doing this for a short time on some low volume sites without errors. I wrote up a detailed explanation on how to set up mod_fastcgi with a shared opcode cache last night.

I found it very important to use mod_fastcgi rather than the newer mod_fcgid because mod_fcgid will only send one request at a time to the PHP process regardless of how many children PHP has spawned via PHP_FCGI_CHILDREN.

like image 24
blt04 Avatar answered Sep 17 '22 12:09

blt04