Almost all hosting control panel software offer multiple PHP versions to serve websites. You can change PHP version per domain using MultiPHP Manager offered by the control panel. However, if the MultiPHP Manager is unavailable then you can set the same using the domain’s .htaccess file.
Popular control panels like WHM/cPanel, Plesk and CentOS Web Panel offer an easy interface to install multiple PHP versions. So first of all, you need to install all desired PHP versions. Just, telling “how to set up multiple PHP versions” is currently out of the scope of this article.
Before we proceed, if you experience trouble running your website after the version change then check the local PHP configuration as well. It might not be in working status. Here is an article specifying the PHP configuration trouble and the solution.
1. PHP Version per Domain using MultiPHP Manager
The inbuilt MultiPHP Manager is the easiest way to set PHP versions for each domain in your hosting account. However, the interface to manage may vary from the control panel to the control panel. For example, if you have full administration on WHM/cPanel then the path is WHM → Home → Software → MultiPHP Manager.
However, most control panels don’t offer this feature in shared and cloud hostings. Luckily, you have another option in that case. You can set PHP Version per domain using .htaccess as well.
2. Change PHP Version for a Domain using .htaccess
You can switch to a different PHP version by specifying code in the .htaccess file. You need to add a handler to change the PHP version rather than the system default. Suppose the default version in your hosting account is PHP 7.0. Further, you wish to change it to 7.3. So open the .htacess file and add the following line to either beginning or at the end there.
1 | AddHandler application/x-httpd-php73 .php |
Similarly, below is the list of handlers for various PHP version supported by hosting control panels.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #PHP 7.3 AddHandler application/x-httpd-php73 .php #PHP 7.2 AddHandler application/x-httpd-php72 .php #PHP 7.1 AddHandler application/x-httpd-php71 .php #PHP 7.0 AddHandler application/x-httpd-php70 .php #PHP 5.6 AddHandler application/x-httpd-php56 .php #PHP 5.5 AddHandler application/x-httpd-php55 .php |
Easy that? Also, you’re not limited to just the domain. You can change PHP version per user account or the directory. Just create a .htacess file directly inside the directory if there is no one. Then add the handler code to set the specific PHP version for all domains/websites hosted inside that.