此解释基于 Ubuntu 16.04,但预计也适用于其他版本
这里的大多数答案都是通过使用命令来操作 php-version
sudo update-alternatives --set ...
虽然该命令非常有用,但从未解释过它的确切作用。下面是解释,包括备份和一些选项:
获取信息
- 命令
update-alternatives 正在显示或更改位于另一个方向的符号链接,在Ubuntu 中通常定义为/etc/alternatives,但也可以更改。
- 与
update-alternatives 相关的选项的完整列表可以使用命令update-alternatives --help 显示,更深入的解释可以使用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 版本。以下命令显示了/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
备份资料
- 要对这些符号链接进行信息备份,只需将列表保存在文件中,然后使用此命令将其保存在桌面上(根据需要调整文件名
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
如果你喜欢你仍然可以添加命令ls 的结果,如上所示:
$ echo "command: ls -al /etc/alternatives/php*" >> ~/Desktop/alternatives-php.txt
$ ls -al /etc/alternatives/php* >> ~/Desktop/alternatives-php.txt
...以及可用的 PHP 版本:
$ echo "command: ls -al /usr/bin/php*" >> ~/Desktop/alternatives-php.txt
$ ls -al /usr/bin/php* >> ~/Desktop/alternatives-php.txt
更改命令行的 PHP 版本
- 如果我们首先过滤
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
- 一方面,为整个系统提供一致的链接可能很有用,另一方面,系统上已经存在多个 php 版本这一事实意味着它用于开发,而 php 用于
cli、@ 987654344@ 和 cgi 可以进行不同的配置。
必须更改哪个符号链接?
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
控制
检查更改:
$: 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
更改已被接受,现在使用的版本是旧版本。
$: man php
... [SCROLL DOWN]
VERSION INFORMATION
This manpage describes php, version 7.0.29-1+ubuntu16.04.1+deb.sury.org+1.
COPYRIGHT
...
这意味着 PHP 版本与手册页一致,命令 man php 返回当前 php 版本的正确描述,无需手动调整手册页.