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.
https://dev.to/ramsey/using-clion-with-php-src-4me0
- Install Homebrew
- Install the following Homebrew packages
brew install libxml2 libiconv apr apr-util bison re2c python capstone compiledb pkg-config perl httpd
python3 -m pip install compiledb --break-system-packages
echo 'export PATH="/usr/local/opt/bison/bin:$PATH"' >> ~/.zshrc
export PATH="/usr/local/opt/bison/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/bison/lib"
- 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 export ICU_CFLAGS=-I/usr/local/opt/icu4c/include export ICU_LIBS=-L/usr/local/opt/icu4c/lib ./buildconf
- Run the configure command, if PHP's Apache module is not required, remove the
--with-apxs2
option (change the options as needed)
./configure --enable-debug --with-iconv=$(brew --prefix libiconv) --with-curl --enable-pcntl --with-apxs2 \ --enable-sockets --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data \ --with-pdo-mysql --with-capstone --enable-dtrace --with-apxs2 --enable-debug \ --with-zip --enable-shmop --enable-sysvshm --enable-sysvsem --enable-sysvmsg --enable-cgi compiledb make
- Run the command
make -j4
, replace4
with the number of cores you have. - Running
make install
is available, but not reccomened. It will add the so to the httpd.conf file and link the cli executable. It will not unlink any previously linked PHP executables so duplicates may end up existing. Manually linking and adding the.so
to the.conf
is better long term.- Run
ln -s "$(pwd)/libs/libphp.so" /usr/local/lib/httpd/modules/libphp.so
- Run
- 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
/usr/local/etc/httpd/httpd.conf
. Replace/path/to/php/
with the appropriate value.
The path to this file may change based on how you've installed httpd. You can use the following to see what modules are installed, and how/where they are loaded.LoadModule php_module /usr/local/lib/httpd/modules/libphp.so <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
apachectl -t -D DUMP_MODULES apachectl -t -D DUMP_INCLUDES
cp php.ini-development php.ini
- Add the following lines to the top of the new
php.ini
zend_extension=/Users/richardmiles/CLionProjects/xdebug/modules/xdebug.so xdebug.mode = debug xdebug.start_with_request = yes xdebug.client_port = 9000
- First you will need to codesign the module (location is
- install xdebug from source https://readybytes.in/blog/how-to-setup-xdebug-in-macbook
git clone https://github.com/xdebug/xdebug.git
cd xdebug
phpize
./configure --enable-xdebug
sudo make && sudo make install
- Debug with Clion. https://dev.to/ramsey/using-clion-with-php-src-4me0
- if your debugging CGI then get the PID of the running process and "Run" > "Attach to Process"
cat /usr/local/var/run/httpd/httpd.pid
- You should set the executable to
httpd -k restart -DFOREGROUND
make sure to use absolute path.which httpd
or if you are using Brew then lookup the path usingbrew --prefix httpd
. This will cause php to enter break stmts without manually binding to a port. echo "settings set target.process.follow-fork-mode child\nsettings set target.load-cwd-lldbinit true" > ~/.lldbinit
- In your clion run configuration make sure to use arguments
-k restart -X
as-DFORGROUND
is no longer an option.
- Profit.