Using PHP-FPM with Apache 2 on CentOS

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.


Posted

in

by

Tags:

Comments

9 responses to “Using PHP-FPM with Apache 2 on CentOS”

  1. Dirk Avatar

    Very helpful post, but I had to add

    listen.owner = apache
    listen.group = apache

    in /etc/php-fpm.d/www.conf to make this work.

    Cheers,
    Dirk

  2. Thomas Avatar
    Thomas

    Awesome post, thanks.

  3. Rob Donovan Avatar

    Hi,

    Thanks for the write-up/info.

    Although I already had fastcgi up, I couldn’t get the status page to work.

    After I put in your ‘location’ bit, it all worked fine though.

    Ta,

    Rob.

  4. Paul Avatar
    Paul

    After setting this up, HTTP basic authorization (such as is used in phpMyAdmin or apcu-panel) does not work.

    1. Marko Avatar

      PHP-FPM and mod_fastcgi has nothing to do with htaccess as it’s Apache’s config file. Apache will read .htaccess files as long as AllowOverride is enabled in httpd.conf. I would suggest to check the config and logs for some clues why it’s not working for you.

      1. Paul Avatar
        Paul

        I don’t think you understood what I’m asking about. After following your tutorial, I installed phpMyAdmin, and edited the config.inc.php file so that this line:

        $cfg[‘Servers’][$i][auth_type] = ‘cookie’

        is replaced by

        $cfg[‘Servers’][$i][auth_type] = ‘http’

        Now, whenever I go to my phpMyAdmin page, the Authentication Required popup says,

        “The server http://[myIPaddress]:80 requires a username and password. The server says: phpMyAdmin localhost

        and when I enter the correct username and password, it merely shows that popup again.

        1. Marko Avatar

          Yeah now I get what you mean.

          I haven’t noticed that kind of problem with PhpMyAdmin (e.g. 3.5.8) and using http authentication. I just set up a new CentOS VPS and both http and cookie auth worked fine. So, as I can’t duplicate this problem, I can’t say how to fix it.

          Maybe some other setting is messing up your authentication?

      2. Paul Avatar
        Paul

        This has nothing to do with htaccess!

Leave a Reply to Paul Cancel reply

Your email address will not be published. Required fields are marked *