Running Laravel on specific PHP version on shared hosting

If you happen to find yourself running a Laravel app on shared hosting, you may need to run the app on a different version of PHP than the account default. This is not best practice, and you should probably upgrade whatever it is that prohibits you from setting the default PHP version to the latest available – but if you do need to force a specific version of PHP to be used, this is what you’re looking for:

For artisan commands you need to specify which binary to use:

/opt/alt/php81/usr/bin/php artisan migrate

The exact path and version to use may differ, and you will have to find out where the binaries are stored. This may vary between providers, but using phpinfo() in an existing webroot will probably give you a hint.

For the webserver you need to specify which version to use in the public/.htaccess file. Add this at the top of the file:

AddHandler application/x-httpd-alt-php81___lsphp .php

Your mileage may vary.