Background: After MAC 12, the built-in PHP is removed, and you need to install it yourself.
Preparation: First create your own certificate to sign the libphp.so file of PHP.
Step 1. Create a certificate
1. Open Launchpad—>Others—>Keychain Access, as shown in the figure below
2. Enter the certificate name in the pop-up box, such as: PHPCA, select the certificate type 代码签名
, check "Let me override these defaults", and click Continue.
3. Fill in the certificate information, the number of valid days can be defined by yourself, click to continue
and then enter
the key pair information: key size 2048 bits, algorithm RSA
Key use extension: signature
Use the extended key to use extension: code signature
After that, it has been defaulted to not need to be modified, and finally "created" directly, and it is successful when you see the picture below.
Step 2. Install PHP using Homebrew
brew install php
After the installation is complete, enter on the command line php -v
to view the version of php. I installed version 8.1.8.
Step 3. Sign the libphp.so file
Enter on the command line, [email protected]
modify it to the version you installed, and you can /opt/homebrew/opt/
view it in the directory, mainly to libphp.so
sign the file, PHPCA
and the name of the signature file generated in step 1.
codesign --sign "PHPCA" --force --keychain ~/Library/Keychains/login.keychain-db /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so
After pressing Enter, a pop-up box will appear, just enter your own power-on password, as shown
in the figure below. After completion, the command line will display: /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.so: replacing existing signature
, which means success.
Step 4. Modify the Apache configuration file
sudo vi /etc/apache2/httpd.conf
Add the following line to the configuration file, PHPCA is the name of the certificate generated in the first step.
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so "PHPCA"
Find DirectoryIndex
and modify it to
DirectoryIndex index.php index.html
Then restart apachectl on it
sudo apachectl start
The default PHP project file is in /Library/WebServer/Documents
the directory, so put the self-developed file in this directory, and then open it in the browser http://localhost/testPage/
to see it, or you can change the project path by yourself.
Directory Structure
-— Library
-— WebServer
-— Documents
-— testPage
-— index.php
index.php content:
<?php
print "<h2>PHP 很有趣!</h2>";
print "Hello world!<br>";
print "我要学习 PHP!";
?>