Apache2 mod_fastcgi

mod_fastcgi is considered non-free by the debian guys, so you can find it in the non-free repository. It has 3 mods of operation:
  • Static (predetermined number of) FastCGI forks managed by mod_fastcgi
  • Dynamic FastCGI forks managed by mod_fastcgi, processes are forked as needed by load
  • External FastCGI - it's up to you to start and manage the processes
Using the module is pretty simple, first you define the application as appropriate for the chosen mode, that is FastCgiServer for static, FastCgiConfig for dynamic and FastCgiExternalServer for external mode. After defining the application, Apache needs to be configured to route requests to the FastCGI server by means of SetHandle, AddHandler or Alias directives.
FastCgiExternalServer /var/www/cgi-bin/myapp.fcgi -host 127.0.0.1:9001
Alias /myapp /var/www/cgi-bin/myapp.fcgi
<Location /myapp>
  SetHandler fastcgi-script
</Location> 
/var/www/cgi-bin/myapp.fcgi isn't required to exist, it's present in the configuration to conform with Apache's routing scheme (same as CGI). The use of FastCgiExternalServer directive requires the application to be started by the user, Apache doesn't care which process is actually listening on port 9001 as long as there is one. If you want Apache to control/run the FastCGI application processes, use static or dynamic modes.

php-cgi

Don't use the dynamic FastCGI method, and when using the static method run 1 php-cgi instance and use PHP_FCGI_CHILDREN environment variable to control the number of children. This is required because php-cgi/php-fpm has it's own process manager and Apache will run multiple parents, thus shared memory features like APC will not work as they should. The following example uses mod_actions and the static mode mentioned above. Action directive is used to define a new handler unique to php-cgi, this is the correct way to map file types to be served by a (fast)cgi program and probably the only way that won't break PATH_INFO, etc.
<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  FastCgiIpcDir /var/lib/apache2/fastcgi
  AddType application/x-httpd-php .php .phtml .php3
  AddType application/x-httpd-php-source .phps
  AddHandler php-fastcgi .php .phtml .php3
  Action application/x-httpd-php /fastcgi/bin/php
  Action php-fastcgi /fastcgi/bin/php
  FastCgiServer /usr/bin/php-cgi -initial-env PHP_FCGI_CHILDREN=4 -initial-env PHP_FCGI_MAX_REQUESTS=10000
  ScriptAlias /fastcgi/bin/php /usr/bin/php-cgi
  <Location /fastcgi/bin/php>
      SetHandler fastcgi-script
  </Location>
</IfModule>

mod_rewrite (mis)behaviour

The most important thing to remember is to always append PT option to the RewriteRule, so it will be handles by other modules. There seems to be a bug causing the internal redirect performed by Action to be caught be RewriteRule even when using the NS options. The easiest workaround is perpending something like
RewriteRule /fastcgi/bin/php - [PT]
to the rewrites.

-- AvishaiIshShalom - 08 Jul 2010
Topic revision: r6 - 10 Mar 2011 - 06:23:33 - AvishaiIshShalom
 

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback