Update to php 7.4 in mac
When I am writing this blog, the latest version of PHP available is 7.4. I had PHP 7.1 installed so I need to update it to the latest as my local WordPress installation is telling my PHP is old and need an upgrade. I am planning to make a theme for my WordPress blog so I require a local WordPress setup. That’s why all these PHP and WordPress setups and version upgrade comes to the picture. Let’s see the walkthrough, how I upgraded it, and what problem I faced.
Homebrew is a nice package manager for mac. So I used it for updating PHP. First, let’s update homebrew to get all the latest packages.
brew update && brew upgrade
After a long time (depending upon network speed), it gets updated. I got some permission denied in @ apply2files packages. I tried to run the previous command with sudo but got the message Running Homebrew as root is extremely dangerous and no longer supported. As Homebrew does not drop privileges on installation you would be giving all build scripts full access to your system. So I leave that alone. No further action for updating the homebrew.
When I checked php version using php -v, I am getting the 7.1.33 insatalled in my system. I tried to delete it using brew unlink [email protected]. It doesn’t work. Getting error Error: No such keg: /usr/local/Cellar/[email protected] Then I tried to install php 7.4 using brew install [email protected]. Again it doesn’t work (got error Error: No available formula with the name “[email protected]”). It is actually suggesting to run brew install [email protected].
I tried to tap some brew using brew tap homebrew/dupes, brew tap homebrew/versions, homebrew/homebrew-php but it gives me the error <tap> was deprecated. This tap is now empty and all its contents were either deleted or migrated.
I already got hints from previous command result. Now I tried brew install [email protected] and it works. After some downloads finally php 7.4 version got installed. I tried to uninstall php 7.1 using brew unlink [email protected] but it didn’t worked. Seems I haven’t used brew for installing php 7.1 last time.
I still have php 7.1 in my machine. So my apache is still pointing to 7.1. I need to change it to 7.4. I have opened /etc/apache2/httpd.conf as root. Add LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so. Comment if there is anything exists already.
Some more tips: Folder location of php webserver where from php file can be fetched can be found in httpd.conf under DocumentRoot (e.g. DocumentRoot “/Library/WebServer/Documents”). php.ini file can be found in /usr/local/etc/php/7.4/php.ini
After changing httpd.conf lets restart the apache. Better use below commands.
apachectl -k graceful apachectl -k stop apachectl -k start
Newly installed php can be started by brew services start php
Now create a php file with phpinfo() and open it in browser. You can see the installed php and what php version is apache using.