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
1) The Apache HTTP Server
Apache 2.4 comes pre-packaged in macOS Monterey 12.4. Check the version to verify
If the version number is proper, start the server by running the command
Next, type //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.
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.
After having Git in your system, go to Homebrew page and copy the command given for installation.
Run the command, and after Homebrew is installed, you can check its version.
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
Now set the compilers to find PHP; do
Link the PHP version.
Now check the PHP version to confirm.
4) Changing the Work Directory
Get back to the login directory typing
Navigate to your home directory (it is named with your user name). In my case, it is dennisgabil
There, create a directory for your web projects, say 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.
Else, if you intend to use the pre-installed one, navigate to /etc/apache2
Create a backup of the httpd.conf file
We need to make some changes to the httpd.conf file. Open the file. To open it with vi editor, type
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.
Also, just so that an index.php file finds preference while loading your PHP projects, find the section
and insert index.php just before index.html
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.
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
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)
Access //localhost. The page should display Hello, World!