Multiple PHP version on Mac - Install php and config by brew

0

Install php Multi version

Day by day everything are moving or changing it's not like how I love my wife still the same 😅 . Anyway this content we are talking about how to install and use PHP multi version in the one computer(Mac). 

So why multi PHP version?

It's because a dev will develop multi project and each project are stick with different PHP version.

Installing :

Install only any versions you need.
brew tap shivammathur/php 
brew install shivammathur/php/php@7.2 
brew install shivammathur/php/php@7.3 
brew install shivammathur/php/php@7.4 
brew install shivammathur/php/php@8.0 
brew install shivammathur/php/php@8.1

Tell computer to use one PHP version as default

brew unlink php 
brew link --overwrite --force php@7.4

In case you want to switch to any version you can just update  php@version

After doing this you can test on terminal by type php -v to see if it work

Define port to each version 

Example below I use PHP version 7.4 if you wish to change any version just change it
/etc/php/version/php-fpm.d/www.conf

Intel x86 Chipset
code /usr/local/etc/php/7.4/php-fpm.d/www.conf

Apple Silicon M1 Chipset 

code /opt/homebrew/etc/php/7.4/php-fpm.d/www.conf

Update content below to change your port and port will be used for php-fpm to separate each version

This way you can config your Apache or Nginx to use PHP by 127.0.0.1:port . Totally port is represent of PHP version.

 # default

user = _www

group = _www

listen = 127.0.0.1:9000

# change to

user = {your_username}

group = _www

listen = 127.0.0.1:9074


Restart

brew services restart php@7.4 or your php version
brew services restart nginx

Create shortcut

Example php 8.1

ln -s /usr/local/opt/php@8.1/bin/php /usr/local/bin/php81

Then you can try php81 -v to verify if it work. 
So for project that using php 8.1 you just use php81 instead of php

Good luck!

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

Random Posts