How to Configure Different PHP Versions per Directory in CloudLinux
Many PHP applications have different version or extension requirements. With CloudLinux’s PHP Selector and mod_lsapi, you can run multiple PHP versions side by side within the same account—on a per-directory or per-domain basis. In this guide, you’ll learn:
- What Clients (End Users) Should Do to select and test a PHP version in their own directories.
- What Administrators Should Do to enable and manage PHP Selector globally.
Part 1: What Clients (End Users) Should Do
-
Create or Edit
.htaccessIn the document root (or any subdirectory) where you want a specific PHP version, open or create a file named
.htaccessand add:<FilesMatch "\.(php4|php5|php3|php2|php|phtml)$"> SetHandler application/x-httpd-alt-php80___lsphp </FilesMatch>Replace
php80with the handler matching your desired ALT-PHP version (e.g.php74,php81, etc.). -
Upload and Propagate
Save and upload the
.htaccess. Apache (viamod_lsapi) will automatically use the specified PHP handler for all matching files in that directory and its subdirectories. -
Verify with
phpinfo()Create
info.phpcontaining:<?php phpinfo(); ?>Visit it in your browser to confirm the active PHP version.
-
Document Your Configuration
Maintain a simple record of which folders use which PHP version for easier future troubleshooting.
Part 2: What Administrators Should Do
1. Ensure Prerequisites Are Installed
- ALT-PHP – CloudLinux’s alternate PHP builds.
- CageFS – per-user filesystem isolation.
- mod_lsapi – LiteSpeed’s PHP handler for Apache.
- PHP Selector (
selectorctlCLI tool).
These components are required for per-user and per-directory PHP version control.
2. Install or Verify mod_lsapi
On EasyApache 4 servers, ensure /etc/container/php.handler includes mappings for each ALT-PHP version, for example:
# cat /etc/container/php.handler
application/x-httpd-alt-php44___lsphp /opt/cloudlinux/alt-php44/root/usr/bin/lsphp
application/x-httpd-alt-php51___lsphp /opt/cloudlinux/alt-php51/root/usr/bin/lsphp
application/x-httpd-alt-php52___lsphp /opt/cloudlinux/alt-php52/root/usr/bin/lsphp
application/x-httpd-alt-php53___lsphp /opt/cloudlinux/alt-php53/root/usr/bin/lsphp
application/x-httpd-alt-php54___lsphp /opt/cloudlinux/alt-php54/root/usr/bin/lsphp
application/x-httpd-alt-php55___lsphp /opt/cloudlinux/alt-php55/root/usr/bin/lsphp
application/x-httpd-alt-php56___lsphp /opt/cloudlinux/alt-php56/root/usr/bin/lsphp
application/x-httpd-alt-php70___lsphp /opt/cloudlinux/alt-php70/root/usr/bin/lsphp
application/x-httpd-alt-php71___lsphp /opt/cloudlinux/alt-php71/root/usr/bin/lsphp
application/x-httpd-alt-php72___lsphp /opt/cloudlinux/alt-php72/root/usr/bin/lsphp
application/x-httpd-alt-php73___lsphp /opt/cloudlinux/alt-php73/root/usr/bin/lsphp
application/x-httpd-alt-php74___lsphp /opt/cloudlinux/alt-php74/root/usr/bin/lsphp
application/x-httpd-alt-php80___lsphp /opt/cloudlinux/alt-php80/root/usr/bin/lsphp
application/x-httpd-alt-php81___lsphp /opt/cloudlinux/alt-php81/root/usr/bin/lsphp
application/x-httpd-alt-php82___lsphp /opt/cloudlinux/alt-php82/root/usr/bin/lsphp
application/x-httpd-alt-php83___lsphp /opt/cloudlinux/alt-php83/root/usr/bin/lsphp
application/x-httpd-ea-php44-lsphp /opt/cpanel/ea-php44/root/usr/bin/lsphp
application/x-httpd-ea-php51-lsphp /opt/cpanel/ea-php51/root/usr/bin/lsphp
application/x-httpd-ea-php52-lsphp /opt/cpanel/ea-php52/root/usr/bin/lsphp
application/x-httpd-ea-php53-lsphp /opt/cpanel/ea-php53/root/usr/bin/lsphp
application/x-httpd-ea-php54-lsphp /opt/cpanel/ea-php54/root/usr/bin/lsphp
application/x-httpd-ea-php55-lsphp /opt/cpanel/ea-php55/root/usr/bin/lsphp
application/x-httpd-ea-php56-lsphp /opt/cpanel/ea-php56/root/usr/bin/lsphp
application/x-httpd-ea-php70-lsphp /opt/cpanel/ea-php70/root/usr/bin/lsphp
application/x-httpd-ea-php71-lsphp /opt/cpanel/ea-php71/root/usr/bin/lsphp
application/x-httpd-ea-php72-lsphp /opt/cpanel/ea-php72/root/usr/bin/lsphp
application/x-httpd-ea-php73-lsphp /opt/cpanel/ea-php73/root/usr/bin/lsphp
application/x-httpd-ea-php74-lsphp /opt/cpanel/ea-php74/root/usr/bin/lsphp
application/x-httpd-ea-php80-lsphp /opt/cpanel/ea-php80/root/usr/bin/lsphp
application/x-httpd-ea-php81-lsphp /opt/cpanel/ea-php81/root/usr/bin/lsphp
application/x-httpd-ea-php82-lsphp /opt/cpanel/ea-php82/root/usr/bin/lsphp
application/x-httpd-ea-php83-lsphp /opt/cpanel/ea-php83/root/usr/bin/lsphp
application/x-httpd-ea-php44___lsphp /opt/cpanel/ea-php44/root/usr/bin/lsphp
application/x-httpd-ea-php51___lsphp /opt/cpanel/ea-php51/root/usr/bin/lsphp
application/x-httpd-ea-php52___lsphp /opt/cpanel/ea-php52/root/usr/bin/lsphp
application/x-httpd-ea-php53___lsphp /opt/cpanel/ea-php53/root/usr/bin/lsphp
application/x-httpd-ea-php54___lsphp /opt/cpanel/ea-php54/root/usr/bin/lsphp
application/x-httpd-ea-php55___lsphp /opt/cpanel/ea-php55/root/usr/bin/lsphp
application/x-httpd-ea-php56___lsphp /opt/cpanel/ea-php56/root/usr/bin/lsphp
application/x-httpd-ea-php70___lsphp /opt/cpanel/ea-php70/root/usr/bin/lsphp
application/x-httpd-ea-php71___lsphp /opt/cpanel/ea-php71/root/usr/bin/lsphp
application/x-httpd-ea-php72___lsphp /opt/cpanel/ea-php72/root/usr/bin/lsphp
application/x-httpd-ea-php73___lsphp /opt/cpanel/ea-php73/root/usr/bin/lsphp
application/x-httpd-ea-php74___lsphp /opt/cpanel/ea-php74/root/usr/bin/lsphp
application/x-httpd-ea-php80___lsphp /opt/cpanel/ea-php80/root/usr/bin/lsphp
application/x-httpd-ea-php81___lsphp /opt/cpanel/ea-php81/root/usr/bin/lsphp
application/x-httpd-ea-php82___lsphp /opt/cpanel/ea-php82/root/usr/bin/lsphp
application/x-httpd-ea-php83___lsphp /opt/cpanel/ea-php83/root/usr/bin/lsphp
# …and so on for each installed version
On non-cPanel servers, the handlers may appear as:
application/x-lsphp74 /opt/alt/php74/usr/bin/lsphp
application/x-lsphp80 /opt/alt/php80/usr/bin/lsphp
# …etc.
This file drives the SetHandler directives in users’ .htaccess files.
3. Configure Symlink Rules for PHP Selector
Create or edit the symlinks rules file so that per-user php.d configs are correctly linked:
nano /etc/cl.selector/symlinks.rules
Add:
php.d.location = selector
Save and exit.
4. Apply Symlink Rules
/usr/bin/selectorctl --apply-symlinks-rules
This command creates the necessary symlinks in each user’s CageFS environment, enabling custom PHP modules and INI settings.
5. Common selectorctl Commands
Use these as root to manage PHP versions and extensions globally or per user.
| Action | Command |
|---|---|
| Change default PHP version for all users | selectorctl --change-to-version=5.3 --version=native |
| Set PHP version for a single user | selectorctl --set-user-current=8.1 --user=username |
| Enable extensions for a user | selectorctl --enable-user-extensions=pdo,phar --version=7.4 --user=username |
| Disable extensions for a user | selectorctl --disable-user-extensions=pdo,phar --version=7.4 --user=username |
| Add or replace php.ini options for a user | selectorctl --add-options=log_errors:on,display_errors:on --version=7.4 --user=username |
| Remove custom INI options | selectorctl --delete-options=display_errors --version=7.4 --user=username |
| List users of a PHP version | selectorctl --list-users --version=5.6 |
| Reset user extensions to default | selectorctl --reset-user-extensions --version=5.6 --user=username |
| Apply symlinks after changes | /usr/bin/selectorctl --apply-symlinks-rules |
6. Note on WHM MultiPHP Defaults
If PHP Selector is enabled and a user’s --set-user-current version matches the system default in WHM → MultiPHP Manager, the WHM default may take precedence over the .htaccess handler. To avoid confusion, ensure the WHM “System Default” version differs from any per-directory overrides or instruct clients accordingly.
7. Best Practices
- Test changes on a staging account with a
phpinfo()script. - Document which folders use which PHP versions.
- Maintain updated ALT-PHP packages via your package manager.
- Inform clients how to use their own
.htaccesshandlers.
With these steps, end users can seamlessly control their PHP environment per directory, while administrators maintain a secure, flexible, and fully managed hosting platform.