32

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
3
  • The question here would be, what does the "Problem Loading Page" say? Additionally, have you looked into your apache access & error log? What does it say when you call your page?
    – Björn K
    Sep 21, 2016 at 12:42
  • I will update the question further later tonight! @BjörnK
    – Arthur
    Sep 21, 2016 at 16:36
  • @arthur To clarify, I downloaded your httpd.conf tested against it; my answer was based on your httpd.conf (I put phpinfo() call in the default /Library/WebServer/Documents). I would pay special attention to anything in the includes, such as vhosts.conf, and any .htaccess files that might be getting in the way. Adding those may help the question.
    – HPierce
    Sep 22, 2016 at 1:06

11 Answers 11

30

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.

2
  • I was stuck even after loading libphp7.so. Adding SetHandler application/x-httpd-php got me up and running. Have an upvote. Oct 6, 2017 at 15:20
  • On the Monterey the brew info [email protected] is the golden nugget that contains all instructions. Good one! Sep 28, 2022 at 18:17
15
+25

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.

1
  • 1
    While this was a good answer several years ago, Liip has stopped updating versions and is currently stalled at 7.3. So I had to remove it and now I'm stuck trying to get the default Apache server to mirror the PHP setup I have in Homestead. Very frustrating.
    – secondman
    Oct 1, 2020 at 14:35
7

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
1
  • 1
    The CA part did the trick on Mac Monterrey M1 apple chip. Thanks Ravi
    – Steferson
    Jan 25, 2022 at 15:22
6

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
1
  • 6
    the --with-apache option is deprecated, use --with-httpd24 instead. Dec 13, 2016 at 8:41
5

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
1
  • It has been removed from macOS 12 and up, so this will not work anymore as far as I can tell. Jan 15, 2022 at 22:08
3

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

3
  • Will give it a run tomorrow and award the bounty if it fixes for me, thanks.
    – Arthur
    Sep 19, 2016 at 6:04
  • 1
    I reinstalled with brew without ldap and apachectl -t returns syntax OK but I am still getting the same problem.
    – Arthur
    Sep 19, 2016 at 22:18
  • 2
    @arthur Darn. I'm not sure if I can help any more with the info you've given as this is working nicely on my end. You should check out some error logs- both apache's and php's and edit your question if you find anything interesting.
    – HPierce
    Sep 19, 2016 at 23:51
3

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.

2
  • I actually have been using docker at the moment, it's just annoying for me having to use git with it and it's so frustrating not being able to set up php7 when 55/56 worked perfectly fine.
    – Arthur
    Sep 19, 2016 at 23:56
  • I use git, I check my source code out to a path on my main drive, and then set the mount point in docker container to that folder , all works fine for me Sep 20, 2016 at 0:12
2

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
1

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

1

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.

0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.