I have a Laravel (5.7.19) app running with apache2 on a redhat7 machine. There are php5.6, php7.1 and php7.4 installed on the machine and somehow apache uses php5.6 as the default and the app doesn't work with php5.6. Is there a way to specify a php version for a Laravel app, such as setting the php version in .env or some other config file of the app? I've googled around and couldn't find an answer. Right now the only way I could make the app to work is to disable php5.6 and then apache would use php7.1 as the default.
-
1Your question is a little confused. Do you want to change the version of PHP used by Apache.... OR ... Do you want to make the App refuse to run on PHP5.6? – RiggsFolly Oct 26 '20 at 13:41
-
i think you what to change php version only in one project is it correct? – Babak Asadzadeh Oct 26 '20 at 13:46
-
Try including php7.1 in your virtualhost confiruation for the laravel app e.g `Include "conf-available/php7.1-fpm.conf"` – Metabolic Oct 26 '20 at 13:47
-
@RiggsFolly thanks. I just hope to specify a php version for the Laravel app. Suppose I have two Laravel apps, one using php5.6, one uses php7.1. How to set the php version for each of the two apps? In such case, I assume the app using php5.6 would work by default, but how to make the app using php7.1 also work? – Shiping Oct 26 '20 at 13:52
-
@Babak, Yes, that's what I just need to do. – Shiping Oct 26 '20 at 13:53
-
@Meta Pakistani thanks. could you point me to some documentation? I checked the apache conf files and no virtual hosts were specified. – Shiping Oct 26 '20 at 14:21
-
your virtualhost will be defined under `/etc/apache/sites-enabled`. Kindly check this answe https://stackoverflow.com/questions/42696856/running-two-php-versions-on-the-same-server which has more details and methods – Metabolic Oct 26 '20 at 14:23
1 Answers
"Is there a way to specify a php version for a Laravel app, such as setting the php version in .env or some other config file of the app?"
No. The PHP interpetor isn't aware of itself. The application's dotenv file is read by the application itself, which means there's already an interpretor doing the job so if it's picked up by php5, there's no way for the interpretor to pass it along to a php7 interpreter.
If you're using Apache (and assuming a single Apache running), you can setup multiple PHP versions under the same Apache server by using the fastcgi module as described here: https://medium.com/@sivaschenko/apache-running-multiple-php-versions-simultaneously-570fc1541580 and then depending on application you can setup virtual hosts using different php versions
THe better way is with nginx + fpm, where you can have different php versions with different fpm instances running on different ports and you can setup server blocks where php requests are routed appropriately.

- 1,059
- 2
- 14
- 26