4bb7b13a2db5fef3113b14b9f05dd7724ff01da3 — François Kooman 3 years ago 1876b03
update post
1 files changed, 81 insertions(+), 0 deletions(-)

A posts/centos_apache_php_fpm.md
A posts/centos_apache_php_fpm.md => posts/centos_apache_php_fpm.md +81 -0
@@ 0,0 1,81 @@
title: CentOS with Apache and PHP-FPM (revisited)
published: 2018-05-30
modified: 2018-06-01

Almost two years I wrote a [post](apache_php_fpm.html) 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:

* Switch Apache to use MPM Event;
* Switch PHP-FPM to use the Unix socket;
* Make existing Apache "mod_php" configuration snippets work with PHP-FPM
  without modification.

### 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](https://src.fedoraproject.org/rpms/php/raw/master/f/php.conf) 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 

Furthermore, we switch to 
[MPM Event](https://httpd.apache.org/docs/trunk/mod/event.html) 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](https://secure.php.net/manual/en/intro.opcache.php) extension to 
further improve performance:

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