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 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
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected] or known as just php
- [email protected]
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/[email protected]/lib/httpd/modules/libphp.so
Go to the end of the file and add:
SetHandler application/x-httpd-php
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
General-purpose scripting language
//www.php.net/
License: PHP-3.01
Formula JSON API: /api/formula/
Bottle JSON API: /api/bottle/
Formula code:
on GitHub
Bottle [binary package] installation support provided for:
monterey | ✅ |
big sur | ✅ |
catalina | ✅ |
64-bit linux | ✅ |
monterey | ✅ |
big sur | ✅ |
Current versions:
Other versions:
8.0.23 | General-purpose scripting language | |
7.3.33 | General-purpose scripting language | |
7.2.34 | General-purpose scripting language |
Revision: 1
Keg-only
Depends on:
apr | 1.7.0 | Apache Portable Runtime library |
apr-util | 1.6.1 | Companion library to apr, the Apache Portable Runtime library |
argon2 | 20190702 | Password hashing library and CLI utility |
aspell | 0.60.8 | Spell checker with better logic than ispell |
autoconf | 2.71 | Automatic configure script builder |
curl | 7.85.0 | Get a file from an HTTP, HTTPS or FTP server |
freetds | 1.3.13 | Libraries to talk to Microsoft SQL Server and Sybase databases |
gd | 2.3.3 | Graphics library to dynamically manipulate images |
gettext | 0.21 | GNU internationalization [i18n] and localization [l10n] library |
gmp | 6.2.1 | GNU multiple precision arithmetic library |
icu4c | 71.1 | C/C++ and Java libraries for Unicode and globalization |
krb5 | 1.20 | Network authentication protocol |
libpq | 14.5 | Postgres C API library |
libsodium | 1.0.18 | NaCl networking and cryptography library |
libzip | 1.9.2 | C library for reading, creating, and modifying zip archives |
oniguruma | 6.9.8 | Regular expressions library |
openldap | 2.6.3 | Open source suite of directory software |
1.1.1q | Cryptography and SSL/TLS Toolkit | |
pcre2 | 10.40 | Perl compatible regular expressions library with a new API |
sqlite | 3.39.3 | Command-line interface for SQLite |
tidy-html5 | 5.8.0 | Granddaddy of HTML tools, with support for modern standards |
unixodbc | 2.3.11 | ODBC 3 connectivity for UNIX |
Depends on when building from source:
httpd | 2.4.54 | Apache HTTP server |
pkg-config | 0.29.2 | Manage compile and link flags for libraries |
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module $[brew --prefix]/opt//lib/httpd/modules/libphp7.so
SetHandler application/x-httpd-php
Finally, check DirectoryIndex includes index.php
DirectoryIndex index.php index.html
The php.ini and php-fpm.ini file can be found in:
$[brew --prefix]/etc/php/7.4/
Analytics [macOS]:
| 33,665 |
| 33,515 |
| 14 |
| 101,773 |
| 101,313 |
| 505,794 |
| 502,877 |
Analytics [Linux]:
| 225 |
| 223 |
| 7 |
| 630 |
| 621 |
| 2,443 |
| 2,416 |