How to use the php that brew installed?

How to use the php that brew installed?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing and Integrating Homebrew PHP Extensions in Apache2 on macOS

When you install a third-party extension like mcrypt via Homebrew on macOS, it is common to encounter a situation where the module loads correctly for CLI scripts but remains invisible to your web server. This happens because Homebrew often installs PHP into a separate directory structure (typically /opt/homebrew/bin/php and /opt/homebrew/etc/php/) that does not automatically align with the PHP binary Apache2 is configured to use.

To resolve this, you must first verify which PHP binary your web server is actually invoking. The most reliable way to check this is by inspecting the php.ini file that Apache is currently loading. You can find the path to this configuration file by running:

php -i | grep 'Loaded Configuration File'

If you are unsure which PHP version or binary Apache is using, examine your Apache configuration files, typically located in /private/etc/apache2/. Look for a LoadModule directive related to PHP (often libphp.so) and trace back where that library file resides. Alternatively, check the error logs in /var/log/apache2/error.log; if Apache fails to load a module, it will usually state whether it was searching in the system directories or failing due to an incompatible architecture.

If you confirm that Apache is using the system PHP (often installed via Xcode Command Line Tools or MacPorts) while mcrypt resides in your Homebrew installation, you have two primary paths forward: either configure Apache to use the Homebrew PHP binary or install the extension into the system PHP directory.

Configuring Apache to Use Homebrew PHP

The most robust solution is to point Apache explicitly to the Homebrew PHP binary and configuration files. This ensures that all future extensions installed via brew install php-* are automatically available without needing to rebuild the extension against a different PHP version.

First, locate your Homebrew PHP executable:

which php
# Output might be: /opt/homebrew/bin/php

Next, identify the corresponding php.ini and module path within the Homebrew installation:

/opt/homebrew/etc/php/7.x/php.ini
/opt/homebrew/lib/php/20190902/libphp.so

(Note: Replace 7.x and 20190902 with the actual version numbers displayed by your Homebrew PHP.)

Edit your Apache configuration file (e.g., /private/etc/apache2/httpd.conf) to load the correct module from the Homebrew location:

LoadFile /opt/homebrew/lib/php/20190902/libphp.so
<IfModule php_module>
    AddHandler application/x-httpd-php .php
    SetEnv APP_ENV production
</IfModule>

You must also ensure that the LoadedConfigurationFile directive in your Apache config points to the Homebrew php.ini:

LoadedConfigurationFile /opt/homebrew/etc/php/7.x/php.ini

After saving the file, restart Apache2:

sudo apachectl restart

Verify the fix by running phpinfo() again. You should now see the mcrypt extension listed alongside other Homebrew-provided modules.

Alternative: Rebuilding System PHP with Homebrew Extensions

If modifying Apache's configuration feels too invasive or if you prefer to keep your system PHP as the primary binary, you can install the missing extension directly into the system PHP directory. This requires knowing the exact prefix of your system PHP installation.

Run:

php-config --prefix
# Example output: /usr/local/Cellar/php/7.4.3

Then install the extension targeting that specific path:

brew install mcrypt --with-php-config=$(php-config)

This command forces Homebrew to compile mcrypt against the system PHP headers and link it into the appropriate directory, ensuring Apache picks it up automatically without changing its binary paths.

Trade-offs and Considerations

Using a separate Homebrew PHP instance for Apache introduces complexity in managing multiple PHP versions. If you later decide to switch from mcrypt to modern alternatives like libsodium, you must ensure that both the old and new extensions do not conflict in your environment. Additionally, some system libraries may be ABI-incompatible between Homebrew and system PHP, leading to runtime errors if mixed improperly.

For production environments involving frameworks like Laravel, consistency is key. If you are already using Composer within a vendor directory managed by Homebrew, mixing system PHP with Homebrew PHP can lead to path resolution issues when autoloading classes or loading service providers. In such cases, aligning your web server to use the same PHP binary as your CLI tools (via Homebrew) simplifies dependency management and reduces debugging time.

By explicitly configuring Apache to load the Homebrew PHP binary or by compiling extensions into the system PHP prefix, you eliminate the discrepancy between CLI and web environments. This approach ensures that phpinfo() reflects an accurate picture of your installed modules and avoids silent failures where features appear missing despite successful compilation.

Our network