PHP7 Installed by Homebrew Doesn't Work with Apache on macOS
When you install PHP via Homebrew on macOS, particularly on El Capitan (10.11), you often encounter a disconnect between the CLI interpreter and the web server module. You might run php -v in your terminal and see version 7.x working perfectly, yet accessing localhost/phpinfo.php in a browser results in a failure to load. The most common symptom is that swapping back to PHP 5 resolves the issue immediately, indicating the problem lies strictly within the Apache configuration rather than the PHP build itself.
The root cause usually stems from how Homebrew handles dynamic loading modules versus the default static linking found in system packages or older setups. When you install PHP with brew install php, Homebrew places a shared object file (typically named libphp7.so or similar, depending on the specific version and build flags) in your Homebrew cellar under Cellar/php/.... However, Apache does not automatically know how to load this new module.
In the provided configuration example from the pastebin, the issue is typically found in the <IfModule> blocks within your httpd.conf or a dedicated LoadModule section. If the line loading the PHP 7 module is commented out, or if Apache is attempting to load an outdated path that no longer exists after the upgrade, the server will silently ignore the PHP handler. This explains why you can see PHP 5 working: the configuration for the older version likely still points to a valid file path that Apache can successfully load.
To fix this, you need to ensure the libphp7.so module is explicitly loaded. Locate your main Apache configuration file, often found at /usr/local/etc/apache22/httpd.conf or /etc/apache2/httpd.conf depending on your setup. Look for the section handling PHP processing. You will likely see a line resembling:
LoadModule php7_module libexec/apache2/libphp7.so
If this line is commented out with a #, remove the hash mark to uncomment it. Ensure that the path libexec/apache2/libphp7.so actually exists on your system. If you installed PHP via Homebrew, the module might be located at /usr/local/Cellar/php/.../lib/httpd/modules/libphp7.so. In these cases, you must update the LoadModule directive to reflect the correct absolute path provided by Homebrew.
Another critical detail is the AddHandler and AddType directives. These tell Apache which files should be processed by PHP. If your configuration only contains handlers for PHP 5 (e.g., .php5), you must ensure that .php7, .phtml, or generic .php extensions are mapped to the new PHP handler. A typical pattern looks like this:
AddHandler php7-script .php
AddType application/x-httpd-php .php
Once these lines are corrected, restart Apache using apachectl graceful. Verify the fix by accessing your test script again. If you still encounter issues, check the error logs located in /usr/local/var/log/apache2/error.log for messages regarding "shared library" loading failures or missing dependencies.
It is also worth noting that Homebrew often installs PHP as an NTS (Non-Threading Server) build by default. While modern macOS versions handle this well, older configurations sometimes require the threading server (TS) variant if mixed with other modules. However, for most standard setups on El Capitan, ensuring the LoadModule path is correct and the handler directives are updated resolves the conflict between CLI and web environments without needing to reinstall PHP entirely.