This guide explains how to install and run several PHP versions at the same time on macOS, using Apache. Each PHP version is assigned to a specific directory, so you can easily switch PHP versions for any project simply by moving it to the relevant folder. This method allows instant version changes without having to update your server configuration every time.
To install different PHP versions, use Homebrew:
-
Add the required tap:
brew tap shivammathur/phpTip: Before installing, it’s recommended to follow these cleanup steps to avoid conflicts with existing PHP installations.
-
Install each PHP version you need, for example:
brew install [email protected] brew install [email protected] brew install [email protected]Repeat for any other versions you wish to use.
Each PHP version comes with its own PHP-FPM configuration. You need to assign a unique port to each version so they can run simultaneously:
-
Locate the configuration files, usually found at:
/opt/homebrew/etc/php/{version}/php-fpm.d/- or
/usr/local/etc/php/{version}/
-
In each version’s directory, open the
www.conforphp-fpm.conffile. Look for the line:listen = 127.0.0.1:9000Change the port to a unique one for each PHP version. For example:
- PHP 7.1:
127.0.0.1:9071 - PHP 7.2:
127.0.0.1:9072 - PHP 7.3:
127.0.0.1:9073
This ensures all PHP-FPM instances can run at the same time without port conflicts.
- PHP 7.1:
To make PHP-FPM listen for requests, start each service:
brew services start [email protected]
brew services start [email protected]
brew services start [email protected]
You can check the status of all services with:
brew services list
Once all PHP-FPM services are running, configure Apache to use the correct PHP version based on the project’s directory.
-
Edit Apache’s configuration file, usually at
/opt/homebrew/etc/httpd/httpd.conf. -
Add a block like the following for each PHP version and directory you want to support:
<DirectoryMatch "{pathToYourLocalServerRoot}/php71/(.+)?"> <IfModule proxy_fcgi_module> <FilesMatch ".+\.ph(ar|p|tml)$"> SetHandler "proxy:fcgi://127.0.0.1:9071" </FilesMatch> </IfModule> </DirectoryMatch>Replace
{pathToYourLocalServerRoot}with the actual path to yourwwwfolder, e.g.,/Users/youruser/www. Repeat this block for each PHP version, changing the directory and port accordingly.
Before restarting Apache, check the configuration:
apachectl configtest
If everything is correct, you should see the message Syntax OK.
Finally, restart Apache to apply the changes:
brew services restart httpd
If Apache was not running, start it with:
brew services start httpd
Summary:
With this setup, each directory (such as www/php71/, www/php72/) will use its assigned PHP version. To switch a project’s PHP version, simply move it to the appropriate folder. This approach is fast and doesn’t require manual server reconfiguration for every change.
Would you like a practical example with real directory paths, or do you need further clarification on any of the steps?
Great guide! Managing multiple PHP versions on macOS can get complicated, but your approach of using directories for each version makes switching between them super fast and efficient. I also use Servbay for managing my projects, which has helped me organize environments across different PHP versions seamlessly. Thanks for the detailed walkthrough!