Install PHP on macOS Monterey 12.4 and Set up Apache

PHP use to come pre-installed in earlier versions of macOS till macOS Big Sur. There was a line in the httpd.conf file that just needs to be uncommented to load the PHP module. But after the upgrade to macOS Monterey 12, PHP has been removed and somewhere in the httpd.conf file you will find the lines #PHP was deprecated in macOS 11 and removed from macOS 12.

Press + SPACE (Spotlight Search) and bring up the terminal. Start as root

				
					sudo su -
				
			

1) The Apache HTTP Server

Apache 2.4 comes pre-packaged in macOS Monterey 12.4. Check the version to verify

					
						httpd -v
					
				
mac apache version
Apache version

If the version number is proper, start the server by running the command

					
						apachectl start
					
				

Next, type http://localhost in the URL bar of the web browser. It should display the It works! page.

The default It works! page is located inside /Library/WebServer/Documents as the index.html.en file.

mac it works location
The "It works!" Page Location

2) Install Homebrew

Next, you need to install Homebrew. But to install Homebrew, you first need Git. If you have already installed Xcode in your system, you need not install Git. But if not, install it first.

xcode developer tools
Xcode

After having Git in your system, go to Homebrew page and copy the command given for installation.

					
						/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
					
				

Run the command, and after Homebrew is installed, you can check its version.

					
						brew -v
					
				

3) Install PHP

You can choose to install a particular version of PHP, say, version 7.4. You can do so by typing the command

					
						brew install php@7.4
					
				

Now set the compilers to find PHP; do

					
						export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
						export CPPFLAGS="-I/usr/local/opt/php@7.4/include"
					
				

Link the PHP version.

					
						brew link --overwrite --force php@7.4
					
				

Now check the PHP version to confirm.

					
						php -v
					
				

4) Changing the Work Directory

Get back to the login directory typing

					
						cd ~
					
				

Navigate to your home directory (it is named with your user name). In my case, it is dennisgabil

					
						cd /Users/dennisgabil
					
				

There, create a directory for your web projects, say Sites

					
						mkdir Sites
					
				

For easy access later, you can place this newly created Sites directory in the Favorites section of your Mac's Finder. Do Spotlight Search ( + SPACE) on your username and drag and drop Sites to the Finder sidebar.

5) Modifying httpd.conf

If you have installed Apache via Homebrew, navigate to /usr/local/etc/httpd.

					
						cd /usr/local/etc/httpd
					
				

Else, if you intend to use the pre-installed one, navigate to /etc/apache2

					
						cd /etc/apache2
					
				

Create a backup of the httpd.conf file

					
						cp httpd.conf httpd.conf.bak
					
				

We need to make some changes to the httpd.conf file. Open the file. To open it with vi editor, type

					
						vi httpd.conf
					
				

Find the line

Listen 80

and change it to

Listen 127.0.0.1:80

Few lines below, you will find a litany of LoadModule lines. Below them, add the line

LoadModule php7_module /usr/local/lib/httpd/modules/libphp7.so

Then few lines down, find the commented line

#ServerName www.example.com:80

and uncomment it and change it to

ServerName localhost

Next, find the below two lines

DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">

and change them to

DocumentRoot "/Users/dennisgabil/Sites"
<Directory "/Users/dennisgabil/Sites">

(You change /dennisgabil/ in the two paths to your /username/ ). For inserting text in vi, press i. Save the changes (press ESC, then : (SHIFT + :), then wq!)

Lastly, add the below piece of code to the file.

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

Also, just so that an index.php file finds preference while loading your PHP projects, find the section

					
						<IfModule dir_module$>
							DirectoryIndex index.html
						</IfModule>
					
				

and insert index.php just before index.html

					
						<IfModule dir_module$>
							DirectoryIndex index.php index.html
						</IfModule>
					
				

6) Code Signing

Now if your httpd.conf file is pre-installed (not installed via brew), it is possile that you get the error No code signing authority for module at /usr/local/lib/httpd/modules/libphp7.so specified in LoadModule directive..

To avoid that, just append your code signing certificate name to the very line in the httpd.conf file where the PHP shared file is being loaded.

						
							LoadModule php7_module /usr/local/lib/httpd/modules/libphp7.so "Dennis"
						
					

If you do not have or do not know how to create a code signing certificate is macOS, go through the tutorial here.

7) Restart Apache

Now restart the server by running the command

					
						sudo apachectl -k restart
					
				

Now all your web projects can go inside /Users/dennisgabil/Sites. If you have a web project called, say, myproj, inside that directory, you can access it by typing localhost/myproj in the URL bar of your browser and it will display your default index.html (or index.php, keeping in mind that we are learning PHP) file. If you have a Git repository for your project, you can clone it there and work.

Just for the purpose of testing, create an index.php page that displays Hello, World! inside the /Sites directory (do not forget to change /dennisgabil to your /username in the path)

					
						echo '<?php echo "Hello, World!"; ?>' > /Users/dennisgabil/Sites/index.php
					
				

Access http://localhost. The page should display Hello, World!