How to Set Custom PHP Version per Directory

Updated on Feb 1, 2024

The PHP version is important not only for speed and performance. There are different applications, which sometimes may require specific PHP versions. With FastComet, all shared hosting clients can set specific PHP versions for different directories. For example, if you want to run PHP 7.4 on your account, but you have to set PHP 7.2 because of your specific application requirements, you can do that by setting custom rules for each of your folders. In this post, we will explain how to run multiple PHP versions on the very same account.

How to Change the PHP Version for your Account

By default, all our Shared Hosting servers run PHP 7.2 as the native PHP version. With time, scripts may discontinue supporting this version of PHP, and thus clients need to have the option to run a newer PHP version on their accounts. If you just want to change the version of PHP site-wide you can follow the steps of our step-by-step tutorial.

See also our guide on how to change your PHP version via SSH.

Choosing a PHP Version for a Specific Directory

You can use any combination of different PHP versions running simultaneously in any directory on the account. While this is not available using the PHP Selector UI, it is fairly simple to do manually.

To set a different PHP version from the one assigned to your domain on your cPanel account, you need to add a special code to your .htaccess file. The .htaccess file is a hidden file that is read by the webserver. The web server follows any instructions contained within the file. For the purposes of this article, we will explain accessing it through the File Manager on the cPanel of your hosting account.

Navigate to the section Files → File Manager. If you wish to edit .htaccess file for your main domain, navigate to the public_html folder. In case you need to make some changes to an addon domain, go to public_html/ folder.

Once you are in the File Manager, make sure that you have "Show hidden files" enabled:

This way, you will be able to see "dot" files, which are usually hidden by default. The specific file you need in that case is the .htaccess file.

To set a custom PHP version for your desired directory, either select the .htaccess file and click on Edit from the toolbar, or right-click over it and choose the option to Edit the file:

A pop-up window will appear, where once again you need to click on Edit:

Available PHP Handlers

Now that you are inside the .htaccess file, аt the top of the file, insert the PHP handler for the version you would like to use. If there is a handler in the file already, it should be removed or replaced with your new handler. Depending on your desired PHP version for your specific application (folder), add one of the following lines:

For PHP 5.6:

AddHandler application/x-httpd-php56 .php .php5

For PHP 7.0:

AddHandler application/x-httpd-php70 .php .php5

For PHP 7.1:

AddHandler application/x-httpd-php71 .php .php5

For PHP 7.2:

AddHandler application/x-httpd-php72 .php .php5

For PHP 7.3:

AddHandler application/x-httpd-php73 .php .php5

For PHP 7.4:

AddHandler application/x-httpd-php74 .php .php5

For PHP 8.0:

AddHandler application/x-httpd-php80 .php .php5

For PHP 8.1:

AddHandler application/x-httpd-php81 .php .php5

Keep in mind that those lines are only for FastCloud Essential and FastCloud Plus clients. If you are using our FastCloud Extra plan, your addition to the .htaccess file needs to be a bit different.

For customers using our FastCloud Extra packages, please use the following snippet in your .htaccess file:

<FilesMatch ".(php4|php5|php3|php2|php|phtml)$">
SetHandler application/x-lsphp<phpversion>

The actual change is made by switching the SetHandler line with one of these depending on the version (look at the last two digits):

  • SetHandler application/x-lsphp56
  • SetHandler application/x-lsphp70
  • SetHandler application/x-lsphp71
  • SetHandler application/x-lsphp72
  • SetHandler application/x-lsphp73
  • SetHandler application/x-lsphp74
  • SetHandler application/x-lsphp80
  • SetHandler application/x-lsphp81

After you have added your own configuration rules, save your changes and close the .htaccess file. Now you should have your custom PHP version set.

If you have difficulty with the PHP settings of your hosting account, do not hesitate to contact the FastComet technical support team.

We hope you find this article useful. Discover more about FastCloud - the top-rated Hosting Solutions for personal and small business websites in four consecutive years by the HostAdvice Community!

SSD Cloud Hosting

  • Free Domain Transfer
  • 24/7 Technical Support
  • Fast SSD Storage
  • Hack-free Protection
  • Free Script Installation
  • Free Website Transfer
  • Free Cloudflare CDN
  • Immediate Activation
View More