Using PHP-FPM with Apache 2.2

Using PHP-FPM with Apache 2.2

Mar 16

  • Created: Mar 16, 2012 4:39 PM

Using PHP-FPM with Apache 2.2
There a lot of ways to use PHP with Apache, with the most popular probably being mod_php due to its ease of use. However there are many problems with mod_php both security and performance-wise and other solutions have been created over time to address those problems. PHP-FPM (PHP FastCGI Process Manager) is one of the newer PHP SAPIs and generally is (from our initial testing) an excellent combination of the performance of FastCGI and security benefits of suPHP. Unfortunately the setup can be somewhat confusing so I’ll walk through the process on 64bit CentOS 5.

The first step is install mod_fastcgi (replace lib64 with lib if installing on a 32 bit system):

curl -s http://fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz | \<br />
tar xzf - &amp;&amp; cd mod_fastcgi-2.4.6 &amp;&amp; \<br />
make -f Makefile.AP2 top_dir=/usr/lib64/httpd &amp;&amp; \<br />
cp .libs/mod_fastcgi.so /usr/lib64/httpd/modules/ &amp;&amp; \<br />
cat &gt; /etc/httpd/conf.d/mod_fastcgi.conf &lt;&lt;EOF<br />
LoadModule fastcgi_module /usr/lib64/httpd/modules/mod_fastcgi.so</p>
<p>&lt;IfModule mod_fastcgi.c&gt;<br />
    AddType application/x-httpd-php .php<br />
    Action application/x-httpd-php /php.fcgi<br />
&lt;/IfModule&gt;<br />
EOF


Then for each user you want to have access to PHP-FPM add FastCGIExternalServer /dev/shm/$USERNAME-php.fcgi -socket /dev/shm/$USERNAME-php.sock -flush (with $USERNAME replaced with their username of course) to /etc/httpd/conf.d/~mod_fastcgi.conf. Now, for each virtualhost you need to add a section like this:

&lt;IfModule mod_fastcgi.c&gt;<br />
    Alias /php.fcgi /dev/shm/$USERNAME-php.fcgi<br />
&lt;/IfModule&gt;

with $USERNAME replaced with the user that “owns” that virtualhost.

Now you’ll need to install PHP-FPM either by compiling it or via yum. We use our own PHP RPMs but the IUS repo has PHP5.3 RPMs with PHP-FPM so those could work. Once PHP-FPM is installed you’ll want to make sure /etc/php-fpm.conf is include’ing /etc/php-fpm.d/*.conf. Then for each user that got a FastCGIExternalServer line, create a configuration file /etc/php-fpm.d/$USERNAME.conf with:

[$USERNAME]<br />
listen = /dev/shm/$USERNAME-php.sock<br />
listen.owner = $USERNAME<br />
listen.group = apache<br />
listen.mode = 0660<br />
user = $USERNAME<br />
pm = ondemand<br />
pm.max_children = (2 x the number of processors in the server)<br />
pm.max_requests = 1024

After that, just start PHP-FPM and restart Apache with:

/etc/init.d/php-fpm start &amp;&amp; \<br />
httpd -S &amp;&amp; \<br />
/etc/init.d/httpd restart

and you should be all set!

Posted in: Nexcess
  • http://www.facebook.com/gergely.bertyak Bertyak Gergely

    php fpm isnt capable running MASS amount of usernames, with 50.000 user pool, the master process eats up to 1Gb of ram…. hope they implement a nicer way of storing pool definitions