title: Sharing PHP-CGI between Apache and NGINX
body: <p>I've been running most of my PHP apps on my website on a fastcgi backend, served by NGINX. I have a new application which seems pretty well integrated with Apache; it's heavily dependent on dynamically written .htaccess files, for example. I'm working on modifying it to work natively with NGINX, but that's not yet ready. In the meantime, I was going to have NGINX proxy all connections to that path straight to Apache, and let Apache handle it.</p>
  
  <p>However, I'm on a memory limited VPS, and I'd rather not run one set of PHP-CGI processes for NGINX (with their own APC cache) and another for Apache (using more memory for <em>their</em> APC cache). Has anyone had any luck sharing PHP between the two?</p>
  
  <p>mod_ fcgid doesn't appear to support using already running servers, so I tried mod_ fastcgi. This seemed to work at first, but was sucking up quite a lot of memory (committed -- it was growing, not just static). Worse, I couldn't get the DirectoryIndex line in my apache configs to work with mod_fastcgi! </p>
  
  <p>I added</p>
  
  <pre>
      AddHandler php5-fastcgi .php
      FastCgiExternalServer /var/www -host 127.0.0.1:9000
  </pre>
  
  <p>to my Apache config, and all .php pages are processed, but a path like <a href="http://example.com/foo/" rel="nofollow noreferrer">http://example.com/foo/</a> doesn't server /foo/index.php! If I remove FastCgiExternalServer, then it does find the index.php, but of course just shows it as text. </p>
  
  <p>I'm assuming it's because DirectoryIndex is processed after FastCgiExternalServer, but I have no idea how this can be changed. </p>