aboutsummaryrefslogtreecommitdiffstats
path: root/posts/centos_apache_php_fpm.md
blob: c10cd436a4f2ef2927771bc02a0e5d15da642d7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 
PHP-FPM!

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