Skip to content

Instantly share code, notes, and snippets.

@RichardTMiles
Forked from giladdarshan/macos_php_build.md
Last active April 23, 2024 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardTMiles/ebf6ce2758bf3f11469d25463092465a to your computer and use it in GitHub Desktop.
Save RichardTMiles/ebf6ce2758bf3f11469d25463092465a to your computer and use it in GitHub Desktop.
Steps to build / compile PHP from source on macOS

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

  1. Install Homebrew
  2. Install the following Homebrew packages
brew install libxml2 libiconv apr apr-util bison re2c python capstone compiledb
curl https://bootstrap.pypa.io/get-pip.py | python3
python -m pip install compiledb
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"
  1. Build PHP
    1. Download PHP' source from PHP's website
    2. 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)
    3. Change folder to the new uncompressed folder (for example cd php-8.0.11)
    4. 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
    
    1. 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 \
            --with-zip --enable-shmop --enable-sysvshm --enable-sysvsem --enable-sysvmsg --enable-cgi
    
    1. Run the command make -j4, replace 4 with the number of cores you have.
    2. 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
  2. If all went well, you have finished building PHP from source
  3. If you need to use the PHP module in Apache:
    1. First you will need to codesign the module (location is libs/libphp.so)
    2. Edit the file /usr/local/etc/httpd/httpd.conf. Replace /path/to/php/ with the appropriate value.
    LoadModule php_module /usr/local/lib/httpd/modules/libphp.so
    <FilesMatch \.php$>
    	SetHandler application/x-httpd-php
    </FilesMatch>
    
    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.
    apachectl -t -D DUMP_MODULES
    apachectl -t -D DUMP_INCLUDES
    
    1. cp php.ini-development php.ini
    2. 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
    
  4. install xdebug from source https://readybytes.in/blog/how-to-setup-xdebug-in-macbook
    1. git clone https://github.com/xdebug/xdebug.git
    2. cd xdebug
    3. phpize
    4. ./configure --enable-xdebug
    5. sudo make && sudo make install
  5. 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 using brew --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.
Screenshot 2024-04-22 at 11 31 28 PM
  1. Profit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment