The following steps will describe how to build PHP from source including PHP's Apache module as it is no longer part of macOS starting with macOS Monterey.
If this is for a development environment, you can simply install PHP with Homebrew using the command brew install php
. This guide is for cases where you need a more portable PHP without heavily dependening on external libraries.
- Install Homebrew
- Install the following Homebrew packages
- libxml2
- libiconv
- apr
- apr-util
- Build Apache, if PHP's Apache module is not required, skip to step 4.
- Download Apache's source from Apache's website
- Decompress the package with the command
tar -zxf /path/to/downloaded_httpd.tar.gz
(Replace the path with the actual path to the downloaded package) - Change folder to the new uncompressed folder (for example
cd httpd-2.4.51
) - Run the configure command (change the options as needed)
./configure --enable-so --with-apr=/usr/local/opt/apr/ --with-apr-util=/usr/local/opt/apr-util/
- Run the command
make -j4
, replace4
with the number of cores you have - Run the command
sudo make install
to install Apache to/usr/local/apache2
or the location specified during configuration (step 3.4)
- Build PHP
- Download PHP' source from PHP's website
- Decompress the package with the command
tar -zxf /path/to/downloaded_php.tar.gz
(Replace the path with the actual path to the downloaded package) - Change folder to the new uncompressed folder (for example
cd php-8.0.11
) - Run the following command to ensure PHP's configuration process can find
libxml2
:
export PKG_CONFIG_PATH=/usr/local/opt/libxml2/lib/pkgconfig
- Run the configure command, if PHP's Apache module is not required, remove the
--with-apxs2
option (change the options as needed)
./configure --with-iconv=/usr/local/opt/libiconv/ --with-apxs2=/usr/local/apache2/bin/apxs
- Run the command
make -j4
, replace4
with the number of cores you have
- If all went well, you have finished building PHP from source
- If you need to use the PHP module in Apache:
- First you will need to codesign the module (location is
libs/libphp.so
) - Edit the file
/etc/apache2/httpd.conf
and add the following lines, replace"Developer ID Certification Authority"
with the name of the CA of the certificate you used to codesign the module:
LoadModule php_module /path/to/php/libs/libphp.so "Developer ID Certification Authority" <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
- If you need to port PHP's Apache module to other Macs without the hassle of building PHP every time, create a package with the following:
- Apache's
httpd.conf
file or a script to add the changes tohttpd.conf
from step 6.2 - The file
libphp.so
copied to the location specified in thehttpd.conf
file - The file
/usr/local/opt/libiconv/lib/libiconv.2.dylib
copied to the exact same path (/usr/local/opt/libiconv/lib/
) on target machines as it is one of PHP's dependency library
- Apache's
- First you will need to codesign the module (location is
- Done