PHP has been removed since Monterey
So we're going to install latest PHP using Home brew and edit the configuration files of apache web server so PHP web hosting is supported from local machine.
- If you don't have Homebrew installed , use the below terminal command to set up home brew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Once the brew set up is success , feed the below command in terminal to instal latest PHP
brew install php
- start php using below command
brew services start php
- create sites directory under current user root directory using below command
sudo mkdir ~/Sites
- open apache folder and create username.conf using below terminal command
cd /etc/apache2/users
sudo nano username.conf
- Feed the below text into the file and press Ctrl+o to save
<Directory "/Users/username/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>
- Now let's open httpd.conf using the below command
sudo nano /etc/apache2/httpd.conf
- Uncomment the below lines from the above file
LoadModule authz_core_module libexec/apache2/mod_authz_core.so
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule include_module libexec/apache2/mod_include.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
- Add the following lines into the same above file
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
- Uncomment the below line
Include /private/etc/apache2/extra/httpd-userdir.conf
- Search for AllowOverride None in the file and replace with below text
AllowOverride All
- Save the file by pressing Ctrl+o
- Open one more apache file using below command
sudo nano /etc/apache2/extra/httpd-userdir.conf
- Uncomment the below line and save it
Include /private/etc/apache2/users/*.conf
- Restart the apache using below command
sudo /usr/sbin/apachectl restart
- Check if webserver is running by opening the below url in browser
http://localhost
- To check if PHP is supported , let's create a php file in webserver directory and try acessing it in the browser
sudo touch /Library/WebServer/Documents/index.php
sudo nano /Library/WebServer/Documents/index.php
and enter the below text and save it.
<?php
phpinfo();
?>
- Check if PHP is supported by opening below url in browser
http://localhost/index.php
- If PHP is not signed yet , then you might get an error. Find the exact error message using below command in terminal
apachectl -t -D DUMP_VHOSTS
- Now feed below command to sign the php using terminal
codesign --sign "your-authority-name" --force --keychain ~/Library/Keychains/login.keychain-db /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
- Open the httpd.conf again and change the uncommented php line with below text
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so "your-authority-name"
- Restart the apache again with below command
sudo /usr/sbin/apachectl restart
- Access the PHP file again through browser, you should be able to see a proper php web page now