Skip to content

Instantly share code, notes, and snippets.

@Eseperio
Last active April 1, 2022 10:47
Show Gist options
  • Save Eseperio/eba7ce0597f3eb889662c9238ae1cffe to your computer and use it in GitHub Desktop.
Save Eseperio/eba7ce0597f3eb889662c9238ae1cffe to your computer and use it in GitHub Desktop.
Multiple PHP versions at same time on MacOS in 2022 and using Apache

Multiple PHP versions at same time on MacOS and Apache

This is how i achieved to get all PHP versions working at the same time.

I have as many php versions as i want all running at same time. Versions are attached to directories, so in my www directory i have a folder for each php version www/php71/, www/php72/ and so on. If i want to change a project to another version, i only have to move it to a different folder. In MacOs, moving a huge folder is less than 1 second, so this is the fastest way to change version without updating server configuration.

Installing PHP

Install all the php versions via brew command. Example command (replace version number for each one)

In order to be able to install different PHP versions: brew tap shivammathur/php

You may follow this steps before installing shivamanthur/php

After that, you can install as many php versions as you need. brew install [email protected] brew install [email protected] brew install [email protected] ...

After that, find configuration files for each version. They are usually under /opt/homebrew/etc/php/{versionNumber}/php-fpm.d or under /usr/local/etc/php/{versionNumber} Note you have to replace {versionNumber} with the version you wan to edit.

On each folder you will find a file called www.conf or php-fpm.conf. Open in your preferred editor and find the following line: listen = 127.0.0.1:9000

That is the address in which PHP will listen for fastcgi requests. In order to be able to run all at the same time, the address must be different. That is why we are going to change the port to a different number. Instead using 9000. we are going to replace the last two digits with the version number. I.E; for PHP7.2, address will be 127.0.0.1:9072

Starting php processes

PHP must be run as a service to listen for connections. To do this, simply run brew services start [email protected], where X.X is the php version number. You can check status of the services running brew services list

Configuring apache

Once all php services has been started, it is time to configure apache. Open /opt/homebrew/etc/httpd/httpd.conf and at the end of the file add the following code for each version. We are telling Apache to use a different php version based on directory where files are located.

Important Remember to change the version number for each version you want to enable and replace {pathToYourLocalServerRoot} with your www folder path.

<DirectoryMatch "{pathToYourLocalServerRoot}/php71/(.+)?">
	<IfModule proxy_fcgi_module>
	    <FilesMatch ".+\.ph(ar|p|tml)$">
	        SetHandler "proxy:fcgi://127.0.0.1:9071"
	    </FilesMatch>
	</IfModule>
</DirectoryMatch>

Check configuration

Now check if your apache configuration is valid running: apachectl configttest If everything is ok, you´ll see a Syntax OK message.

Apply configuration

Time to restart Apache

Run brew services restart httpd

If apache was not running, run: brew services start httpd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment