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

这篇博客介绍了在Linux环境中,当PHP版本在phpinfo()和命令行中不一致时如何进行修复。提供了多种解决方案,包括使用`update-alternatives`命令来设置PHP5.6为默认版本,以及如何备份和恢复PHP版本信息。此外,还详细解释了`update-alternatives`命令的作用和如何交互式地切换PHP版本。
摘要由CSDN通过智能技术生成

问题描述

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

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

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

最佳解决方案

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

次佳解决方案

从PHP 7.1 => PHP 5.6

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

从PHP 5.6 => PHP 7.1

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

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

问候!!

第三种解决方案

此解释基于Ubuntu 16.04,但预计也适用于其他版本

这里的大多数答案都是通过使用命令来操纵php-version

sudo update-alternatives --set ...

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

获取信息

命令update-alternatives正在显示或更改符号链接,这些符号链接位于Ubuntu中的替代方向,通常定义为/etc/alternatives,但也可以更改。

可以使用命令update-alternatives --help显示与update-alternatives相关的完整选项列表,可以使用man update-alternatives显示更深入的说明。

由于命令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

使用命令update-alternatives显示项目使用此命令:

$ update-alternatives --list php /usr/bin/php7.0 /usr/bin/php7.2

要显示更多细节,您可以执行此操作:

$ 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

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

$ 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

备份信息

要对这些符号链接进行信息备份,只需将列表保存在文件中,然后使用此命令将其保存在桌面上(根据需要调整文件名alternatives-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:

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

更改命令行的PHP-version

如果我们首先过滤man文件,那么/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

虽然一方面为整个系统提供一致的链接可能是有用的另一方面,系统上已经存在几个php-versions的事实意味着它用于开发,而用于cli,web和cgi的php可以被不同地配置。哪个符号链接必须更改?用于Web的PHP通常不是通过使用/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

检查更改:

$: 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.

现在我们检查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 ...

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

第四种方案

这是更具互动性的方式:

update-alternatives --config php

您将看到所有可用版本的列表,并选择其中一个版本

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值