DEV Community

Cover image for Serving sites with different PHP-version using Laravel-valet
Alexander Dyriavin
Alexander Dyriavin

Posted on

Serving sites with different PHP-version using Laravel-valet


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, 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
Enter fullscreen mode Exit fullscreen mode

Then you need to apply changes with :

source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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/*.*/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Now all we need to do it's just :

rm -f ~/.config/valet/valet.sock && valet restart
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (12)

Collapse
 
leslieeeee profile image
Leslie

If you are a macOS user, ServBay.dev is worth to try. You don't need to spend some time or couple of days to setup anything. Just download it and you can use it immediately. You can run multiple PHP versions simultaneously and switch between them effortlessly.
Honestly, this tool has greatly simplified my PHP development and is definitely worth trying!

Collapse
 
kontramundo profile image
Heisenberg

Thanks

Collapse
 
ihab_abu_afia profile image
🇵🇸 Ihab Abou Afia 🇸🇦 🇪🇬 🇦🇪

I did ran:
rm -f ~/.config/valet/valet.sock && valet restart

but the 8.1 site worked while the 7.4 give me an error of Gateway.

and I noticed that I have only 1 socket file which is valet84.sock and no valet74.sock create.

Any thoughts?

Collapse
 
istavros profile image
Stavros Ioannidis • Edited

Well you have to start every php you need to run. For example

brew services start php@8.0
brew services start php@7.4 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dani821 profile image
Dani821

i have followed all your steps but i'm facing 502 Bad Gateway Error.

Collapse
 
j_m profile image
J M

are there sockets for alternative php versions created???
~/.config/valet/valet**.sock ???

valet72.sock, valet74.sock or so?

Collapse
 
dani821 profile image
Dani821

i managed to solve the issue. Thanks

Collapse
 
dani821 profile image
Dani821

there is no Nginx.config file present in ~/.config/valet/Nginx
Please upadte the article

Collapse
 
dyriavin profile image
Alexander Dyriavin

Hello!

Did you secured your app with valet secure?

Collapse
 
dani821 profile image
Dani821 • Edited

no i skip that step..
do i really need to secure app to get this working??

Thread Thread
 
dyriavin profile image
Alexander Dyriavin

Yes, it would create Nginx configuration for your app

Collapse
 
dani821 profile image
Dani821

Here is the error image.