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: DSO, PHP-FPM, FastCGI, lsphp (1) or suPHP (2). 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.

(2) suPHP is End Of Life already and strongly not recommended for use these days.

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.

DSO (CLI) - PHP as an Apache module

This mode runs PHP scripts as "apache". This means it cannot write to disk unless folders either have 777 permissions or owned by apache. It is recommended to use MOD_RUID2 with this mode so that it works with user permissions.

Even with mod_ruid, it's still more difficult to track down which script belongs to which User.

DSO is also quite quick, has low memory usage since no forking is done, and the same process can be used for all VirtualHosts (no user changes needed). Memory accelerators can be used with this mode. CLI cannot have per-User php.ini files, but can have per-domain settings in the VirtualHosts to enable/disable PHP features. It can also use .htaccess files to enable/disable PHP features. It's difficult to lower cpu usage in this mode because the processes run as apache, so the system can't easily tell which User to slow down (if a limiter is available).

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 DSO

To switch to DSO mode:

cd /usr/local/directadmin/custombuild
./build update
./build set php1_mode mod_php
./build set mod_ruid2 yes
./build set use_hostname_for_alias yes
./build php n
./build rewrite_confs
1
2
3
4
5
6
7

How to switch to FastCGI

To switch to FastCGI mode:

cd /usr/local/directadmin/custombuild
./build update
./build set php1_mode fastcgi
./build set mod_ruid2 no
./build set use_hostname_for_alias yes
./build php n
./build rewrite_confs
1
2
3
4
5
6
7

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 update
./build set php1_mode php-fpm
./build set mod_ruid2 no
./build set htscanner yes
./build mod_htscanner2
./build php n
./build rewrite_confs
1
2
3
4
5
6
7
8

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 update
./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 mod_ruid2 no
./build set htscanner yes
./build mod_htscanner2
./build php n
./build rewrite_confs
1
2
3
4
5
6
7
8
9
10
11

Applying php_value/php_flag from .htaccess . The htscanner

PHP handlers PHP-FPM, FastCGI, and SuPHP 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/suPHP will read and apply php_options from .htaccess files.

To install:

./build set htscanner yes
./build mod_htscanner2
1
2

Now build htscanner for your chosen PHP version as:

./build php_htscanner2 PHP_VERSION
1

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
1

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 update
./build php n
./build rewrite_confs
1
2
3
4
5

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

Last Updated: 6/23/2021, 9:36:08 PM