First, confirm the path that PHP7.2 was installed at. In most of the cases, it should be /usr/local/opt/php@7.2, but may have some differences, if you’re not sure, go check the .*shrc(* = your shell, e.g. .zshrc, .bashrc) file, it should have a line like PATH="/usr/local/opt/php@7.2/bin.
Second, copy this line(in my case): usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so, according to the thing I’ve mentioned in the first step, you might be needed to change something in this line to fit the situation(like different path name) you’ve met.
Third, edit your Apache config file: sudo vi /etc/apache2/httpd.conf (Also, there might be some differences here, but should not have too much). Then, find the line: LoadModule php7_module libexec/apache2/libphp7.so. This is the original PHP7 module, which is the 7.1.XX one. We need to comment it out by adding a # at the head of this line, make it become #LoadModule php7_module libexec/apache2/libphp7.so.
Finally, we will add a new line LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so, the path should come from the second step.
After all the things are done, we can run sudo apachectl configtest first to make sure everything is OK or not. Then, run sudo apachectl restart, the phpinfo(); should display the correct PHP version you’ve assigned.
However, this may not successfully solve your problem. There are still some problems to be solved: PHP configurations needed to be set, PHP-FHM should be set and run.
So, you can easily compare the two php.ini files(/etc/php.ini and /usr/local/etc/php/7.2/php.ini) through an editor that have a comparison function, to make sure that the new one already set to “normal” status.
Another is to set PHP-FPM, since the built-in PHP already has PHP-FPM function, that means the newer one should also enable this function, otherwise, everything may no go in order. We need to modify the Apache config file again by commenting/uncommenting or adding following lines, you should notice that the ‘php7_module’ is commented out again, and that’s a normal practice.
#LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so
LoadModule proxy_module libexec/apache2/mod_proxy.so
LoadModule proxy_fcgi_module libexec/apache2/mod_proxy_fcgi.so
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
LoadModule rewrite_module libexec/mod_rewrite.so
Include /private/etc/apache2/extra/httpd-vhosts.conf
<FilesMatch .php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
After the modification, remember to restart Apache server. Then, run these commands, or just the last one and restart your Mac, then everything should be as usual:
sudo /usr/local/opt/php@7.2/sbin/php-fpm --fpm-config /usr/local/etc/php/7.2/php-fpm.conf
sudo cp /usr/local/opt/php@7.2/homebrew.mxcl.php@7.2.plist /Library/LaunchDaemons/homebrew.mxcl.php@7.2.plist