Skip to content

Instantly share code, notes, and snippets.

@giladdarshan
Last active September 13, 2024 11:12
Show Gist options
  • Save giladdarshan/cc5da8f73c3197027d1f921dc9fe8907 to your computer and use it in GitHub Desktop.
Save giladdarshan/cc5da8f73c3197027d1f921dc9fe8907 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.

  1. Install Homebrew
  2. Install the following Homebrew packages
    • libxml2
    • libiconv
    • apr
    • apr-util
  3. Build Apache, if PHP's Apache module is not required, skip to step 4.
    1. Download Apache's source from Apache's website
    2. 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)
    3. Change folder to the new uncompressed folder (for example cd httpd-2.4.51)
    4. 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/
    
    1. Run the command make -j4, replace 4 with the number of cores you have
    2. Run the command sudo make install to install Apache to /usr/local/apache2 or the location specified during configuration (step 3.4)
  4. 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
    
    1. 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
    
    1. Run the command make -j4, replace 4 with the number of cores you have
  5. If all went well, you have finished building PHP from source
  6. 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 /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>
    
    1. 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:
      1. Apache's httpd.conf file or a script to add the changes to httpd.conf from step 6.2
      2. The file libphp.so copied to the location specified in the httpd.conf file
      3. 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
  7. Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment