Apache MPM Worker and PHP-FPM on CentOS

Written by
Date: 2013-07-13 10:04:11 00:00


Introduction

A LAMP server is one of the most used configurations of the GNU/Linux operating system. And being Wordpress the most used CMS to power Internet sites, it is not surprise that Apache + PHP + MySQL is mostly used to power Wordpress installations.

Apache can work in different modes, being prefork the most used one, but worker is more efficient. Read more about differences between prefork and worker MPM

I have shown in the past how to install Apache MPM Worker and PHP-FPM on Ubuntu, because Ubuntu and Debian is what I use the most. But a friend asked me how to do it for CentOS, so, here we go.

Installing Apache MPM Worker with PHP-FPM and MySQL on CentOS

This have been tested on CentOS 6, and more specifically on a 64 bits installation on a Digital Ocean VPS.

First we need to add one repository.

rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt
cd /tmp/
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

Now let's install the needed packages.

yum install vim php-fpm mod_fastcgi httpd mysql mysql-server php-mysql

And make all daemons start automatically when we reboot the server

chkconfig --levels 235 httpd on
chkconfig --levels 235 mysqld on
chkconfig --levels 235 php-fpm on

Add a name to the Apache server, this is to avoid the warning message of, Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName.

echo "ServerName localhost" >> /etc/httpd/conf.d/servername.conf

Now, configure the fastcgi Apache module. We need to make to changes.

vim /etc/httpd/conf.d/fastcgi.conf

Change this

FastCgiWrapper On

for this

FastCgiWrapper Off

and add this at the end

<IfModule mod_fastcgi.c>
    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 /var/run/php5-fpm.sock -pass-header Authorization
</IfModule>

That will make it possible for Apache to pass PHP code back to PHP-FPM and get the responce once the code have been executed.

Because we are using the folder /usr/lib/cgi-bin/ we need to create it:

mkdir -p /usr/lib/cgi-bin

On Debian, PHP-FRM listens to a socket by default, but on CentOS it listens to a TPC port, in order to reserver TCP ports for Apache, we will change PHP-FPM to be like Debian.

vim /etc/php-fpm.d/www.conf 

change this line

listen = 127.0.0.1:9000

for this one

listen = /var/run/php5-fpm.sock

Finall restart all servers.

service httpd restart
service php-fpm restart
service mysqld restart

You can now install your PHP application, and have a more efficient Apache server.