François' Blog

CentOS with Apache and PHP-FPM (revisited)

Published on 2018-05-30 | Last modified on 2018-06-01

Almost two years I wrote a post about using Apache with PHP-FPM on CentOS. Some things have changed (for the better) and also I somehow understand things better now! This post is about configuring Apache with PHP-FPM on CentOS 7. The whole point of this is to increase the performance of running PHP scripts. Even with PHP 5.4, the default on CentOS, you'll see a substantial performance improvement compared to mod_php.

We will:

Installation

$ sudo yum -y install httpd php-fpm

Make sure you don't have mod_php installed, i.e.:

$ sudo yum -y remove php

Configuration

Apache

We steal the configuration file from Fedora where since a few releases the PHP installation defaults to PHP-FPM. We install that in /etc/httpd/conf.d/php.conf. The big advantage here is that you don't need to modify any other Apache configuration files to make use of PHP-FPM!

Furthermore, we switch to MPM Event for performance reasons. Modify /etc/httpd/conf.modules.d/00-mpm.conf and disable the mpm_prefork_module and enable the mpm_event_module:

#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
LoadModule mpm_event_module modules/mod_mpm_event.so

Don't forget to restart Apache:

$ sudo systemctl restart httpd

PHP

By default on CentOS PHP-FPM will listen on a TCP port, we want to modify this to use a Unix socket for performance reasons. Make sure you specified/modfied at least these options in /etc/php-fpm.d/www.conf:

listen = /run/php-fpm/www.sock
listen.group = apache
listen.mode = 0660

The socket permissions will look like this, enough for Apache to talk to the socket:

$ ls -l /run/php-fpm/www.sock
srw-rw----. 1 root apache 0 May 23 09:16 /run/php-fpm/www.sock

Don't forget to restart PHP-FPM:

$ sudo systemctl restart php-fpm

This should be all to run your PHP scripts a lot faster with minimal configuration changes and be in line with the (default) configuration of PHP on Fedora and on CentOS 8 when it is finally released!

Update (2018-06-01): do not forget to install the OPcache extension to further improve performance:

$ sudo yum -y install php-opcache
$ sudo systemctl restart php-fpm

History