Different php version for different folders/sub-domains [shared hosting] [composer] [CLI]

Different php version for different folders/sub-domains [shared hosting] [composer] [CLI]

Are you trying to have different php version for different sub domains or folder? Well, I have collected some information to make it work under shared hosting. Here are what you will be able to do after following this tutorial:

  • You will be able to update php version for different folders/sub-domains.
    • htaccess will do the job for this task.
  • You will be able to run composer to update Laravel or PHP libraries.
    • You might be able to update php version on website updating htaccess but updating it help you to use composer. For example composer update command won’t work due to php version incompatibility.

Follow these steps:

  • Type nano ~/.bashrc and hit enter.
  • Type alias composer81=”/opt/alt/php81/usr/bin/php ~/composer.phar and hit enter.
    • You can use different location for php and composer.
    • I have downloaded latest version of composer in root direction so the location is ~/composer.phar. You can find it in default location as well that is /opt/cpanel/composer/bin/composer.
    • You can add more alias with php version name. For example alias composer74, composer80 and such other. Don’t forget to update ea-php80, ea-php81 and so on.
  • Now type nano ~/your_sub_domain_folder/.htaccess.
  • Paste following codes
AddHandler application/x-httpd-alt-php81___lsphp .php .php4 .phtml
  • Make sure you have selected correct php version. I have enter handler for php version 8.1. Here are notes to be remembered.
    • When you add php version in htaccess file, the web will serve as that php version, but in CLI, php version will be the which you have set from Cpanel->Select PHP Version.
    • Website will work file pretending the system is in php version 8.1 based on your htaccess configuration.
    • And terminal CLI will understand which php version to link and use will be defined based on steps mentioned above. The point regarding .bashrc.

Once you are done with these configuration, you are done changing php configuration based on sub-domains or folders.

Finally you can use composer and new php version like so:

  • composer81 update
  • πŸŽ‰

Let me know if you have any queries.


Leave a Reply