Skip to main content

Multiple PHP versions on macOS

Install XCode Command Line Tools

xcode-select --install

Install Homebrew

Follow the instructions here to install Homebrew.

Install PHP

Tap into older versions of php

brew tap shivammathur/php

Now, install all the required versions of PHP

brew install shivammathur/php/php@7.0
brew install shivammathur/php/php@7.1
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
brew install shivammathur/php/php@8.2

Switching PHP version for command line

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

replace 7.4 with the desired PHP version.

Install Apache

Stop the in-built apache for mac

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Install apache from Brew

brew install httpd
brew services start httpd

Go to http://localhost:8080.

Apache configuration

Change the listening port of apache from 8080 to 80. Open /opt/homebrew/etc/httpd/httpd.conf in a text editor.

Find the line:

Listen 8080

and change to:

Listen 80

Switching PHP version for apache

#LoadModule php7_module /opt/homebrew/opt/php@7.0/lib/httpd/modules/libphp7.so
#LoadModule php7_module /opt/homebrew/opt/php@7.1/lib/httpd/modules/libphp7.so
#LoadModule php7_module /opt/homebrew/opt/php@7.2/lib/httpd/modules/libphp7.so
#LoadModule php7_module /opt/homebrew/opt/php@7.3/lib/httpd/modules/libphp7.so
#LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so
#LoadModule php_module /opt/homebrew/opt/php@8.0/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/php@8.1/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/php@8.2/lib/httpd/modules/libphp.so

Uncomment the desired module corresponding the PHP version and restart apache for changes to take effect.

brew serives restart httpd