Homebrew安装的PHP7不适用于macOS上的Apache

I am trying to upgrade to php7 which I installed via homebrew.

In CLI php -v returns

PHP 7.0.10 (cli) (built: Aug 21 2016 19:14:33) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

But for localhost, firefox pops up a problem loading page, and I can't view a localhost/phpinfo.php; if I swap the module lines out back to php5 it works fine.

Here's a pastebin http://pastebin.com/950yC7wA of my apache2/httpd.conf, I have no idea how to go about fixing this.

  • osx version El Capitan 10.11

I had the same problem and it went away when I commented out this line in /etc/apache2/httpd.conf

LoadModule php5_module libexec/apache2/libphp5.so

Running $ apachectl -t will show any issues with your current configuration. When I ran it I got this:

httpd: Syntax error on line 171 of /private/etc/apache2/httpd.conf: Cannot load /usr/local/opt/php70/libexec/apache2/libphp7.so into server: dlopen(/usr/local/opt/php70/libexec/apache2/libphp7.so, 10): Symbol not found: _ldap_control_find Referenced from: /usr/local/opt/php70/libexec/apache2/libphp7.so Expected in: /System/Library/Frameworks/LDAP.framework/Versions/A/LDAP in /usr/local/opt/php70/libexec/apache2/libphp7.so

I can confirm this workaround posted on github, installing the package without LDAP support will prevent this error:

brew reinstall php70 --without-ldap

While Homebrew is an excellent tool, it is unnecessary at times.

I mention in my posts on Installing Apache, PHP, and MySQL on Mac OS X, OS X comes with Apache and PHP pre-installed. As such, you simply have to configure and enable them.

Unfortunately, as of Mac OS 10.11 (El Capitan), the PHP version is still 5.5. I imagine this will change with 10.12 (Sierra) since PHP 5.5 is EOL. However, it may only be PHP 5.6.

For installing PHP 7, I recommend using PHP OSX. They offer package installs for PHP versions 5.3 to 7.1 (latest). It's available for Mac OS 10.6+ (Snow Leopard).

Since it is a package install, it works out of the box with the default Apache install. You'll only need to update your PATH and modify their PHP ini file. Both of which are outlined in their FAQ.

If you want a more step by step tutorial, check out my recent post on Updating PHP on Mac OS X.

Not a direct solution to your problem, but I've had countless issues with php on OS X. Most recently was trying to get OCI libraries working after upgrading to El Capitan.

I've given up trying to use it now and began using docker for my development. I find it as a far better solution and you can even set up multiple environments on the same code base which is really useful for testing different versions.

Docker is really easy to set up on OS X, and has a huge range of prebuilt containers available in the repository, you just install and set the mount point to your source directory and use the port it gives you to access.

If you are using Apache on OS X Sierra, php7 does not come with the apache module by default anymore.

With the release of macOS Sierra the Apache module is now not built by default. If you want to build it on your system you have to install php with the --with-apache option. See brew options php70 for more details.

Try reinstalling with the flag:

brew reinstall php70 --with-apache

Make sure that:

  • You've installed PHP with support for Apache module (see: brew options php71).

    E.g. --with-httpd22 or --with-httpd24 is included.

  • You've followed the instructions from brew info php71

    ==> Caveats
    To enable PHP in Apache add the following to httpd.conf and restart Apache:
        LoadModule php7_module /usr/local/opt/php71/libexec/apache2/libphp7.so
    
        <FilesMatch .php$>
            SetHandler application/x-httpd-php
        </FilesMatch>
    
    Finally, check DirectoryIndex includes index.php
        DirectoryIndex index.php index.html
    
  • You restarted Apache via sudo apachectl restart.

For step by step tutorial, see: Setup Apache, MySQL and PHP using Homebrew on macOS Sierra.

Troubleshooting

  • When something doesn't work, check the logs in real-time via:

    tail -f /usr/local/var/log/apache2/*error*
    

    Then start/restart the server.

    Note: When finished, hit Control-C to quit tail.

I know it's very old topic but when You update PHP You also need to update Apache confing - for example:

LoadModule php5_module libexec/apache2/libphp5.so

change to:

LoadModule php7_module libexec/apache2/libphp7.so

I discover today a new path to load the differents php libraries in the httpd.conf file (/usr/local/etc/httpd/httpd.conf) :

### OLD VERSION (NOT WORKING NOW)  ###
#LoadModule php5_module    /usr/local/opt/php56/libexec/apache2/libphp5.so
#LoadModule php7_module    /usr/local/opt/php70/libexec/apache2/libphp7.so
#LoadModule php7_module    /usr/local/opt/php71/libexec/apache2/libphp7.so
#LoadModule php7_module    /usr/local/opt/php72/libexec/apache2/libphp7.so

### NEW VERSION ###
#LoadModule php5_module    /usr/local/opt/php56/lib/httpd/modules/libphp7.so
#LoadModule php7_module    /usr/local/opt/php70/lib/httpd/modules/libphp7.so
#LoadModule php7_module    /usr/local/opt/php71/lib/httpd/modules/libphp7.so
LoadModule php7_module    /usr/local/opt/php72/lib/httpd/modules/libphp7.so

For the folks who may experience this issue, make sure you are able to restart apache using "apachectl restart". In my case, I had to first stop all processes of httpd using command "httpd stop" and then start apache using "apachectl start".