Enable Apache event MPM with php-fpm

Apache mod_event php-fpmStarting from version 2.4 Apache offers 3 MPM (Multi-processing Module) we can choose from, depending on your needs.

  • The prefork MPM uses multiple child processes without threading. Each process handles one connection at a time without creating separate threads for each. Without going into too much detail, we can say that you will want to use this MPM only when debugging an application that uses, or if your application needs to deal with, non-thread-safe modules like mod_php.
  • The worker MPM uses several threads per child processes, where each thread handles one connection at a time. This is a good choice for high-traffic servers as it allows more concurrent connections to be handled with less RAM than in the previous case.
  • Finally, the event MPM is the default MPM in most Apache installations for versions 2.4 and above. It is similar to the worker MPM in that it also creates multiple threads per child process but with an advantage: it causes KeepAlive or idle connections (while they remain in that state) to be handled by a single thread, thus freeing up memory that can be allocated to other threads. This MPM is not suitable for use with non-thread-safe modules like mod_php, for which a replacement such a PHP-FPM must be used instead.

To check the MPM used by your Apache installation, run:

output

Check-Apache-MPMTo change mod_mpm_prefork to mod_mpm_event, you will need to edit:

and uncomment the line that loads the desired module like so:

Make sure to comment ...mod_mpm_prefork.so line so only one MPM module will be loaded at the time.

Note: To make the event MPM work in Debian, you may have to install the libapache2-mod-fastcgi package from the non-free repositories.

Additionally, for CentOS you will need php-fpm (along with fcgi and mod_fcgid) whereas in Debian it’s called php5-fpm (along with apache2-mpm-event).

So, install the necessary packages.

For CentOS:

For Debian/Ubuntu:

In /etc/httpd/conf.d/php.conf (CentOS) add a handler for .php files and comment an existing one:

…driving php requests to fcgi pool.

Now we must tell fcgi where to look for site php files. In case you have only one site on your server you can place the following lines somewhere in global section of httpd.conf or place it in /etc/httpd/conf.modules.d/10-php.conf like I did:

If you have multiple vhosts configured, you should place the lines above in every respective vhost section and define the proper path to site’s files. For example:

Now restart the web server and the newly installed php-fpm (or php5-fpm) service.
On RedHat/CentOS:

On Debian/Ubuntu:

If you got an error:

Failed to start apache : Starting httpd: Syntax error on line 31 of /etc/httpd/conf.d/php.conf: Invalid command ‘php_value’, perhaps misspelled or defined by a module not included in the server configuration

open php.conf and edit it as follows:

if you think you may need to back to mod_prefork. Or just comment them or delete if you don’t think you will back to mod_prefork.

Restart Apache and now check it with httpd -V command. you should get something similar to this:

Choose-Apache-MPM-ModuleIn CentOS 7, you will need to make sure that the http and https services are enabled through the firewall, and that the network interface(s) are properly added to the default zone.

Good luck!

Want me to do this for you? Drop me a line: itgalaxyzzz {at} gmail [dot] com