Running Apache 2 and PHP is simple with mod_php but there are more efficient alternatives like using PHP-FPM (FastCGI Process Manager) which is an alternative PHP FastCGI implementation. With it the PHP process runs standalone without the need for a web server and listens for incoming requests on either a TCP or a Unix socket. Web servers can connect the PHP process and send requests using the FastCGI protocol. It solves mod_php’s problem of spinning up and destroying PHP instances with every request and thus is more memory efficient and provides better performance.
These instructions are for CentOS 6.4 but the process should however work similarly with other Linux distributions.
Setting up the PHP-FPM
Install the FPM-CGI binary for PHP and add it to start after server reboot:
# yum install php-fpm
# chkconfig --levels 235 php-fpm on
Configure the PHP-FPM pool in /etc/php-fpm.d/www.conf to use sockets and enable some status information for e.g. Munit:
;listen = 127.0.0.1:9000
listen = /tmp/php5-fpm.sock
pm.status_path = /status
ping.path = /ping
Start the service with:
service php-fpm start
Setting up Apache and mod_fastcgi
Apache can be configured to run FastCGI with two modules: mod_fastcgi and mod_fcgid. The difference is explained at Debian bug report #504132: “mod_fcgid passes just one request to the FCGI server at a time while mod_fastcgi passes several requests at once, the latter is usually better for PHP, as PHP can manage several request using several threads and opcode caches like APC usually work only with threads and not with processes. This means that using mod_fcgid you end up having many PHP processes which all have their very own opcode cache.”
In short: mod_fastcgi is better.
Install mod_fastcgi
So we need to get mod_fastcgi which isn’t at the time found from CentOS default or EPEL repos but from RPMForge or by building it from sources.
Getting mod_fastcgi from RPMForge
Install the RPMForge repo:
# wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
# rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
Add some priorities which repo to use:
# yum install yum-priorities
# vi /etc/yum.repos.d/epel.repo
... add the line priority=10 to the [epel] section
Install mod_fastcgi
# yum install mod_fastcgi
Or building mod_fastcgi from sources
You can build the mod_fastcgi from sources. Make sure required packages are installed (httpd-devel and apr-devel required to compile mod_fastcgi):
# yum install libtool httpd-devel apr-devel apr
Get the latest mod_fastcgi source code:
# cd /opt
# wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz
Untar tar ball:
# tar -zxvf mod_fastcgi-current.tar.gz
# cd mod_fastcgi-2.4.6/
As we are using Apache 2, we make a copy of Makefile.AP2: cp Makefile.AP2 Makefile
Compile and install mod_fastcgi for 64 bit system:
# make top_dir=/usr/lib64/httpd
# make install top_dir=/usr/lib64/httpd
Configure mod_fastcgi
If you have php enabled disable it
# mv /etc/httpd/conf.d/{php.conf,php.conf.disable}
Set up a (non-existent) directory that Apache can route the requests through. That directory must be available to Apache and it might be /usr/lib/cgi-bin/ so the routed file is then e.g. /usr/lib/cgi-bin/php5-fcgi.
# mkdir /usr/lib/cgi-bin/
Configure mod_fastcgi settings in /etc/httpd/conf.d/mod_fastcgi.conf to be:
LoadModule fastcgi_module modules/mod_fastcgi.so
DirectoryIndex index.php index.html index.shtml index.cgi
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /tmp/php5-fpm.sock -pass-header Authorization
# For monitoring status with e.g. Munin
SetHandler php5-fcgi-virt
Action php5-fcgi-virt /php5-fcgi virtual
We add handler and action which sends all requests of PHP to the virtual URL created above, which is in turn then sent to the external FastCGI server. We also add configuration to have some status information about our PHP-FPM.
Start Apache:
# service httpd start
PHP should now work.
Leave a Reply