phpinfo()显示的版本与php -v显示的版本不同的原因是什么?

huangapple go评论79阅读模式
英文:

Why phpinfo() shows a different version than php -v?

问题

我通过brew安装了php。 使用brew install PHP。 我的php-cli版本是7.2

  • 但是,如果我在服务器上使用phpinfo(),我得到的php版本是7.1。
  • 如果我在终端中使用php -v,我得到的php版本是7.2。
英文:

I install php via brew. With brew install PHP. My version of php-cli is 7.2

  • But, if I use phpinfo() on my server, I get php version 7.1.
  • If I use php -v in my terminal, I get php version 7.2.

答案1

得分: 1

免责声明:我将这个_希望_详细的回答添加为复兴徽章的一部分。(在问题被提出后30天以上的第一个得分为2或更多的答案)

简短版本

php -v 的设置可能与您的 web服务器 内部的 phpinfo() 不同,这是由于fpm的运行状态以及您的web服务器的原因。也许您需要重新启动 php-fpm 和/或您的 web服务器(如apache、nginx等)。

详细版本:命令行

基于命令行的调用,如 php -vphp -iphp test.php,每次在终端中执行脚本时都会读取您的配置、选项和参数。

这意味着,如果您在操作系统中更新了php,那么您将在终端会话中获得最新的版本。

注意:根据您的PATH变量,您仍然可能会获得较旧的版本。

这是一个例子:我是一个OSX用户,通过brew安装了php。当前 php 链接到 php@7.3。这是我验证哪些版本在哪里运行的方法。我使用which php。

$ which php
/usr/local/opt/php@7.3/bin/php
$ php -v
PHP 7.3.11 (cli) (built: Oct 24 2019 11:29:42) ( NTS )

// 或等效的“绝对路径”示例

$ /usr/local/opt/php@7.3/bin/php -v
PHP 7.3.11 (cli) (built: Oct 24 2019 11:29:42) ( NTS )

我在本地系统上安装了多个php版本。我可以分别运行它们。这是一个例子:

$ /usr/local/opt/php@7.2/bin/php -v
PHP 7.2.20 (cli) (built: Jul  5 2019 12:56:54) ( NTS )

详细版本:为什么我的phpinfo()使用了错误的php版本?

既然我们现在明白了在单个操作系统上可以运行多个不同版本的php,让我们深入研究运行 phpinfo() 的基于web服务器的脚本。

在安装新的php版本之后,您的二进制文件将被新版本替换。太好了!但是..我的运行中的web服务器如何得知这一点?

为此,我将参考php文档中的Unix系统上的Apache 2.x 第7点。此外,apache允许我们为PHP加载不同的模块。

> 编辑您的httpd.conf以加载PHP模块。[...]
>
> 对于PHP 7:
> LoadModule php7_module modules/libphp7.so
>
> 对于PHP 5:
> LoadModule php5_module modules/libphp5.so

由于您从PHP 7.1升级到PHP 7.2,可能没有更改,但是您的web服务器仍在运行旧版本的php。

在这种情况下,重新启动您的apache web服务器应该有助于它正确地获取新的二进制文件。

请告诉我这是否有助于解决您的问题。对于其他读者:如果您认为我的解释有遗漏或错误,请在评论中告诉我。

英文:

Disclaimer: I'm adding this hopefully extensive answer as part of the Revival badge. (Answer more than 30 days after a question was asked as first answer scoring 2 or more)

TL;DR

php -v settings can be different from phpinfo() inside your web-server due to the running service state of fpm and your webserver. Perhaps you need to restart php-fpm and/or your web-server (apache, nginx, ..).

Long version: Command line

Command-line based calls like php -v or php -i or php test.php will read your configuration, options and arguments every time you execute your script within a terminal.

This means if you update php in your operating system then you'll get the most recent version in your terminal session.

Attention: Depending on your PATH variables you still might get an older version.

What does this mean? Even if you update your php version on your operating system you might still have an older terminal session which links to an older php-binary.

Here is an example: I am an OSX user and installed php through brew. Currently php links to php@7.3. Here is my way to validate which versions are running where. For this I'm using which php.

$ which php
/usr/local/opt/php@7.3/bin/php
$ php -v
PHP 7.3.11 (cli) (built: Oct 24 2019 11:29:42) ( NTS )

// or the equivalent "absolute path" example

$ /usr/local/opt/php@7.3/bin/php -v
PHP 7.3.11 (cli) (built: Oct 24 2019 11:29:42) ( NTS )

I have multiple php-versions installed on my local system. I can run them all individually. Here is an example:

$ /usr/local/opt/php@7.2/bin/php -v
PHP 7.2.20 (cli) (built: Jul  5 2019 12:56:54) ( NTS )

Long version: Why is my phpinfo() using the wrong php version?

Since we now have an understanding that we can run multiple and different versions of php on a single operating system, lets dig into our web-server based script which runs phpinfo().

After installing a new php-version you'll have your binaries replaced by new versions. Awesome! but.. how does my running web server get notified about it?

For this I'll refer to the php documentation Apache 2.x on Unix systems Point 7. Also apache allows us to load different modules for PHP.

> Edit your httpd.conf to load the PHP module. [...]
>
> For PHP 7:
> LoadModule php7_module modules/libphp7.so
>
> For PHP 5:
> LoadModule php5_module modules/libphp5.so

Since you are upgrading from PHP 7.1 to PHP 7.2 there is probably no change but your webserver is still running the old linked version of php.

In this case a restart of your apache webserver should help so it can pickup the new binaries correctly.

Please let me know if this will help you to solve your issue. For all the other readers: If you think there is something missing OR there is something wrong in my explanation then please let me know with a comment.

huangapple
  • 本文由 发表于 2020年1月6日 20:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定