How to set a specific PHP version for different directories
There are multiple cases when you might need to set a specific PHP version for a different directory of a website or application that you are developing.
One example of such an instance could be creating or testing subdomain (such as dev.mydomain.com), where you can deploy any changes or new features in a test environment prior to releasing them on the live version of your site or app.
Another example could be creating a separate mobile version of your website on a subdomain such as mobile.mydomain.com. Additionally, you might also want to switch your blog content to a subdomain like blog.mydomain.com.
Regardless of the intention, you might need to set a different PHP version or configuration to each directory as some versions might offer more useful features for your directory’s intended purpose.
Our shared hosting platform offers this as a possibility, allowing you to set a custom PHP version for each folder or domain and this tutorial we are going to walk you through the necessary steps that you need to take to make that possible. So without further ado, let’s dive right into it!
How to change the default PHP version & options for your whole cPanel account
Every cPanel account hosted with us at VPSBG comes with a default PHP version, which can vary, depending on the date the given account was activated. This means that if you got your account quite some time ago, the default version might not satisfy your needs anymore and you could look to update to a newer version with more features.
On the other hand, you might need to change the PHP version if you would like to tweak or change some PHP options such as ‘memory_limit’ among others.
The process of changing the default PHP version for your entire cPanel account is actually quite simple. All that you need to do is to navigate to the “Software” tab of your cPanel account and click on the ‘Select PHP Version’ button. Once you have done so, a panel similar to the one in the following image should appear:
From here, select the PHP version you would like to use and click on the ‘Set as current’ button. This is the page that also allows you to modify all of the loaded PHP modules.
If you would like to change a given PHP parameter, simply navigate to the ‘Options’ tab and edit all of the options from there.
Remember that any changes that you make will be automatically saved once you have finished editing the settings.
How to set a specific PHP version for a given directory or domain name
As we already mentioned in the beginning of this documentation entry, there might be instances in which you might need to set a custom PHP version for a specific folder, domain or subdomain rather than the entire cPanel account.
To do this, you will need to make use of the .htaccess file that should already be located in the directory that you will be changing the PHP version of. If, however, this file doesn’t exist, you will need to manually create it. To do this simply open up a basic text editor like notepad, add your instructions and save it as ‘.htaccess’ by making sure to select the ‘All files (*.*)’ setting under the ‘Save as type’ setting.
The .htaccess file can be used to define the index page of a given directory as well as to issue redirects and restrict access to certain folders/directories. It can also be utilized to set a specific PHP version for a given folder/domain name.
Simply open the file and add the following line:
AddHandler application/x-httpd-alt-php74 .php
This example shows how to set the PHP version to 7.4. Alternatively, if you would like to use another PHP version, you will need to edit the last two integers to reflect the version you wish to utilize. For example, if you wish to make use of PHP 8.1, you will need to edit the instruction line to the following:
AddHandler application/x-httpd-alt-php81 .php
When you save the .htaccess file into the directory that you wish to change the PHP version of, the PHP version that you specified will be loaded with the default PHP extensions (modules) and options.
If you wish to, however, load any additional PHP modules or perform changes to the existing ones, you will need to go to the cPanel settings and then navigate to ‘Select PHP version’. From there, select the loaded PHP version from the dropdown menu and edit the modules or the options that you want to change.
IMPORTANT: Remember to not click ‘Set as current’ as this will set the selected version as the default PHP version of the whole cPanel account! As we mentioned previously, the changes are saved automatically so that you do not need to press any additional buttons!