Updating to PHP versions 7.4 and 8 on macOS 11 Big Sur and Catalina

The latest macOS versions of Big Sur and Catalina currently ship with PHP 7.3 and have a deprecation notice that they intend to drop support for PHP altogether in a future upgrade.

You can see this by running a <? phpinfo(); ?> function in a webhosted file or running php -v on the command line.

[email protected] Documents % php -v
WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.
PHP 7.3.24-(to be removed in future macOS) (cli) (built: Dec 21 2020 21:33:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.24, Copyright (c) 1998-2018 Zend Technologies

So moving forward the best solution is to use Homebrew to install and then choose whichever version of PHP you want including 7.4 and version 8.

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 – example uses 7.4

brew install shivammathur/php/[email protected]

Other options are

Link the PHP Version

brew link --overwrite --force [email protected]

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 [email protected]

Using new PHP in macOS Shipped Apache

If you are using macOS in built shipped Apache, you can use the new PHP version by editing the Apache file:

sudo nano /etc/apache2/httpd.conf

Find the PHP module and comment it out and add in the new PHP version path:

#LoadModule php7_module libexec/apache2/libphp7.so

LoadModule php7_module /usr/local/opt/[email protected]/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:

#LoadModule php5_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp5.so
#LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so
LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so
#LoadModule php_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp.so
#LoadModule php_module /usr/local/opt/[email protected]/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/p[email protected]/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

13 Comments

  1. Ali on November 6, 2022 at 4:40 pm

    THANK YOU VERY MUCH!
    You saved my day!

    I was surfacing over the Internet around to solve an issue how to combine the Apache2 server with the new PHP8.1.3_1, which I already installed on my machine by using the (brew) command. At the beginning, I had no idea that there is no relationship about the PHP’s version between what I see in the Terminal and (phpinfo.php). But, later on, I found that there is no connection between them – Thanks to stackoverflow.com :-)

    So, I a piece of the issue has been solved, and the biggest solution’s piece I found in this article! About how to update the httpd.conf’s file to accept the latest installed version of PHP.

    Thank you again for making my day :-)

    Best regards.

  2. Daniel on July 12, 2022 at 11:02 am

    your post states “macOS bundled Apache”
    after spending hours searching how to fix the php not working. homebrew states that their php does not work with built in apache, and you must install homebrew apache. maybe update your page?

  3. Daniel on July 12, 2022 at 9:39 am

    the homebrew version of php 7.4 that runs is not working correctly so drupal hangs on updating. ugh, stuck.

  4. Daniel on July 12, 2022 at 4:42 am

    I am on macOS 10.15.7
    after following the instruction to install php8.1, php I get, can’t connect to server in browser, I had php 7.3 installed and working. browser showed a white screen loading any php.
    the only error I got was if I run sudo apachectl -k restart
    httpd: Syntax error on line 188 of /private/etc/apache2/httpd.conf: Cannot load /usr/local/opt/[email protected]/lib/httpd/modules/libphp.so into server: dlopen(/usr/local/opt/[email protected]/lib/httpd/modules/libphp.so, 10): Symbol not found: _sqlite3_enable_load_extension\n Referenced from: /usr/local/opt/[email protected]/lib/httpd/modules/libphp.so\n Expected in: /usr/lib/libsqlite3.dylib\n in /usr/local/opt/[email protected]/lib/httpd/modules/libphp.so

    I installed php7.4 and still get the error when running sudo apachectl -k restart
    but php7.4 does work. I could not find and solution to fixing php8.1.

    how do I get php8.1 to work?

  5. maree on March 20, 2022 at 10:13 am

    if there is problem to start apache from xampp
    ‘use sudo apachectl stop’
    first then start apache from xampp

  6. mickael on September 19, 2021 at 9:46 am

    if you have an apple selicon m1 chip you should change every line with /usr/local/ by /opt/homebrew/.
    like :
    LoadModule php_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp.so

    change to :

    LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so

  7. Gilles Godefroid on July 15, 2021 at 2:03 pm

    First time I found to do that after many tests
    Thanks

  8. Peter Morgan-Tansley on June 30, 2021 at 1:04 pm

    Hi

    I just tried this but get these errors using configtest –

    AH06665: No code signing authority for module at /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so specified in LoadModule directive. Proceeding with loading process, but this will be an error condition in a future version of macOS.
    httpd: Syntax error on line 188 of /private/etc/apache2/httpd.conf: Cannot load /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so into server: dlopen(/usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so, 10): image not found

  9. Maciek on May 10, 2021 at 11:20 am

    Would this work on High Sierra macos server? I recall installing homebrew PHP once but it caused some random errors with Proxy.

    • Neil Gowran on May 12, 2021 at 11:39 pm

      Yes should be fine on High Sierra, just need to make sure for older installs that all Homebrew is up to date and also good practice to run brew doctor

  10. Maria on May 9, 2021 at 4:59 pm

    In my case, when I ran `brew link –overwrite –force [email protected]`, I got the following:

    No such keg: /usr/local/Cellar/php

    I did have shivammathur/php installed and then installed php 8.0 with it, but still got this message. ???

  11. Douglas Hemingway on April 18, 2021 at 1:51 am

    Using this instruction brew tap shivammathur/php caused an error message:

    Warning: Calling `sha256 “digest” => :tag` in a bottle block is deprecated! Use `brew style –fix` on the formula to update the style or use `sha256 tag: “digest”` instead.

  12. R0bertinski on April 14, 2021 at 5:52 am

    In my case I had to edit nano ~/.bash_profile and change these lines:
    export PATH=”/usr/local/opt/[email protected]/bin:$PATH”
    export PATH=”/usr/local/opt/[email protected]/sbin:$PATH”
    To this:
    export PATH=”/usr/local/opt/[email protected]/bin:$PATH”
    export PATH=”/usr/local/opt/[email protected]/sbin:$PATH”

Leave all Comment