Most of these answers is for php-cli (Command Line Interface). It's for when you use php scripts from command line only. With a
php -v
PHP 8.1.29 (cli) (built: Jun 5 2024 05:51:57) (NTS) Copyright (c) The
PHP Group Zend Engine v4.1.29, Copyright (c) Zend Technologies
with Zend OPcache v8.1.29, Copyright (c), by Zend Technologies
You only get CLI php version. It's not the same as php-FPM. If you want to know what version apache uses there are other ways. You can use in your php file
echo PHP_VERSION;
or function to know more details (including version)
phpinfo();
And load it via browser.
That's why i'm writing this comment. I'm new to mac and was looking for a way to make httpd use other versions.
brew link && brew unlink only changes CLI php versions for me, but not what httpd uses. I don't know if it's a bug or i did something wrong.
Only way i can change php versions httpd uses is to modify my httpd.conf (/opt/homebrew/etc/httpd/httpd.conf) file:
I commented the line:
#LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
Because "/opt/homebrew/opt/php/" is a link to php version that apache uses.
Add some lines and uncomment only the one with php version you need (they have to be installed with "brew install php@version":
LoadModule php7_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp7.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
#LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Finally, check DirectoryIndex includes index.php
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
After that:
brew services restart httpd
And it works. No need no brew link/unlink.
I don't know if it's a best way, but for me was the only one that worked.