Setting a specific PHP version per folder allows you to customize PHP settings for different applications or sections of your website. This is especially useful if you have multiple scripts or CMS installations requiring different PHP versions. Here’s a step-by-step guide to achieve this.
Step 1: Determine Your Hosting Environment
Before proceeding, ensure your hosting provider allows per-folder PHP version settings. This typically requires suPHP or CloudLinux PHP Selector to be enabled on the server.
Step 2: Log in to cPanel
- Open your browser and navigate to your cPanel login page (e.g.,
https://yourdomain.com:2083
). - Enter your username and password, then click Log in.
Step 3: Open File Manager
- Scroll down to the Files section in your cPanel dashboard.
- Click on File Manager.
- Navigate to the folder where you want to set a specific PHP version (e.g.,
/public_html/subfolder
).
Step 4: Edit or Create the .htaccess
File
- Locate the
.htaccess
file in the target folder. If it doesn’t exist:- Click + File in the File Manager toolbar.
- Name the file
.htaccess
and click Create New File.
- Right-click on the
.htaccess
file and select Edit.
Step 5: Add PHP Version Configuration
- In the editor, add the following line to specify the PHP version for the folder:
AddHandler application/x-httpd-alt-php81 .php
- Replace
php81
with the version you want to use (e.g.,php74
for PHP 7.4,php80
for PHP 8.0).
2. Save your changes by clicking Save Changes in the top-right corner.
Step 6: Test the Configuration
- Create a
phpinfo.php
file in the same folder to verify the PHP version:- Add the following code to the file:
phpinfo();
- Save the file and visit
https://yourdomain.com/subfolder/phpinfo.php
in your browser. - Check the PHP version displayed on the page.
2. Delete the phpinfo.php
file after verification to prevent exposing sensitive server information.
Optional: Use a Custom php.ini
File
If your hosting provider supports custom php.ini
files, you can also create or edit a php.ini
file in the folder to define the PHP version and other settings. Contact your hosting provider for guidance on this method.
Tips for Per-Folder PHP Configuration
- Backup Files: Always create a backup of the
.htaccess
file before making changes. - Error Handling: If the folder throws an error after the change, verify the PHP version compatibility and syntax in the
.htaccess
file. - Consistency: Use a consistent PHP version for all related scripts in the folder to avoid conflicts.
By following these steps, you can easily set the PHP version per folder, ensuring compatibility and optimal performance for different sections of your website.