Why is the PHP version different in phpinfo() and CLI?

I realized that there is a miss matching between the PHP version reported by phpinfo() and php -v (in the CLI).

phpinfo():  5.5.24
php -v: 5.6.9

I am working on a Mac OS X 10.10 (Yosemite) and installed a library (php-version) to try to manage the PHP versions.

It reports me 3 different versions installed on my laptop:

  5.4.41
  5.5.25
* 5.6.9

I would like to set the same version of PHP in both Apache and CLI.

How can I tell Apache which PHP version use?

I tried to write this line in my httpd.conf:

LoadModule php5_module    /usr/local/opt/php56/libexec/apache2/libphp5.so

Then:

sudo apachectl restart

But all this not solve the problem… In my PHP info I still have: 5.5.24


Solution 1:

A few different issues here all hiding under the concept of PHP version confusion, so will try to address each one as clearly as possible. First this:

I realized that there is a miss matching between the PHP version reported by phpinfo() and php -v (in the CLI).

phpinfo():  5.5.24
php -v: 5.6.9

PHP CLI is not the same as the PHP Apache module.

As I explain in the answer to this other question here, don’t panic! If you are concerned about what PHP version your Apache server is using, the output of phpinfo() is always what you should pay attention to. The Apache PHP module and the PHP command line binary are two different things that don’t interfere with each other.

Just pay attention to the output of phpinfo() if you are concerned about setting the correct PHP module version in Apache.

That said, you still seem to have issues getting the correct PHP module loaded in Apache:

I tried to write this line in my httpd.conf:

LoadModule php5_module    /usr/local/opt/php56/libexec/apache2/libphp5.so

Make sure your Apache server is loading the correct PHP module.

While what you did technically looks to be correct, the only reason I can see for this to not work is somehow there is another LoadModule php5_module directive in the Apache config files that is superseding the value you are setting that line.

I would recommend looking through the httpd.conf—which I assume is located here /etc/apache2/httpd.conf—and see if perhaps there is indeed another LoadModule php5_module that you missed or did not notice when editing that file. Looking at my equivalent file in Mac OS X 10.9.5 I see the line is commented out—since I don’t use Mac OS X Apache/PHP setups—and reads something like this:

#LoadModule php5_module libexec/apache2/libphp5.so

Of course in your case it would be uncommented. More details on configuring Apache and PHP for web development can be found on this site.

Seriously consider using MAMP as an alternative to Mac OS X Apache/PHP quirks.

Now all of that said, I don’t know what you are attempting to do, but if you are doing web development on a Mac OS X system you should seriously consider using MAMP instead of hacking the core Mac OS X web stack to get things running.

The benefit of MAMP is it’s an extremely production level Mac OS X equivalent of a LAMP stack. And since it’s geared towards real-world web development, it has all the modules and configs setup exactly as one should have them setup.

The core problem with Apache and PHP on Mac OS X is the software is typically out of date, a pain to configure/tweak and manage and a headache to debug when stuff like this happens. And what if a Mac OS X update comes along that wipes out your carefully setup Apache and PHP settings? You are back to square one.

Solution 2:

I found the solution to my problem. After editing /etc/apache2/httpd.conf I used to restart apache using:

sudo apachectl restart

For some reason using this command Apache would not fetch the updates in the httpd.conf and load the old config.

Using:

sudo apachectl -k stop
sudo apachectl -k start

Make a kind of hard restart of all the services and also reads the updates in the config file and finally read the new path for the php5_module:

/usr/local/opt/php56/libexec/apache2/libphp5.so

Thanks also @JakeGould for his explanation.