Different PHP versions per single hosting
Using simple one-liner in .htaccess file you can enforce different PHP version in two or more of your folders. If you have different websites / domains / services registered for each folder, you can as an effect have single hosting running two or more applications that requires different version of PHP parser.
Note that this will most likely work on shared hostings only, where your ISP provides different versions of PHP at your service and some web tool (i.e. cPanel) to switch between them. In most of such cases web tool allows you to pick the same PHP version for the entire hosting. With this solution you can have them different.
However, on your private servers and hostings you have to provide that different versions of PHP parser will be installed and made available.
Case study
For the purpose of example I picked following scenario. Single shared hosting hosts:
- WordPress which (at the moment of writing this) deprecates PHP 7.3 as too old and suggesting using PHP 7.4
- webtrees in old 1.7.x branch which has some functionalities failing on PHP 7.4+
To meet both requirements we enforce (using .htaccess):
- PHP 7.3 for webtrees
- PHP 7.4 for WordPress (or leave the default, if entire hosting is set to that version).
Here is an example:
Solution
.htaccess
As previously mentioned, this is a one-liner.
To enforce PHP 7.3 create a .htaccess file with following content or add this line to already existing file:
AddHandler application/x-httpd-php73 .php .php5
And put this file to a folder where an application requiring specific PHP version is deployed.
Similarly, to enforce PHP 7.4 use:
AddHandler application/x-httpd-php74 .php .php5
And… that’s pretty much all. Here is the source where you can find example entries for other versions.
phpinfo()
The quickest (and the dirtiest) way to check, if this is working on your hosting. Put:
<?php phpinfo(); ?>
to i.e. index.php file and put that file to the same folder.
Summary
So here we have our example folder set:
test73 .htaccess index.php test74 .htaccess index.php
Files contents as above.
And the effect of this on server side:
So that would be pretty much what we have expected.