How to Install PHP (8.1, 7.4, or 5.6) on macOS

PHP is a server-side programming language that stands for HyperText Preprocessor. Although it was first created for web development, it is now also employed as a general-purpose language. Rasmus Lerdorf came up with it in 1994.

The third-party repository that may be added to the system with brew tap contains the PHP versions for macOS. Additionally, it enables the installation and usage of different PHP versions on a single macOS machine.

This guide will assist you in setting up PHP on macOS.

Prerequisites

Before starting the installation of Node.js and NPM using this tutorial you must have the following prerequisites

  • Terminal: You must have Mac Terminal access and a basic understanding of how to use the terminal program. Log in to your Mac and launch Terminal.
  • Homebrew: Homebrew is a well-known package manager for Mac operating systems. It is helpful for installing most open-source applications, such as Node.js. The following command will install Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 

Installing PHP on macOS

The most recent version of macOS Sierra has PHP 5.6, OSX 10.11 El Capitan includes PHP 5.5, and OSX 10.8 Mountain Lion includes PHP version 5.3. The most recent version of PHP 8.1 is now available for download. The instructions below will assist you with installing PHP 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, or 5.6 on your macOS machine.

  • First, we need to add the shivammathur/php tap to homebrew.
brew tap shivammathur/php
  • Installing PHP: Use the below command to install PHP 8.1 on a macOS system. To install a different PHP version just replace 8.1 with the required version.
brew install shivammathur/php/[email protected]
  • Configure PHP 8.1 as default:
brew link --overwrite --force shivammathur/php/[email protected]

Once the installation is finished, restart the shell and check the active PHP version.

php -v 
#Output 
PHP 8.1.6 (cli) (built: May 21 2022 03:46:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.6, Copyright (c), by Zend Technologies

Configure PHP with Apache

Edit the Apache configuration file and a text editor:

vim /usr/local/etc/httpd/httpd.conf 

Add the following snippet to the file:

LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

You can also change the directory index:

    DirectoryIndex index.php index.html

Save the file and close it.

Now restart the Apache service to apply changes:

apachectl restart 

Now create a phpinfo() function file and access it in the browser.

PHP configuration with Apache

Upgrade PHP version

You can upgrade your PHP version to the latest patch release using the brew upgrade command.

For example, to upgrade PHP 8.1:

brew upgrade shivammathur/php/[email protected]

Switch Active PHP Version

If you have installed multiple PHP versions on your mac, you can switch to a new version anytime.

For example, to switch to PHP 7.4:

brew link --overwrite --force shivammathur/php/[email protected] 

Make sure you already have installed the PHP 7.4 version on your system.

Conclusion

You used this method to successfully install PHP on a macOS computer. On a single machine, you may install different PHP versions and switch between them whenever you want.


Notice: ob_end_flush(): failed to send buffer of zlib output compression (1) in /home/u212091429/domains/mydevhelp.com/public_html/wp-includes/functions.php on line 5420

Notice: ob_end_flush(): failed to send buffer of zlib output compression (1) in /home/u212091429/domains/mydevhelp.com/public_html/wp-includes/functions.php on line 5420