What is mod_php?
Apache has the ability to run PHP as a built in module rather than as a separate process like FPM. This is the mod_php module. There are certain configuration options specific to mod_php and certain limitations. You can read more on the available options for Apache and PHP here:
How do we know mod_php is being used?
As with many other PHP things, phpinfo() page will confirm how your PHP site is being loaded. Check the Server API (or SAPI) near the top. If this says “apache2handler” then PHP is being handled by Apache
But this is small text. Scroll down and you’ll see an entire section of Apache settings. With any other PHP handler, PHP wouldn’t know what web server is in front of it but with mod_php it can see all the Apache settings. We can even see mod_php installed in the “Loaded Modules”
One thing to note is that just because mod_php is loaded doesn’t mean it is being used. The Apache configuration determines how PHP requests are handled. Likewise, just because FPM is installed and the mod_proxy_fcgi module is loaded doesn’t mean that FPM is being used.
You’ll see FPM/FCGI listed as the Server API if it is one of those handlers.
You can also use the php_sapi_name() funciton documented here:
How is it enabled or disabled?
After being installed, mod_php is loaded just like any other Apache module. In the httpd.conf (or an additional .conf file) you would have a line like this:
LoadModule php7_module /usr/lib64/httpd/modules/libphp7.so
But just because mod_php is loaded doesn’t mean it is being used. There also needs to be a handler set for php requests which will look something like this:
<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
To disable mod_php you would need to remove the handler mapping and set some other PHP handler. For FPM this would be a ProxyPass to the port that FPM listens on and would look something like this
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/usr/local/apache2/htdocs/$1
This is a regex that matches .php requests (along with the query string parameters) and passes it to an FPM pool listening on port 9000.
Note that this can be set per VirtualHost which means one site on a server could be runnning as mod_php and another could be running as FPM. They can even be running as different versions of PHP! This is more common on shared hosting and servers managed by Cpanel or Plesk.
When using mod_php, our PHP extension will not work with threaded Apache mpm’s such as event or worker. It must use mpm_prefork. The Apache and PHP development teams both discourage using mod_php with a threaded Apache as it can be unstable:
If a threaded mpm is loaded you will see New Relic loaded in the PHPInfo but it will state that it is disabled:
You can check the loaded Apache modules with apachectl:
[root@roger ~]# /usr/local/apache2/bin/apachectl -M Loaded Modules: core_module ( static ) so_module ( static) http_module ( static ) mpm_event_module (shared) authn_file_module (shared) php7_module (shared)
Note that depending on the setup the mpm may either be a static or shared module. If it is a static module it will require a recompile to change the mpm. Apache can also be compiled with the
--enable-mpms-shared=all flag which will build all available mpms as DSOs which can be loaded in your httpd.conf like other modules to allow you to quickly switch between them like below:
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
With other Apache/PHP handlers there is not this limitation as PHP is run as a separate process so the threaded Apache process does not affect PHP.