The running PHP version for your hosting account can be set via the pages Select PHP Version or MultiPHP Manager in your cPanel control panel. There are a few differences between them:

  Select PHP Version (CloudLinux PHP Selector) MultiPHP Manager (cPanel)
Security Patches for Old PHP Versions Yes No
Selectable per Domain / Sub-Domain No (account-wide only) Yes
Customisable PHP Extensions & Options Yes No
Available on Cloud/VPS/Dedicated Optionally (requires CloudLinux OS) Yes

The user interface on the "Select PHP Version" page only permits you to set one PHP version per hosting account. However, there is a workaround where you can set a different PHP version for a sub-directory that we'll go over below.

Select a different PHP version for an add-on domain or sub-domain

For add-on domains and sub-domains, it's recommended to select different PHP versions through the MultiPHP Manager interface. This method does not require any manual editing of files, preserves compatibility and allows us to keep track of the versions used in case of PHP upgrades or other maintenance work.

To set a different PHP version for an add-on domain or sub-domain, please navigate in cPanel to "MultiPHP Manager" under the "Software" section. Select the PHP version that you'd like to apply from the "PHP Version" drop-down menu, select the domain or sub-domain for which you'd like to apply the change, then click on the "Apply" button.

If you need to choose other PHP extensions, unfortunately, this is not possible with MultiPHP Manager. The alternative would be to manually set a PHP version via PHP Selector in the respective directory, as described in the next step.

Use a different PHP version in a sub-directory

The only way to set a different PHP version in a sub-directory is by manually editing the .htaccess file. However, please note that this method should only be considered as a work-around and may not always work correctly. It can suddenly stop working, for example if the PHP version is old and eventually gets removed from the server.

Please take note of our PHP Version Lifespan (including Launch Date and End-of-Life) before proceeding further.

Step 1 - Customize your PHP version (via PHP Selector)

First, you will need to assure that your PHP environment is prepared and compatible with your site and scripts. The default settings should be suitable for WordPress and most sites, but you may want to check the system requirements of your site and scripts first, then enable any missing PHP extensions under the Extensions tab and edit any necessary PHP options under the Options tab in cPanel under Software -> Select PHP Version.

There is a separate guide on how to manage PHP versions here: Manage PHP version and configuration for your cPanel hosting account

Step 2 - Set the PHP version

Navigate to the desired sub-directory for which you'd like to set a different PHP version via FTP or using the cPanel File Manager. If you use the cPanel File Manager, make sure that hidden files are shown [Settings -> Show Hidden Files (dotfiles)].

.htaccess file must exist in the sub-directory. If one does not exist, create it first and make sure to add the dot prefix at the beginning of the file name.

You need to add 3 lines of code to the top of the .htaccess file. The following code will set the sub-directory to the defined PHP version:

<FilesMatch "\.ph(p[4-8]?|tml)$">
SetHandler application/x-httpd-alt-phpXX___lsphp
</FilesMatch>

Alternatively, if the above rule does not work correctly, you can try this rule:

<FilesMatch "\.ph(p[4-8]?|tml)$">
AddHandler application/x-httpd-alt-phpXX___lsphp .php .php7 .php8 .phtml
</FilesMatch>

Replace phpXX with the PHP version number that you would like to use. For example, to use PHP 8.1, you would replace phpXX with php81. Please use only versions that are listed in cPanel under Software -> Select PHP Version, otherwise, your PHP scripts and site will stop working.

Warning: If the applied version does not exist, any PHP file would fail to display in the browser and would be downloadable. This can be a major security concern, reason why it's important to apply the correct PHP version. If you are not sure, please create a temporary sub-directory for testing purposes first before applying any .htaccess rules to a production site.

Save the .htaccess file. The rule and the respective PHP version will apply with immediate effect.

Check the running PHP version

To assure that the correct PHP version runs in the sub-directory, please create a random PHP file (e.g. php-version-check.php) and add the following code to it:

<?php
echo 'Current PHP version: ' . phpversion();
?>

Open the URL of this file in your browser and you will see which PHP version is running under that location.

Setting the PHP options

The PHP options for PHP versions available via PHP Selector should be set in cPanel under Software -> Select PHP Version -> Options for each PHP version that you plan to use.

The PHP options for PHP versions available via MultiPHP Selector cannot be defined through the user interface and must be defined manually in the following files instead.

Instructions for EasyApache / MultiPHP and Servers without CloudLinux and PHP Selector

If the server on which your site/account is hosted does not run CloudLinux and PHP Selector, or if you prefer not to use the PHP Selector for some reason, you can use the PHP versions that are installed by EasyApache.

Please follow step 2, as mentioned above, and use the following code instead:

<FilesMatch "\.ph(p[4-8]?|tml)$">
SetHandler application/x-httpd-ea-phpXX___lsphp
</FilesMatch>

Alternatively, if the above rule does not work correctly, you can try this rule instead:

<FilesMatch "\.ph(p[4-8]?|tml)$">
AddHandler application/x-httpd-ea-phpXX___lsphp .php .php7 .php8 .phtml
</FilesMatch>

Replace phpXX with the PHP version number that you would like to use. For example, to use PHP 8.2, you would replace phpXX with php82.

To find out which PHP versions are currently available, please navigate in cPanel to the "MultiPHP Manager" page under the "Software" section. Generally, all PHP versions that are currently under active support (as listed here) should be installed on any of our servers. If you have full access to the server (e.g. on dedicated/virtual servers), you can navigate to WHM -> Software -> EasyApache 4 to see which PHP versions are currently installed.

Warning: If the applied version does not exist, any PHP file would fail to display in the browser and would be downloadable. This can be a major security concern, reason why it's important to apply the correct PHP version. If you are not sure, please create a temporary sub-directory for testing purposes first before applying any .htaccess rules to a production site.

Setting PHP Options for MultiPHP / EasyApache PHP Versions

On servers running Apache in mod_lsapi mode, it is possible to set the PHP options using the .htaccess file, for example:

<IfModule lsapi_module>
   php_flag display_errors Off
   php_value max_execution_time 60
   php_value max_input_time 90
   php_value max_input_vars 1000
   php_value memory_limit 512M
   php_value post_max_size 504M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-phpXX"
   php_value upload_max_filesize 496M
   php_flag zlib.output_compression Off
</IfModule>

For other PHP handlers, please use <IfModule php8_module> instead of <IfModule lsapi_module>, or define the PHP options twice for both cases, in the event of a transfer to a different server environment.

The same PHP options should also be defined in the .user.ini and php.ini files, for example:

display_errors = Off
max_execution_time = 60
max_input_time = 90
max_input_vars = 1000
memory_limit = 512M
post_max_size = 504M
session.gc_maxlifetime = 1440
session.save_path = "/var/cpanel/php/sessions/ea-phpXX"
upload_max_filesize = 496M
zlib.output_compression = Off

Please remember to update all 3 files (.htaccess, .user.ini and php.ini) when updating any PHP options. It is not strictly necessary to define the PHP options in all 3 files, but cPanel will automatically populate these files if the PHP options are defined in a file, but not in the others, which can lead to conflicts and compatibility issues with your scripts if not applied correctly.

On servers running Apache in suPHP mode, setting PHP options in the .htaccess file will cause a "500 Internal Server Error". For this PHP handler, it's necessary to define the PHP options only in the php.ini file. The php.ini file normally applies only in the directory where it's saved (excluding sub-directories). To use a single php.ini file for all sub-directories, you can add the rule suPHP_ConfigPath /home/username/public_html to the .htaccess file, where this path should be changed accordingly with your username and the respective directory.

Troubleshooting

If the site is not compatible with the applied PHP version, or if the PHP options or extensions are misconfigured, you may notice various errors, such as "500 Internal Server Error". Please ensure that your site and scripts are compatible with the applied PHP version, extensions and options. If you haven't done this already, please update the scripts to the latest version.

You can find any errors related to your site and scripts in the "error_log" files under which the site is located and under its sub-directories. You can use the cPanel File Manager to search for "error_log" files.

Should you have any difficulties applying the custom PHP version, please contact our technical support team and mention which PHP version you've applied and to which domain or sub-directory.

DISCLAIMER: The scripts provided in our knowledgebase are for informational purposes only. We do not provide any warranty or support. It is essential to review and modify the scripts to fit your site's specific needs. There may be unforeseen outcomes when adding new script code to an existing website. You should discuss it with your website manager and seek advice from an experienced website developer if you are unsure.

Updated by SP on 26/03/2024

Was this answer helpful? 5 Users Found This Useful (10 Votes)