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 | \ tar xzf - && cd mod_fastcgi-2.4.6 && \ make -f Makefile.AP2 top_dir=/usr/lib64/httpd && \ cp .libs/mod_fastcgi.so /usr/lib64/httpd/modules/ && \ cat > /etc/httpd/conf.d/mod_fastcgi.conf <<EOF LoadModule fastcgi_module /usr/lib64/httpd/modules/mod_fastcgi.so <IfModule mod_fastcgi.c> AddType application/x-httpd-php .php Action application/x-httpd-php /php.fcgi </IfModule> 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:
<IfModule mod_fastcgi.c> Alias /php.fcgi /dev/shm/$USERNAME-php.fcgi </IfModule>
$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.d/*.conf. Then for each user that got a
FastCGIExternalServer line, create a configuration file
[$USERNAME] listen = /dev/shm/$USERNAME-php.sock listen.owner = $USERNAME listen.group = apache listen.mode = 0660 user = $USERNAME pm = ondemand pm.max_children = (2 x the number of processors in the server) pm.max_requests = 1024
After that, just start PHP-FPM and restart Apache with:
/etc/init.d/php-fpm start && \ httpd -S && \ /etc/init.d/httpd restart
and you should be all set!