Installing support for serving PHP content via Apache is pretty easy. Following official tutorials a simple sudo apt-get install php
in terminal will get you started.
When installing PHP via terminal like shown above APT normally will install libapache2-mod-php* aka mod_php as well. The official Apache Wiki claims this configuration to be the oldest and slowest and does not recommend to use it any further.
To better understand why this configuration is outdated we will take a look at request handling and different Multi-Processing Modules (MPMs) in Apache. To serve multiple queries at a time Apache relies on these moduls for processing requests. Running in an unix environment Apache can utilize mpm_prefork, mpm_worker or mpm_event.
mpm_prefork & mod_php
When using mpm_prefork Apache is forking it’s parent process in advance to serve incoming requests. In contrast to more modern MPMs mpm_prefork is not using threads and therefore is preferred when using non thread safe libraries (e.g. mod_php). Each of these forked child processes is including PHP thanks to mod_php so memory usage is quite high. While mpm_prefork works ok for servers handling small amounts of requests response times and memory usage clearly falls behind when compared with mpm_event on high traffic servers.
mpm_event & php-fpm
More modern MPMs like mpm_event utilize threads to process incoming requests. While PHP can be compiled with Thread Safety enabled it isn’t in many packages delivered with distros or additional package sources. You can validate this by verifying the output of <?php phpinfo(); ?>
. To overcome this hurdle Andrei Nigmatulin started to work on a FastCGI process manager and 2004 which has been reworked and is bundled since PHP 5.3.3. Requests are handled by threads within Apache and forwarded to PHP-FPM when required using mod_proxy_fcgi. Static files will be served by Apache directly.
A deeper comparsion between MPMs for unix-like systems can be found at Datadog.
You’re interested in performance and resource usage of different MPMs when running PHP scripts? Read the follow-up!
Illustrations in this post contain icons beautifully crafted by icons8.