Simply Fast WordPress [4] Techniques to Speed Up WordPress 540% with CentOS 7 (Cont)

Configure Apache with event MPM+php-fpm

We are almost done with this set of tuning. We will be changing the configuration of Apache from prefork MPM+mod_php to event MPM+php-fpm.

Previously, Apache's main multiprocessing module was prefork MPM, but since Apache 2.4 you can choose to use event MPM. If you couple event MPM with fast cgi's php-fpm, your machine will use less memory and prevent the performance drop related to increased concurrent connections.

If you want something to compare it to, it is very similar to the recently popular lightweight high speed web server Nginx. This is probably why this configuration has been dubbed the new Apache, since it takes the merits of Apache's abundant modules and flexible htaccess control and produces much better performance.

First, install and enable php-fpm.

[root@ip plugins]# yum install php-fpm -y
[root@ip plugins]# systemctl enable php-fpm
[root@ip plugins]# systemctl start php-fpm

Next we will edit Apache's settings files: "/etc/httpd/conf.modules.d/00- mpm.conf", "/etc/httpd/conf.d/php.conf", "/etc/httpd/conf/httpd.conf"

1. # Select the MPM module which should be used by uncommenting exactly
2. # one of the following LoadModule lines:
3. # prefork MPM: Implements a non-threaded, pre-forking web server
4. # See: http://httpd.apache.org/docs/2.4/mod/prefork.html
5. #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
6. # worker MPM: Multi-Processing Module implementing a hybrid
7. # multi-threaded multi-process web server
8. # See: http://httpd.apache.org/docs/2.4/mod/worker.html
9. #
10.  #LoadModule mpm_worker_module modules/mod_mpm_worker.so
11.  # event MPM: A variant of the worker MPM with the goal of consuming
12.  # threads only for connections with active processing
13.  # See: http://httpd.apache.org/docs/2.4/mod/event.html
14.  #
15.  LoadModule mpm_event_module modules/mod_mpm_event.so

[/etc/httpd/conf.modules.d/00- mpm.conf | Lines 5 and 15 are modified]

1. <IfModule prefork.c>
2. #
3. # Cause the PHP interpreter to handle files with a .php extension.
4. #
5. <FilesMatch \.php$>
6.     SetHandler application/x-httpd-php
7. </FilesMatch>
8. #
9. # Allow php to handle Multiviews
10.  #
11.  AddType text/html .php
12.  #
13.  # Add index.php to the list of files that will be served as directory
14.  # indexes.
15.  #
16.  DirectoryIndex index.php
17.  #
18.  # Uncomment the following lines to allow PHP to pretty-print .phps
19.  # files as PHP source code:
20.  #
21.  #<FilesMatch \.phps$>
22.  #    SetHandler application/x-httpd-php-source
23.  #</FilesMatch>
24.  #
25.  # Apache specific PHP configuration options
26.  # those can be override in each configured vhost
27.  #
28.  php_value session.save_handler "files"
29.  php_value session.save_path    "/var/lib/php/session"
30.  </IfModule>

[/etc/httpd/conf.d/php.conf | Lines 1 and 30 are modified]

1. (omitted)
2. #
3. # DirectoryIndex: sets the file that Apache will serve if a directory
4. # is requested.
5. #
6. <IfModule dir_module>
7.     DirectoryIndex index.php index.html
8. </IfModule>
9. (omitted)
10.  # Supplemental configuration
11.  #
12.  # Load config files in the "/etc/httpd/conf.d" directory, if any.
13.  IncludeOptional conf.d/*.conf
14.  # gzip setting
15.  AddOutputFilterByType DEFLATE text/html text/plain text/css
16.  AddOutputFilterByType DEFLATE text/javascript application/x-javascript application/javascript
17.  BrowserMatch ^Mozilla/4 gzip-only-text/html
18.  BrowserMatch ^Mozilla/4.0[678] no-gzip
19.  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
20.  # expire setting
21.  ExpiresActive On
22.  ExpiresDefault "access plus 600 seconds"
23.  ExpiresByType text/html "access plus 10 seconds"
24.  # event MPM setting
25.  <IfModule mpm_event_module>
26.          StartServers             2
27.          MinSpareThreads         25
28.          MaxSpareThreads         50
29.          ThreadsPerChild         50
30.          MaxRequestWorkers       50
31.          MaxConnectionsPerChild   0
32.         <FilesMatch \.php$>
33.                  SetHandler "proxy:fcgi://127.0.0.1:9000"34.         </FilesMatch>
35.  </IfModule>

[/etc/httpd/conf/httpd.conf | Lines 7 and 24-35 are modified]

Restart Apache for the changes to take effect.

[root@ip plugins]# systemctl restart httpd

In my environment, page load time was 33ms and requests per second was 60.79, a performance boost of 5%.