MacOS – Stuck in upgrading PHP version on localhost Mojave

homebrewmacosmojavePHP

I'm trying to get PHP7.2 working on localhost, but phpinfo() keeps saying 7.1.19 is in place while ~php -v tells me PHP7.2.18 is in place.

I run ~php -v it says:

PHP 7.2.18 (cli) (built: May 2 2019 13:03:01) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.18, Copyright (c) 1999-2018, by Zend Technologies

~echo $PATH

/usr/local/opt/php@7.2/sbin:/usr/local/opt/php@7.2/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/VMware
Fusion.app/Contents/Public:/opt/X11/bin

I installed PHP7.2 via brew in

➜ php@7.2 pwd
/usr/local/opt/php@7.2

I expect te get on localhost PHP 7.2.18 in place.

UPDATE

Followed these instructions from the link mentioned by @miken32 (How to use the php that brew installed?)

Edited httpd.conf in folder /etc/apache2

#LoadModule php7_module libexec/apache2/libphp7.so
LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so

If I uncomment the first line and comment the second line, I can run phpinfo() from localhost, however it says it's PHP 7.1.19 and php -v says 7.2.18

In case I comment the first line and uncomment the second line, localhost won't start and php -v says 7.2.18

I also followed these steps, but no luck either.

$ brew update php // get the latest homebrew php packages
$ brew install php@7.2
$ brew link php@7.2 // create an alias to this keg-only version; see comments output during installation
$ echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.bash_profile // add the alias to your path; see comments output during installation
$ source ~/.bash_profile // reload .bash_profile to use the new settings immediately
$ sudo apachectl restart

Also tried brew link php71 --force but that didn't help either.

So it seems the webserver doesn't start when I switch lines httpd.conf

Any steps I can take the get 7.2.18 going?

UPDATE 2

➜  apache2 sudo apachectl -t  

httpd: Syntax error on line 178 of /private/etc/apache2/httpd.conf:
Cannot load /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so into
server: dlopen(/usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so,
10): no suitable image found. Did
find:\n\t/usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so: code
signature in (/usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so) not
valid for use in process using Library Validation: mapped file has no
cdhash, completely unsigned? Code has to be at least ad-hoc
signed.\n\t/usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so: stat()
failed with errno=22

Line 178 says:

LoadModule php7_module
/usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so

Have to look into it. Somewhere on SO in a older post the suggestion was done if there are any 'strange' characters that might cause the error. Verified it and the only strange one is '@'. However that is Brew's out of the box installation.

Best Answer

You're trying to use the PHP module from HomeBrew with the Apple-provided Apache web server. That is an unsupported configuration on Mojave.

It doesn't work because macOS tries to validate that your software is correctly signed (i.e. not tampered with) - and it fails this check after you have changed which binaries are going to be loaded in. You can fix this by disabling SIP (System Integrity Protection) - but that is a really bad idea, as this would disable the protection on your other programs as well.

The correct way to proceed is to stop using the built-in Apache web server, and instead use a different web server. The easiest transition is to simple use Apache provided by HomeBrew instead.

You can do that by first installing Apache from HomeBrew:

brew install httpd

Then stop macOS from starting up the Apple-provided Apache on boot:

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Make sure that the new php module line you have inserted is now not commented, and the old is - i.e. make it look like this:

#LoadModule php7_module libexec/apache2/libphp7.so
LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so

Then start up the new Apache web server installation:

brew services restart httpd

Depending on your settings you will then be able to connect with your browser to localhost:80 or localhost:8080 to view your site.