The latest macOS version Monterey 12 has removed PHP altogether from its operating system, there is a comment in the Apache config file httpd.conf that says:
#PHP was deprecated in macOS 11 and removed from macOS 12
Fortunately you can install PHP and all the different PHP versions including 5.7, 7.4 and 8 with some help from Homebrew.
Install Homebrew
If you don’t have Homebrew installed, install it, here is a guide on how to install Homebrew.
So once you have Homebrew installed on your macOS a particular PHP Homebrew formulae will need to be added from this maintainer, what is great about this, is all PHP versions are available whereas only some versions are available on the default Homebrew formulas.
After Homebrew install, follow the below:
Add the PHP formulae
brew tap shivammathur/php
Choose the PHP version – this example uses 7.4
brew install shivammathur/php/php@7.4
Other options are
- php@5.6
- php@7.0
- php@7.1
- php@7.2
- php@7.3
- php@7.4
- php@8.0 or known as just php
- php@8.1
Link the PHP Version
brew link --overwrite --force php@7.4
Change your version to match
Restart the Terminal
Run…
php -v
You should now see the new version, to change to another version just repeat the process from the brew install...
then unlink and link in the new PHP version by issuing a command like below but with your correct version:
brew unlink php && brew link --overwrite --force php@8.1
Using new PHP in macOS Monterey shipped Apache
If you are using macOS in built shipped Apache, you can use the PHP but it needs to be Code Signed, before Monterey this practice was optional but now it is mandatory. This guide will help you with Code Signing.
After code signing PHP follow the below to complete loading the PHP module with Apples Apache.
sudo nano /etc/apache2/httpd.conf
Find the ‘Loadmodule’ lines and new PHP version path:
Intel Chip
LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so
Apple Chip
LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so
Restart Apache
sudo apachectl restart
Now the new PHP version will be used.
You could download all the versions and just uncomment the one you want if you intend to swap around (change path prefix depending on Chip used – see above:
#LoadModule php5_module /usr/local/opt/php@5.6/lib/httpd/modules/libphp5.so #LoadModule php7_module /usr/local/opt/php@7.0/lib/httpd/modules/libphp7.so #LoadModule php7_module /usr/local/opt/php@7.1/lib/httpd/modules/libphp7.so #LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so #LoadModule php7_module /usr/local/opt/php@7.3/lib/httpd/modules/libphp7.so LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so #LoadModule php_module /usr/local/opt/php@8.0/lib/httpd/modules/libphp.so #LoadModule php_module /usr/local/opt/php@8.1/lib/httpd/modules/libphp.so
PHP 8 and macOS Apache
One extra step is needed for PHP 8 and macOS bundled Apache:
sudo nano /etc/apache2/httpd.conf
Add the new PHP 8 and comment out the old one.
LoadModule php_module /usr/local/opt/php@8.0/lib/httpd/modules/libphp.so
Go to the end of the file and add:
<FilesMatch .php$> SetHandler application/x-httpd-php </FilesMatch>
Restart Apache
Removing Older Homebrew PHP version (optional)
If you have the older PHP formulas from an older Homebrew installation, you may wish to remove these:
rm -rf $(brew --cellar)/php
Remove old PHP Launch Agents and daemons, if present:
rm -f ~/Library/LaunchAgents/homebrew.mxcl.php* rm -f /Library/LaunchAgents/homebrew.mxcl.php* rm -f /Library/LaunchDaemons/homebrew.mxcl.php*
Remove the deprecated homebrew/php
tap, if present:
brew untap homebrew/php
Run brew cleanup:
brew cleanup
15 comments
Sammy
If you are using macOS, there’s a new option you can try: servbay.dev. It includes all versions of PHP, MariaDB, and PostgreSQL, as well as Redis and Memcached. You can run multiple versions of PHP instances at the same time and it’s easy to switch between them for debugging/testing. With just a few clicks, you can enjoy your PHP development environment.
dragan
There is no LoadModule php7_module … in httpd.conf
I try to put it, but no result. Frustrating.
Chris
This doesn’t work on a M1 Mac unfortunately;
Error: Cannot install in Homebrew on ARM processor in Intel default prefix (/usr/local)!
Please create a new installation in /opt/homebrew using one of the
“Alternative Installs” from:
https://docs.brew.sh/Installation
You can migrate your previously installed formula list with:
brew bundle dump
The ‘Alternative Installs” link just gives instructions similar to yours. (ie round in circles)
Arvind
Hi, Is there any way to install PHP in macOS Monteray without using Homebrew ?
Brian Kawell
Wassup guys. I love CS. What is the PHP config path?
MC
Thank you, thank you, thank you. Covered all the bases, I was frustrated beyond belief with this, which I found this page days ago.
Matt
The linking portion did not work for me, I’m seeing
“`
dyld[27644]: Library not loaded: /usr/local/opt/openldap/lib/libldap.2.dylib
Referenced from: /usr/local/Cellar/php@7.4/7.4.27.reinstall/bin/php
Reason: tried: ‘/usr/local/opt/openldap/lib/libldap.2.dylib’ (no such file), ‘/usr/local/lib/libldap.2.dylib’ (no such file), ‘/usr/lib/libldap.2.dylib’ (no such file), ‘/usr/local/Cellar/openldap/2.5.7/lib/libldap.2.dylib’ (no such file), ‘/usr/local/lib/libldap.2.dylib’ (no such file), ‘/usr/lib/libldap.2.dylib’ (no such file)
Abort trap: 6
“`
Jason
I also got this. Did you find a solution?
ptucky
Thank you for great solution.
B Kay Ess
Thank you so much for this great post. Extremely helpful. Especially codesign post.
Pedro
Hi, thank you for your tutorial. I try to put
“`
LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so
“`
into httpd.conf and show this message in the browser:
This site can’t be reached
localhost refused to connect.
Amir Sarbazi
I had the same problem,
Just add your cert name after libphp7.so
For example:
LoadModule php7_module /usr/…..libphp7.so “Your Name’s CA”
Luke
Nowhere does it mention a “cert name” in the article. And I did add a dev certificate ssl name and it does not work for me either.
Charles
I had the same problem, but “add your cert name after libphp7.so” as you suggested does not work for me. BTW, I have M1 Pro macbook pro which might be the issue? After installing, I find php7.4 gets installed at /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so
which is the location for Intel chip. It’s kind of odd. Maybe this is the problem?
I uninstalled it, and reinstall the php7.4, and this time it throws error:
Error: Cannot install in Homebrew on ARM processor in Intel default prefix (/usr/local)!
Please create a new installation in /opt/homebrew using one of the
“Alternative Installs” from:
https://docs.brew.sh/Installation
You can migrate your previously installed formula list with:
brew bundle dump
I am stuck. Any help would be appreciated!
Charles
I had the same problem, however adding cert name after libphp7.so does not work for me. So I am still stuck. I am using macbook pro M1 Pro version. There is another discrepancy I encountered: Since I have M1 Pro silicon, I would expect the module to be at /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so, however, it is actually at location for Intel Chip: /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so
Maybe I need to run homebrew differently for M1 Pro silicon?
I am still stuck. Any help would be appreciated!
Thanks!