Arthur
Arthur

Reputation: 3942

PHP7 installed by Homebrew doesn't work with Apache on macOS

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.

Upvotes: 32

Views: 42194

Answers (11)

Ravi Rajendra
Ravi Rajendra

Reputation: 718

You can add brew php module to MacOS Monterey apache. Follow all the steps properly.

After installing homebrew php (brew install php / brew install [email protected]) and adding the following lines in /etc/apache2/httpd.conf

LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
#LoadModule php7_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp7.so


<FilesMatch \.php$>
        SetHandler application/x-httpd-php
</FilesMatch>

IMPORTANT: You need to test the apache2 config by running the following command. If you don't run this you will never understand why it is not working.

sudo apachectl configtest

If you get an error that no code signing authority like below

[so:error] [pid 69894] AH06665: No code signing authority for module at /.../libphp[7].so specified in LoadModule directive.

You need to follow the tutorial here or https://blog.phusion.nl/2020/12/22/future_of_macos_apache_modules/ to create your local CA file & Certificate and use the below command to sign the libphp.so file. Note: replace Ravi with your certificate name

codesign -s "Ravi" --force --keychain ~/Library/Keychains/login.keychain-db /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
# codesign -s "Ravi" --force --keychain ~/Library/Keychains/login.keychain-db /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp7.so

Now go back to /etc/apache2/httpd.conf and add "Ravi" (your certificate name) at the end of the LoadModule line like this

LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so "Ravi"
#LoadModule php7_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp7.so "Ravi"

After doing it run

sudo apachectl configtest

and you should see at the end (ignore notice line)

Syntax OK

Upvotes: 8

Ajay D
Ajay D

Reputation: 11

I had a similar problem when I upgraded from PHP 5.6 to 7.2.

In httpd.conf comment out LoadModule php5_module /usr/local/php5/libphp5.so and uncomment LoadModule php7_module /usr/local/Cellar/[email protected]/7.2.22_1/lib/httpd/modules/libphp7.so

The real trick that worked for me was when I ran the following command to find out all modules loaded by Apache:

sudo /usr/sbin/httpd -t -D DUMP_MODULES

This was where I found both php7 and php5 were getting loaded. After looking through the files there was an additional conf that had reference to php5.

After removing that reference and restarting apache I was up and running.

Upvotes: 1

kenorb
kenorb

Reputation: 166399

Make sure that:

  • You've followed the instructions from brew info [email protected]:

    ==> Caveats
    To enable PHP in Apache add the following to httpd.conf and restart Apache:
        LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/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.

Upvotes: 32

Naresh Dhiman
Naresh Dhiman

Reputation: 61

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".

Upvotes: 1

Vincent Moulene
Vincent Moulene

Reputation: 1283

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

Upvotes: 2

Wordica
Wordica

Reputation: 2597

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

Upvotes: 0

dmac
dmac

Reputation: 51

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

Upvotes: 5

Flo Schild
Flo Schild

Reputation: 5294

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

Upvotes: 6

Jason McCreary
Jason McCreary

Reputation: 72981

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.

Upvotes: 16

Ben Cummins
Ben Cummins

Reputation: 508

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.

Upvotes: 3

HPierce
HPierce

Reputation: 7409

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\n Referenced from: /usr/local/opt/php70/libexec/apache2/libphp7.so\n Expected in: /System/Library/Frameworks/LDAP.framework/Versions/A/LDAP\n 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

Upvotes: 3

Related Questions