如何在Ubuntu 14.04上更改php-cli版本?

时间:2022-08-24 07:26:25

I am new to using Linux and I broke some php settings while tampering.

我是使用Linux的新手,我在篡改时打破了一些php设置。

If I execute a php script containing: phpinfo(); it shows the php versions as 5.6, but via the command line, if I run php -v it returns a version of 7.0.

如果我执行包含以下内容的php脚本:phpinfo();它将php版本显示为5.6,但是通过命令行,如果我运行php -v它将返回7.0版本。

I want to have both versions match.
How can i fix my command line PATH to also use php 5.6?

我希望两个版本都匹配。我如何修复我的命令行PATH也使用PHP 5.6?

4 个解决方案

#1


38  

sudo update-alternatives --set php /usr/bin/php5.6

Please see: Source

请参阅:来源

#2


8  

From PHP 7.1 => PHP 5.6

从PHP 7.1 => PHP 5.6

sudo update-alternatives --set php /usr/bin/php5.6

From PHP 5.6 => PHP 7.1

从PHP 5.6 => PHP 7.1

sudo update-alternatives --set php /usr/bin/php7.1

You can change the versions as you want.

您可以根据需要更改版本。

You can check this tutorial

您可以查看本教程

Regards!!

问候!!

#3


1  

This explanation is based on Ubuntu 16.04 but is expected to work for other versions too

Most answers here manipulate the php-version by using the command

这里的大多数答案通过使用命令来操纵php版本

sudo update-alternatives --set ...

While the command is quite useful it's never explained what it does exactly. Here comes the explanation, including backup and some options:

虽然命令非常有用,但它从未解释过它的确切功能。这里有解释,包括备份和一些选项:

Get the Information

  • The command update-alternatives is displaying or changing symlinks which reside in an alternative direction which is in Ubuntu usually defined as /etc/alternatives but which could be changed too.
  • 命令update-alternatives正在显示或更改符号链接,这些符号链接位于Ubuntu中的替代方向,通常定义为/ etc / alternatives,但也可以更改。
  • a full list of options related to update-alternatives can be shown with the command update-alternatives --help, a deeper explanation can be shown with man update-alternatives.
  • 可以使用命令update-alternatives --help显示与update-alternatives相关的选项的完整列表,可以使用man update-alternatives显示更深入的解释。
  • As the command update-alternatives is primary changing symlinks in a special folder, the content of that folder can be shown with common commands too, following all items starting with php are shown:
  • 由于命令update-alternatives是特殊文件夹中的主要更改符号链接,因此该文件夹的内容也可以使用常用命令显示,并显示以php开头的所有项目:
 $ ls -al /etc/alternatives/php*  
lrwxrwxrwx 1 root root 15 Jan 19 02:58 /etc/alternatives/php -> /usr/bin/php7.2  
lrwxrwxrwx 1 root root 31 Jan 19 02:58 /etc/alternatives/php.1.gz -> /usr/share/man/man1/php7.2.1.gz  
lrwxrwxrwx 1 root root 19 Jan 19 03:00 /etc/alternatives/php-cgi -> /usr/bin/php-cgi7.2  
lrwxrwxrwx 1 root root 35 Jan 19 03:00 /etc/alternatives/php-cgi.1.gz -> /usr/share/man/man1/php-cgi7.2.1.gz 
lrwxrwxrwx 1 root root 23 Jan 19 03:00 /etc/alternatives/php-cgi-bin -> /usr/lib/cgi-bin/php7.2
  • to display the items with the command update-alternatives use this command:
  • 使用命令update-alternatives显示项目使用此命令:
$ update-alternatives --list php  
/usr/bin/php7.0  
/usr/bin/php7.2
  • to show more details you can execute this:
  • 要显示更多细节,您可以执行此操作:
$ update-alternatives --display php
php - auto mode
  link best version is /usr/bin/php7.2
  link currently points to /usr/bin/php7.2
  link php is /usr/bin/php
  slave php.1.gz is /usr/share/man/man1/php.1.gz
/usr/bin/php7.0 - priority 70
  slave php.1.gz: /usr/share/man/man1/php7.0.1.gz
/usr/bin/php7.2 - priority 72
  slave php.1.gz: /usr/share/man/man1/php7.2.1.gz

Finally we still want to know which php-versions we can link. The following command shows a list of the currently available php-versions in /usr/bin/:

最后,我们仍然想知道我们可以链接哪些php版本。以下命令显示/ usr / bin /中当前可用的php版本列表:

$ ls -al /usr/bin/php*
lrwxrwxrwx 1 root root      21 Jan  1 19:47 /usr/bin/php -> /etc/alternatives/php
-rwxr-xr-x 1 root root 4385840 Apr  5 18:13 /usr/bin/php7.0
-rwxr-xr-x 1 root root 4875488 Apr  5 18:10 /usr/bin/php7.2
lrwxrwxrwx 1 root root      25 Jan  1 19:47 /usr/bin/php-cgi -> /etc/alternatives/php-cgi
-rwxr-xr-x 1 root root 4279672 Apr  5 18:13 /usr/bin/php-cgi7.0
-rwxr-xr-x 1 root root 4769272 Apr  5 18:10 /usr/bin/php-cgi7.2
-rwxr-xr-x 1 root root     663 Feb 29  2016 /usr/bin/phpunit

Backup the information

  • To make an informational backup of these symlinks just save the list in a file and save it on your desktop with this command (adjust the filename alternatives-php.txt to your needs):
  • 要对这些符号链接进行信息备份,只需将列表保存在文件中,然后使用此命令将其保存在桌面上(根据需要调整文件名alternative-php.txt):
$ echo "command: update-alternatives --list php" > ~/Desktop/alternatives-php.txt
$ update-alternatives --display php >> ~/Desktop/alternatives-php.txt 
$ echo "command: update-alternatives --display php" >> ~/Desktop/alternatives-php.txt
$ update-alternatives --display php >> ~/Desktop/alternatives-php.txt  

if you like you still can add the result of the command ls like shown above:

如果你喜欢你仍然可以添加如上所示的命令的结果:

$ echo "command: ls -al /etc/alternatives/php*" >> ~/Desktop/alternatives-php.txt
$ ls -al /etc/alternatives/php* >> ~/Desktop/alternatives-php.txt

... and the available PHP-versions:

...以及可用的PHP版本:

$ echo "command: ls -al /usr/bin/php*" >> ~/Desktop/alternatives-php.txt
$ ls -al /usr/bin/php* >> ~/Desktop/alternatives-php.txt

Change the PHP-version for commandline

  • If we filter first the man-documents the list in /etc/alternatives/php*/ includes 3 lines:
  • 如果我们首先过滤man-documents,/ etc / alternatives / php * /中的列表包含3行:
 $ ls -al /etc/alternatives/php*  
lrwxrwxrwx 1 root root 15 Jan 19 02:58 /etc/alternatives/php -> /usr/bin/php7.2  
lrwxrwxrwx 1 root root 19 Jan 19 03:00 /etc/alternatives/php-cgi -> /usr/bin/php-cgi7.2  
lrwxrwxrwx 1 root root 23 Jan 19 03:00 /etc/alternatives/php-cgi-bin -> /usr/lib/cgi-bin/php7.2
  • While on the one hand it might be useful to have consistent links for the whole system on the other hand the fact that several php-versions exist already on the system implies that it's used for development and php for cli, web and cgi could be configured differently.
    Which symlink has to be changed?
    PHP for web is usually not configured by usage of the symlinks in /etc/alternatives and cli(commandline) is usually not using the cgi-version or cgi-configuration. So it can be assumed that changing only the symlink for php will work for commandline, change of php-cgi and php-cgi-bin is probably not required. Usually the most recent version is linked by default, so the command below is using an older version taken from the list in /usr/bin/:
  • 虽然一方面为整个系统提供一致的链接可能是有用的另一方面,系统上已存在几个php版本的事实意味着它用于开发,并且可以配置用于cli,web和cgi的php不同。哪个符号链接必须更改? PHP for web通常不是通过使用/ etc / alternatives中的符号链接来配置的,而cli(命令行)通常不使用cgi-version或cgi-configuration。因此可以假设只更改php的符号链接将适用于命令行,可能不需要更改php-cgi和php-cgi-bin。通常,最新版本默认链接,因此下面的命令使用从/ usr / bin /中的列表中获取的旧版本:
$ sudo update-alternatives --set php /usr/bin/php7.0
update-alternatives: using /usr/bin/php7.0 to provide /usr/bin/php (php) in manual mode

Control

Checking the change:

检查更改:

$: php -v
PHP 7.0.29-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Apr  5 2018 08:34:50) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.29-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

The change was accepted and the used version is an older one now.

该更改已被接受,现在使用的版本较旧。

  • Now we check the version the man-page is written for:
  • 现在我们检查man-page的编写版本:
$: man php
... [SCROLL DOWN]
VERSION INFORMATION
   This manpage describes php, version 7.0.29-1+ubuntu16.04.1+deb.sury.org+1.
COPYRIGHT
...

This means the PHP-version is consistent with the man-page and the command man php returns the right descriptions for the current php-version and no manual adjustments concerning the man-page have been required.

这意味着PHP版本与man-page一致,并且命令man php返回当前php-version的正确描述,并且不需要关于man-page的手动调整。

#4


0  

You can try setting the path to the correct php version on the terminal command line:

您可以尝试在终端命令行上设置正确的php版本的路径:

set PATH="/usr/bin/php5.6/bin:$PATH"

Modify the path to match your own path to PHP 5.6 (ie. if it was installed first, it might be something like "/usr/bin/php").

修改路径以匹配您自己的PHP 5.6路径(即,如果它首先安装,它可能类似于“/ usr / bin / php”)。

The "$PATH" at the end appends the current path value, so don't forget to use it.

最后的“$ PATH”会附加当前路径值,所以不要忘记使用它。

#1


38  

sudo update-alternatives --set php /usr/bin/php5.6

Please see: Source

请参阅:来源

#2


8  

From PHP 7.1 => PHP 5.6

从PHP 7.1 => PHP 5.6

sudo update-alternatives --set php /usr/bin/php5.6

From PHP 5.6 => PHP 7.1

从PHP 5.6 => PHP 7.1

sudo update-alternatives --set php /usr/bin/php7.1

You can change the versions as you want.

您可以根据需要更改版本。

You can check this tutorial

您可以查看本教程

Regards!!

问候!!

#3


1  

This explanation is based on Ubuntu 16.04 but is expected to work for other versions too

Most answers here manipulate the php-version by using the command

这里的大多数答案通过使用命令来操纵php版本

sudo update-alternatives --set ...

While the command is quite useful it's never explained what it does exactly. Here comes the explanation, including backup and some options:

虽然命令非常有用,但它从未解释过它的确切功能。这里有解释,包括备份和一些选项:

Get the Information

  • The command update-alternatives is displaying or changing symlinks which reside in an alternative direction which is in Ubuntu usually defined as /etc/alternatives but which could be changed too.
  • 命令update-alternatives正在显示或更改符号链接,这些符号链接位于Ubuntu中的替代方向,通常定义为/ etc / alternatives,但也可以更改。
  • a full list of options related to update-alternatives can be shown with the command update-alternatives --help, a deeper explanation can be shown with man update-alternatives.
  • 可以使用命令update-alternatives --help显示与update-alternatives相关的选项的完整列表,可以使用man update-alternatives显示更深入的解释。
  • As the command update-alternatives is primary changing symlinks in a special folder, the content of that folder can be shown with common commands too, following all items starting with php are shown:
  • 由于命令update-alternatives是特殊文件夹中的主要更改符号链接,因此该文件夹的内容也可以使用常用命令显示,并显示以php开头的所有项目:
 $ ls -al /etc/alternatives/php*  
lrwxrwxrwx 1 root root 15 Jan 19 02:58 /etc/alternatives/php -> /usr/bin/php7.2  
lrwxrwxrwx 1 root root 31 Jan 19 02:58 /etc/alternatives/php.1.gz -> /usr/share/man/man1/php7.2.1.gz  
lrwxrwxrwx 1 root root 19 Jan 19 03:00 /etc/alternatives/php-cgi -> /usr/bin/php-cgi7.2  
lrwxrwxrwx 1 root root 35 Jan 19 03:00 /etc/alternatives/php-cgi.1.gz -> /usr/share/man/man1/php-cgi7.2.1.gz 
lrwxrwxrwx 1 root root 23 Jan 19 03:00 /etc/alternatives/php-cgi-bin -> /usr/lib/cgi-bin/php7.2
  • to display the items with the command update-alternatives use this command:
  • 使用命令update-alternatives显示项目使用此命令:
$ update-alternatives --list php  
/usr/bin/php7.0  
/usr/bin/php7.2
  • to show more details you can execute this:
  • 要显示更多细节,您可以执行此操作:
$ update-alternatives --display php
php - auto mode
  link best version is /usr/bin/php7.2
  link currently points to /usr/bin/php7.2
  link php is /usr/bin/php
  slave php.1.gz is /usr/share/man/man1/php.1.gz
/usr/bin/php7.0 - priority 70
  slave php.1.gz: /usr/share/man/man1/php7.0.1.gz
/usr/bin/php7.2 - priority 72
  slave php.1.gz: /usr/share/man/man1/php7.2.1.gz

Finally we still want to know which php-versions we can link. The following command shows a list of the currently available php-versions in /usr/bin/:

最后,我们仍然想知道我们可以链接哪些php版本。以下命令显示/ usr / bin /中当前可用的php版本列表:

$ ls -al /usr/bin/php*
lrwxrwxrwx 1 root root      21 Jan  1 19:47 /usr/bin/php -> /etc/alternatives/php
-rwxr-xr-x 1 root root 4385840 Apr  5 18:13 /usr/bin/php7.0
-rwxr-xr-x 1 root root 4875488 Apr  5 18:10 /usr/bin/php7.2
lrwxrwxrwx 1 root root      25 Jan  1 19:47 /usr/bin/php-cgi -> /etc/alternatives/php-cgi
-rwxr-xr-x 1 root root 4279672 Apr  5 18:13 /usr/bin/php-cgi7.0
-rwxr-xr-x 1 root root 4769272 Apr  5 18:10 /usr/bin/php-cgi7.2
-rwxr-xr-x 1 root root     663 Feb 29  2016 /usr/bin/phpunit

Backup the information

  • To make an informational backup of these symlinks just save the list in a file and save it on your desktop with this command (adjust the filename alternatives-php.txt to your needs):
  • 要对这些符号链接进行信息备份,只需将列表保存在文件中,然后使用此命令将其保存在桌面上(根据需要调整文件名alternative-php.txt):
$ echo "command: update-alternatives --list php" > ~/Desktop/alternatives-php.txt
$ update-alternatives --display php >> ~/Desktop/alternatives-php.txt 
$ echo "command: update-alternatives --display php" >> ~/Desktop/alternatives-php.txt
$ update-alternatives --display php >> ~/Desktop/alternatives-php.txt  

if you like you still can add the result of the command ls like shown above:

如果你喜欢你仍然可以添加如上所示的命令的结果:

$ echo "command: ls -al /etc/alternatives/php*" >> ~/Desktop/alternatives-php.txt
$ ls -al /etc/alternatives/php* >> ~/Desktop/alternatives-php.txt

... and the available PHP-versions:

...以及可用的PHP版本:

$ echo "command: ls -al /usr/bin/php*" >> ~/Desktop/alternatives-php.txt
$ ls -al /usr/bin/php* >> ~/Desktop/alternatives-php.txt

Change the PHP-version for commandline

  • If we filter first the man-documents the list in /etc/alternatives/php*/ includes 3 lines:
  • 如果我们首先过滤man-documents,/ etc / alternatives / php * /中的列表包含3行:
 $ ls -al /etc/alternatives/php*  
lrwxrwxrwx 1 root root 15 Jan 19 02:58 /etc/alternatives/php -> /usr/bin/php7.2  
lrwxrwxrwx 1 root root 19 Jan 19 03:00 /etc/alternatives/php-cgi -> /usr/bin/php-cgi7.2  
lrwxrwxrwx 1 root root 23 Jan 19 03:00 /etc/alternatives/php-cgi-bin -> /usr/lib/cgi-bin/php7.2
  • While on the one hand it might be useful to have consistent links for the whole system on the other hand the fact that several php-versions exist already on the system implies that it's used for development and php for cli, web and cgi could be configured differently.
    Which symlink has to be changed?
    PHP for web is usually not configured by usage of the symlinks in /etc/alternatives and cli(commandline) is usually not using the cgi-version or cgi-configuration. So it can be assumed that changing only the symlink for php will work for commandline, change of php-cgi and php-cgi-bin is probably not required. Usually the most recent version is linked by default, so the command below is using an older version taken from the list in /usr/bin/:
  • 虽然一方面为整个系统提供一致的链接可能是有用的另一方面,系统上已存在几个php版本的事实意味着它用于开发,并且可以配置用于cli,web和cgi的php不同。哪个符号链接必须更改? PHP for web通常不是通过使用/ etc / alternatives中的符号链接来配置的,而cli(命令行)通常不使用cgi-version或cgi-configuration。因此可以假设只更改php的符号链接将适用于命令行,可能不需要更改php-cgi和php-cgi-bin。通常,最新版本默认链接,因此下面的命令使用从/ usr / bin /中的列表中获取的旧版本:
$ sudo update-alternatives --set php /usr/bin/php7.0
update-alternatives: using /usr/bin/php7.0 to provide /usr/bin/php (php) in manual mode

Control

Checking the change:

检查更改:

$: php -v
PHP 7.0.29-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Apr  5 2018 08:34:50) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.29-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

The change was accepted and the used version is an older one now.

该更改已被接受,现在使用的版本较旧。

  • Now we check the version the man-page is written for:
  • 现在我们检查man-page的编写版本:
$: man php
... [SCROLL DOWN]
VERSION INFORMATION
   This manpage describes php, version 7.0.29-1+ubuntu16.04.1+deb.sury.org+1.
COPYRIGHT
...

This means the PHP-version is consistent with the man-page and the command man php returns the right descriptions for the current php-version and no manual adjustments concerning the man-page have been required.

这意味着PHP版本与man-page一致,并且命令man php返回当前php-version的正确描述,并且不需要关于man-page的手动调整。

#4


0  

You can try setting the path to the correct php version on the terminal command line:

您可以尝试在终端命令行上设置正确的php版本的路径:

set PATH="/usr/bin/php5.6/bin:$PATH"

Modify the path to match your own path to PHP 5.6 (ie. if it was installed first, it might be something like "/usr/bin/php").

修改路径以匹配您自己的PHP 5.6路径(即,如果它首先安装,它可能类似于“/ usr / bin / php”)。

The "$PATH" at the end appends the current path value, so don't forget to use it.

最后的“$ PATH”会附加当前路径值,所以不要忘记使用它。