mod_lsapi: The Fastest PHP Handler for Apache on CloudLinux

Why mod_lsapi beats suPHP, fcgid and PHP-FPM on a shared CloudLinux server, how to switch to it on DirectAdmin and cPanel, and the directives worth tuning.

Published
Reading time
5 min

The PHP handler is the piece of Apache that decides how a PHP process is started, who it runs as, and whether it stays alive for the next request. On a shared server with hundreds of accounts that decision matters more than almost any php.ini value. mod_lsapi is LiteSpeed's SAPI running as an Apache module, and CloudLinux ships its own build free with the licence. This post explains why it is the handler to use on a CloudLinux server, how to switch to it, and what to tune.

The handlers, honestly compared

mod_php runs PHP inside the Apache worker itself. It is fast but every site runs as the Apache user, so one compromised site can read every other site's wp-config.php. On shared hosting it is disqualified before performance comes into it.

suPHP starts a fresh PHP process as the site owner for every request. Isolation is right, speed is not: each request pays for process start-up and there is no opcache, because the process that compiled the code is gone before the next request arrives.

mod_fcgid keeps PHP processes alive per user, which fixes the start-up cost, but its process management is crude. Each user's processes hold their own opcache, idle processes linger, and under a spike it spawns until memory runs out or its own limits kick in.

PHP-FPM is the modern choice on a VPS with a handful of sites. Each site gets a pool with a tuned number of workers and it is quick. On a shared server the same design is the problem: a pool per user means hundreds of pools, each holding idle workers, and the classic CloudLinux PHP Selector cannot hook into it on DirectAdmin at all.

mod_lsapi keeps the good parts of each. Processes run as the user, inside the user's LVE and CageFS. They start on demand and go away when idle, so 400 quiet accounts cost nothing. The LSAPI protocol between Apache and PHP is lighter than FastCGI. Opcache works and persists per user between requests. And PHP Selector works with it, which is why it is the handler CloudLinux assumes.

Switching on DirectAdmin

CustomBuild handles the whole thing once it knows the server is CloudLinux:

bash
cd /usr/local/directadmin/custombuild
./build update
./build set cloudlinux yes
./build set php1_mode lsphp
./build php n
./build rewrite_confs
cagefsctl --force-update

./build php n compiles PHP as lsphp binaries and installs mod_lsapi; rewrite_confs rewrites every virtual host to use the new handler. If you run more than one PHP slot, set php2_mode lsphp and so on to match. Expect a few minutes of building and a brief Apache restart at the end. Sites keep serving throughout apart from that restart. Before you begin, grep -E 'php[1-4]_mode|cloudlinux' /usr/local/directadmin/custombuild/options.conf shows what you are changing from.

Note that this is one of the two settings PHP Selector depends on, so if you are following installing CloudLinux on a DirectAdmin server you will already have done it.

Switching on cPanel

cPanel has no CustomBuild, so CloudLinux provides a tool that patches EasyApache's configuration:

bash
yum install liblsapi liblsapi-devel
switch_mod_lsapi --setup
switch_mod_lsapi --enable-global
switch_mod_lsapi --check

--setup installs the module and its handler definitions, --enable-global makes lsapi the handler for every PHP version, and --check reports whether the configuration is consistent. --enable-domain example.com switches one domain at a time if you would rather test on a single site first, and --build-native-lsphp builds an lsphp binary for the panel's native PHP so users on "native" in PHP Selector also get the benefit.

Checking that it is in use

Three quick tests. Create info.php with <?php phpinfo(); on a site, load it and read Server API: it should say LiteSpeed, not CGI/FastCGI or FPM/FastCGI. Delete the file afterwards. Then on the server:

bash
httpd -M | grep lsapi
ps -eo user,comm | grep lsphp | sort | uniq -c

The first line confirms the module is loaded; the second shows lsphp processes owned by individual users rather than by apache, which is the isolation working. Load a busy site twice and the second request should be visibly faster than the first, which is opcache warming inside the persistent process.

The directives worth tuning

mod_lsapi's defaults suit a shared server and most servers never need changes. When they do, the directives go in Apache's global configuration: on DirectAdmin that is /etc/httpd/conf/extra/httpd-includes.conf, which survives rewrite_confs; on cPanel it is /etc/apache2/conf.d/lsapi.conf.

apache
lsapi_backend_children 20
lsapi_backend_max_reqs 10000
lsapi_backend_max_idle 300

lsapi_backend_children is the most PHP processes one user can have at once. Keep it at or below the account's EP limit, because a request that cannot get a process queues either way and the EP limit is the one that produces a clear 508 error for the reader. lsapi_backend_max_reqs recycles a process after that many requests, which is insurance against a leaking extension. lsapi_backend_max_idle is how long a quiet process lingers in seconds; lower it on a server that is short of memory, raise it on one where the same sites are hit all day. Restart Apache after any change.

CRIU: faster cold starts

The one cost of on-demand processes is the first request after an idle period, when PHP starts and opcache is empty. CloudLinux addresses it with CRIU, which snapshots a warmed-up lsphp process and restores it in a few milliseconds instead of starting fresh:

bash
yum install criu
systemctl enable --now criu

Then add lsapi_criu On to the same configuration file and restart Apache. It is most noticeable on a server with many low-traffic sites, exactly the profile of a reseller server, where each site's first visitor of the hour would otherwise pay the start-up cost.

Going back

On DirectAdmin ./build set php1_mode php-fpm followed by ./build php n and ./build rewrite_confs returns to PHP-FPM, remembering that PHP Selector stops working at that point. On cPanel switch_mod_lsapi --uninstall reverts EasyApache's handler settings. Neither touches site files. In practice we have not needed to on a CloudLinux server: on a VPSPioneer managed VPS converted to CloudLinux, lsphp is the handler we configure by default.

#cloudlinux#apache#php#mod-lsapi#performance

Keep reading

More from CloudLinux

All guides

CloudLinux

MySQL Governor: Stop One Database From Slowing Everyone Down

How CloudLinux MySQL Governor tracks MariaDB load per user and throttles the heavy ones inside their LVE, with install steps, dbtop and dbctl limits explained.

4 min read →

CloudLinux

CloudLinux PHP Selector: A PHP Version and Extensions Per Account

How CloudLinux PHP Selector gives each account its own PHP version, extensions and php.ini values, what it needs on DirectAdmin, and the CLI to manage it.

5 min read →

CloudLinux

What CloudLinux Does for Shared Hosting Stability (and Why It Matters)

How CloudLinux keeps one busy account from slowing a shared server: LVE limits, CageFS, PHP Selector, what a 508 error means and how to stay within limits.

6 min read →