General

Overview

In order to run PHP websites, the server must understand the PHP code and generate a page to be displayed in a browser. It interprets the code based on which PHP library you are using, such as PHP 5 or PHP 7. A PHP handler is what actually loads the libraries so that they can be used for interpretation.

PHP handlers

PHP handlers determine how PHP is loaded on the server.

There are multiple different handlers that can be used for loading PHP: PHP-FPM, FastCGI or LSPHP (1). Each handler delivers the libraries through different files and implementations. Each file and implementation affects Apache's performance, because it determines how Apache serves PHP.

(1) LSPHP is available with OpenLiteSpeed / LiteSpeed webservices, or with the CloudLinux OS over their liblsapi.

It is critical for your server's performance that you select the handler that best suits your situation. Selecting the right handler is just as important as the PHP version itself. One handler is not necessarily always better than another, as it depends on your unique setup. What caching you need, what modules you need, etc.

Each setup has differences which may make it more secure in one case, but less in another.

CGI/FastCGI - as an External CGI module

CGI stands for 'Common Gateway Interface'. The CGI handler will run PHP as a CGI module as opposed to an Apache module. FastCGI (aka: mod_fcgid or FCGI) is a high performance variation of CGI. It has the security/ownership benefits of suPHP in that PHP scripts will run as the actual system user as opposed to 'nobody'. The difference with FastCGI is that it can drastically save on CPU performance and give speeds close to that of DSO. It can also be used with an opcode cacher like OpCache or APC, which can help further speed the loading of pages.

One drawback is that FastCGI has a high memory usage. This is because, rather than creating the PHP process each time it is called, like suPHP, it keeps a persistent session open in the background. This is what lets it work with an opcode caching software.

PHP-FPM - as an External daemon

An alternative PHP FastCGI implementation is PHP-FPM (FastCGI Process Manager). This mode runs PHP via a stand-alone PHP server. Apache accesses this PHP server to make queries. PHP scripts that run via PHP-FPM have many of the same features as with the older PHP handler, suPHP.

PHP-FPM is a much more complex system, but if working correctly, it will give you the absolute fastest performance. Note that memory accelerators can be used with this mode in addition to the benefits of having a separate PHP server (file caching). Memory usage with PHP-FPM can vary drastically depending on how it's set up. At least 1 process is needed per DA User during a request for that User. We use the on-demand mode which only runs the User processes that are needed, which stick around for a while in case other requests come in, but then die after a short period to help lower the memory usage. If all of your sites are very busy, then you may be very short on RAM. The php.ini setup is on a per-User basis in a php-fpm.conf file. PHP-FPM is a much newer technology and has much less testing. Add the extra complexity, and it may not yet be the best choice for a production box. After further testing and proving itself, it may end up being the top choice.

What handler to use

If you're lost with the above information - use the default, which is PHP-FPM.

If you run CloudLinux - always use LSPHP.

--

The next sections will assume that one is switching their PHP engine. We'll use php1_mode for the example. Others (php2/php3/php4_mode) are done same way.

How to switch to FastCGI

To switch to FastCGI mode:

cd /usr/local/directadmin/custombuild
./build set php1_mode fastcgi
./build set use_hostname_for_alias yes
./build php
./build rewrite_confs

The use_hostname_for_alias option is used to redirect the webapps (squirrelmail, roundcube, phpmyadmin) to the hostname of the server because www.domain.com/squirrelmail will run as the User instead of webapps, which won't work correctly (one of the few drawbacks of FastCGI).

How to switch to PHP-FPM

To switch to PHP-FPM mode:

cd /usr/local/directadmin/custombuild
./build set php1_mode php-fpm
./build set_php htscanner yes
./build mod_htscanner2
./build php
./build rewrite_confs

Mixing handlers per versions

You can mix different PHP modes for different versions, but this is not recommended as it leads to increased memory consumption. The mode can be specified for each PHP version installed in the same way that the following commands specify PHP-FPM for each PHP below:

cd /usr/local/directadmin/custombuild
./build set php1_mode php-fpm
./build set php2_mode php-fpm
./build set php3_mode php-fpm
./build set php4_mode php-fpm
./build set_php htscanner yes
./build mod_htscanner2
./build php
./build rewrite_confs

Applying php_value/php_flag from .htaccess . The htscanner

PHP handlers PHP-FPM and FastCGI need a special module to read and apply php_value/php_flag options from the .htaccess file. Only mod_php and lsphp read them natively.

Two modules required:

  1. mod_htscanner2 so that Apache will ignore php_options in the .htaccess instead of not opening the page due to error.

  2. php_htscanner2 so that PHP-FPM/FastCGI will read and apply php_options from .htaccess files.

To install:

./build set_php htscanner yes
./build mod_htscanner2

Now build htscanner for your chosen PHP version as:

./build php_htscanner2 PHP_VERSION

Or build htscanner for all enabled PHP versions using this one-liner:

grep php._release options.conf | cut -d= -f2-2 | grep -v no | awk '{ print "./build php_htscanner2 "$1 }' | sh

I want to have a different version of PHP

If you're looking to change PHP versions in CustomBuild 2.0, say from PHP 5.6 to PHP , you'd type:

cd /usr/local/directadmin/custombuild
./build set php1_release "7.4"
./build php
./build rewrite_confs

You can have a few different versions of PHP running simultaneously.

Last Updated: