22 March 2021

cPanel servers use a default, or inherited, PHP version. The default version is automatically applied to new cPanel accounts you create. This makes it easy to upgrade the PHP version for all accounts that use the inherited version. You do need to be careful though – upgrading a PHP version can break websites.

The PHP upgrade dilemma

The problem with upgrading the default PHP version is that it’s difficult to be sure whether or not all websites using the old PHP version will work properly under the new version. A minor upgrade from, say, PHP 7.3 to 7.4 is unlikely to cause any problems. However, there are quite a few differences between PHP 5.6 and PHP 7. If a website uses deprecated functions such as mysql_connect() then the website won’t be able to connect to its database.

Of course, the easiest way to avoid upgrade errors is to not upgrade the PHP version. That will cause issues in the long run though. Once the inherited version reaches its End of Life it no longer gets security updates. Your websites also won’t benefit from performance improvements in newer PHP versions.

Looking for deprecated code

To check if a website uses deprecated PHP code you can check the site’s error logs. For instance, if you see entries like these then you need to investigate the warning before changing the PHP version:

[22-Mar-2021 05:00:01 Europe/London] PHP Deprecated:  Methods with the same name as their class will not be constructors in a future version of PHP; MySQL has a deprecated constructor in /home/example/public_html/includes/MySQL.class.php on line 34

A quick way to assess how many websites on your server use deprecated PHP code is by grepping error logs for the string “deprecated”. For instance, the below command looks for the strings “Mar-2021” and “deprecated” in any public_html directory. It lists any error logs containing the strings.

# find /home/*/public_html/ \
-maxdepth 1 \
-type f \
-name error_log \
-exec grep -il "Mar-2021.*deprecated" {} \+

Changing the inherited PHP version

Changing the default PHP version is as easy as selecting the new version from the PHP Version drop-down menu. To change the version for multiple domains you can select the domain on the User Domain Settings tab and then select the new version from the Set PHP Version select menu.

Using whmapi1

You can also use cPanel’s whmapi1 command line utility. For instance, this command gives you a (neatly formatted) list with virtual hosts using the inherited PHP version:

# whmapi1 php_get_vhosts_by_version version=inherit | grep " - " | awk '{ print $NF }' | sort

If you want to change the PHP version for all virtual hosts that use a specific version then you can use the below commands. The first command writes the list with hosts to a file (domains.txt). The second command loops through the file and changes the PHP version to ea-php74:

# whmapi1 php_get_vhosts_by_version version=ea-php73 | grep " - " | awk '{ print $NF }' | sort > domains.txt
# while read -r domain; do whmapi1 php_set_vhost_versions version=ea-php74 vhost="$domain"; done < domains.txt

Inheritance errors

When you change the PHP version cPanel adds an AddHandler directive to the first .htaccess file it finds in the domain’s root directory. The rule looks like this:

# php -- BEGIN cPanel-generated handler, do not edit
# Set the "ea-php74" package as the default "PHP" programming language.

  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml

# php -- END cPanel-generated handler, do not edit

Changing the PHP version will fail if a virtual host doesn’t have a .htaccess file. In that case the virtual host uses the default PHP version. If that happens, create an empty .htaccess file via cPanel’s file manager or from the command line:

# touch /path/to/document_root/.htaccess

And finally, you can manually add the AddHandler directive to a .htaccess file. This is not recommended but it can be useful if you have a custom configuration. If you manually add the directive then you should also update the /var/cpanel/userdata/$USER/$DOMAIN file. This file is used to display the PHP version in cPanel. If you don’t update the file the two will be out of sync.