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

29

3

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

Salvatore Dibenedetto

Posted 2015-09-11T22:45:51.747

Reputation: 431

1

@bertieb Question is indeed similar to that other one. I just answered it, but there is specific issue here I will attempt to answer as well.

– JakeGould – 2015-09-12T00:42:29.247

2

Possible duplicate of phpinfo() and php -v shows different version of PHP

– Pmpr – 2017-03-03T14:54:29.943

Answers

15

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.

JakeGould

Posted 2015-09-11T22:45:51.747

Reputation: 38 217

Why did you answer this Duplicate instead of marking the question as a duplicate?

– Pmpr – 2017-03-03T14:55:18.743

@Trix Because that “duplicate” is a similar answer to a different question posted within days of each other. As I answer to a similar comment here, “@bertieb Question is indeed similar to that other one. I just answered it, but there is specific issue here I will attempt to answer as well. – JakeGould Sep 12 '15 at 0:42” One question is about the difference between PHP from the command line. The other is about that AND PHP module issues.

– JakeGould – 2017-03-03T16:38:27.887

4

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.

Salvatore Dibenedetto

Posted 2015-09-11T22:45:51.747

Reputation: 431

Edited the post to remove the -k flag from the apachectl commands since if you check apachectl -h that flag/option does not exist there. No idea where you got that from. Also, while you state doing stop and start cleared things up, the odd thing is that restart on Linux systems does exactly that. If you check the script located in /usr/sbin/apachectl (it’s a script so use any text editor to view) it seems to do exactly that as well. Maybe on Mac OS X the LaunchDaemons for Apache is just “weird” when compared to standard Linux? Anyway, great you solved this. – JakeGould – 2015-09-12T15:13:23.100

Just for your info i found the command with the -k flag here: http://httpd.apache.org/docs/2.2/stopping.html

– Salvatore Dibenedetto – 2015-09-13T08:51:26.720

You are correct. I looked through the “help” output of /usr/sbin/apachectl (without any command) and it clearly states, [-k start|restart|graceful|graceful-stop|stop]. I am so used to the way Apache works on Linux systems now it seems I forgot about the -k flag for use with apachectl commands. So knowing that the real problem you had is you were attempting to restart with this command sudo apachectl restart when it should have had the -k flag added like this sudo apachectl -k restart. – JakeGould – 2015-09-13T17:21:37.070