LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » Guest Post / HTML-CSS-PHP-JavaScript / PHP Codes » How To Use Custom PHP Version In A Selected Directory/Folder?

How To Use Custom PHP Version In A Selected Directory/Folder?

How-To-Use-Custom-PHP-Version-In-A-Selected-Directory-Folder
The PHP version is important not only for speed and performance. There are different applications, which sometimes may require specific PHP versions. 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.

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 web server. The web server follows any instructions contained within the file. Once you are in the File Manager, make sure that you have “Show hidden files” enabled.

Recommended For You:
Common Security Vulnerabilities In PHP Frameworks And How To Avoid Them?

To use a custom PHP version in a selected directory or folder, you can follow these steps. Please note that the exact steps might vary depending on your operating system and server setup.

Assuming you have multiple PHP versions installed on your system (let’s say PHP 7.4 and PHP 8.0), and you want to use a custom PHP version (e.g., PHP 7.4) in a specific directory:

Check PHP Versions Installed: Verify that you have the desired PHP versions installed on your system. You can do this by running the following commands in your terminal:

php -v # Check the default PHP version 
php7.4 -v # Check PHP 7.4 version 
php8.0 -v # Check PHP 8.0 version

Create a .htaccess File: In the directory where you want to use the custom PHP version, create a .htaccess file if it doesn’t already exist.

Specify Custom PHP Version: Edit the .htaccess file and add the following lines to specify the PHP version you want to use:

AddHandler application/x-httpd-php7.4 .php # Use PHP 7.4

Replace .php with the appropriate file extension if needed.

Restart Server: After making changes to .htaccess, you might need to restart your web server for the changes to take effect. The way you restart the server depends on your server environment. For example, if you’re using Apache, you might run:

sudo service apache2 restart # Restart Apache

Remember that these steps are just a general guideline. Your server setup might require adjustments or have additional considerations. Also, keep in mind that using a custom PHP version might have compatibility issues with your code or applications, so thorough testing is essential before making this change on a production server.

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:

Recommended For You:
How To Use Live Chat For Better Customer Experience?

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

Available PHP Handlers:

Another way is to use FilesMatch it in your .htaccess file:

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

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.

Additionally, if you’re running a local development environment, you might consider using tools like Docker or using built-in features of development environments like WAMP, MAMP, or XAMPP to manage different PHP versions for different projects. These tools can provide a more isolated and controlled environment for managing PHP versions.

You Like It, Please Share This Recipe With Your Friends Using...

4 Responses to “How To Use Custom PHP Version In A Selected Directory/Folder?”

  1. This article truly sets a new standard for quality content. I’m blown away by the depth of insights and the well-researched information provided. My gratitude goes out to the author for sharing such a gem with us. It’s definitely going to be a reference point for many discussions.

    • EXEIdeas says:

      Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you with more awesome and valuable content from a different mind. Thanks again.

  2. I’m hooked. Each of your articles is helpful. I love your work. Thanks.

    • EXEIdeas says:

      Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you with more awesome and valuable content from a different mind. Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *