Serving sites with different PHP-version using Laravel-valet

Alexander Dyriavin
3 min readFeb 5, 2021

--

Before starting doing anything from this article, make sure that you already have installed:

Since this article is related to serving sites with different PHP version, I will skip parts with installing Brew, Laravel-valet, and so on.

Let’s assume that you already have installed php7.* version (with all needed packages and extensions), but for some reason, you need to serve app which is using different version neither your installed.

Run this commands step by step:

Install needed PHP-version (brew install php@. → in my case it will be php@7.2).

Once it installed, brew link php@7.2 and then brew will prompt that you need to add env-path to your .zshrc/.bashrc:

echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc

Then you need to apply changes with :

source ~/.zshrc

Now we have locally instaled 2 versions of PHP, in my case php7.4 & php7.2

Next step will be changing the current version of PHP for Valet :

valet use php@7.2

All valet-services will be restarted and you will able to run commands from a terminal ( composer install, etc) for your project with the older version of PHP

Now we need to link our App to valet-sites, in my case app is built with Laravel, so I need to go to the entry point of application and link it to Valet:

cd my-app.loc/public
valet link my-app.loc

That will create folder in valet.

Now we need to secure our website to use SSL certificates and valet will generate Nginx configuration, which is needed to change fastcgi_pass

valet secure my-app.loc

Again valet will restart needed services, and you will be able to make changes in Nginx config for your app.

But for now, we need to make changes in our php.ini configuration.

You need to find your php.ini file, to that just run and cd to your PHP-directory:

php -i | grep php.ini
# output will look like this:
# Configuration File (php.ini) Path => /usr/local/etc/php/7.4
# Loaded Configuration File => /usr/local/etc/php/7.4/php.ini
# cd to your php directory
cd /usr/local/etc/php/*.*/

We are interested in PHP-fpm configuration which is used by Valet for serving sites, probably in most cases it will be located in the same folder as your PHP but in sub-folder called like “PHP-fpm.d”, we need to change valet socket to ours, so cd to PHP-fpm directory and open “valet-fpm. conf” in your favourite IDE, we need to change “listen” directive

# By default it would like this
listen = /Users/**user-name**/.config/valet/valet.sock

We need to change it according to your requirements, in my case I have installed php7.4 ( primary ) and php7.2 (for the temporary project ), currently I am in php7.4 directory, so my settings will be :

listen = /Users/**user-name**/.config/valet/valet74.sock
# Save the file and exit

Now we need to do the same actions but for php7.2 version (in my case ) :

cd /usr/local/etc/php/7.2/php-fpm.d
vim php-fpm.d
# Changing listen to php72.sock
listen = /Users/**user-name**/.config/valet/valet72.sock
# Save and exit

Short explanation :

We need this to valet will proxy the request to different PHP versions, so :

php7.4 → valet74.sock

php7.2 → valet72.sock

php5.6 → valet56.sock and etc

Now we need to update our Nginx configs for apps:

cd ~/.config/valet/Nginx

In this folder, you will find-out Nginx. conf for each of your linked site: Open needed config with any IDE, and find “fastcgi_pass”, by default it will look like this :

fastcgi_pass "unix:/Users/**user-name**/.config/valet/valet.sock";
# Change it to needed version of your php

Now all we need to do it’s just :

rm -f ~/.config/valet/valet.sock && valet restart

And finally, you can run different web-apps with different PHP-version at the same time. If something went wrong and your app doesn’t work — and throws you an error like this :

This package / app requires php 7.2 but your PHP version (7.4) does not satisfy that requirement.

Make sure that your php7.2 is up and running using this command :

brew services list 
# if error /stopeed
brew services restart or start php7.2
rm -f ~/.config/valet/valet.sock && valet restart

If you got any problems to let me know in the comments! Thanks for reading this article!

--

--

Alexander Dyriavin

Open-minded, technology-driven, and highly passionate about computer science software engineer