How to customize PHP version for different directories in cPanel
  John Mwaniki /   14 Sep 2021

How to customize PHP version for different directories in cPanel

I have in the previous articles covered how to check which PHP version your website is running on, and how to change and update PHP version to a current and actively supported one.

For some reason, you may decide to have different sections of your website running on different PHP versions. This could be due to some legacy code in a certain directory within the website, which can only run on older PHP versions.

In this article, I will show you how you can customize your PHP version for different directories in hosting cPanel account.

How to customize PHP version for directories

To achieve that, we will use the .htaccess file, which is a directory-level configuration file for Apache-based web servers.

Follow the steps below:

Step 1: Log in to your cPanel account

Step 2: Click on the "File Manager" under the Files section

Step 3: Navigate to the various directories in File Manager in which you want to customize/change the PHP version.

Inside the directory, create a file and name it ".htaccess". I suggest you first enable the showing of hidden files to make sure none exist before creating.

If there exists a file with that name, don't create another one. Just right-click on it and select the "Edit" option to open it.

Add the lines below to the .htaccess file.

<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml

The above lines set the PHP version for the directory the .htaccess file is placed to version 7.4.

Note that after the first 7, we have not included a dot(.) and after the second 7 we have not added a 4.

To update the version of that directory to 8.0, you do it as below.

<IfModule mime_module>
  AddHandler application/x-httpd-ea-php80 .php .php8 .phtml

The process is simple as that. You can create as many .htaccess files as possible, but each directory/folder can only have one. This way, you can customize your PHP files in multiple directories in your website to run on different PHP versions.