我已经在我的Ubuntu 16.04上安装了php 5.6和php 7.1

我知道用Apache作为我的网络服务器,我可以做到

a2enmod php5.6 #to enable php5
a2enmod php7.1 #to enable php7

当我在Apache模块中禁用php7.1并启用php 5.6时,Apache识别出了这个变化,并按预期使用php 5.6解释器。

但是当我从命令行运行内部php web服务器时:

php -S localhost:8888

PHP使用PHP 7处理请求。那么如何在命令行中在php 5.6和php 7.1之间切换呢?


当前回答

我做了一个bash脚本在Ubuntu上的不同PHP版本之间切换。

希望它能帮助到别人。

剧本如下: (保存在/usr/local/bin/sphp.sh,不要忘记用命令sudo chmod +x /usr/local/bin/sphp.sh)

#!/bin/bash # Usage if [ $# -ne 1 ]; then echo "Usage: sphp [phpversion]" echo "Example: sphp 7.2" exit 1 fi currentversion="`php -r \"error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));\"`" newversion="$1" majorOld=${currentversion:0:1} minorOld=${currentversion:1:1} majorNew=${newversion:0:1} minorNew=${newversion:2:1} if [ $? -eq 0 ]; then if [ "${newversion}" == "${currentversion}" ]; then echo "PHP version [${newversion}] is already being used" exit 1 fi echo "PHP version [$newversion] found" echo "Switching from [php${currentversion}] to [php${newversion}] ... " printf "a2dismod php$majorOld.$minorOld ... " sudo a2dismod "php${majorOld}.${minorOld}" printf "[OK] and " printf "a2enmod php${newversion} ... " sudo a2enmod "php${majorNew}.${minorNew}" printf "[OK]\n" printf "update-alternatives ... " sudo update-alternatives --set php "/usr/bin/php${majorNew}.${minorNew}" printf "[OK]\n" sudo service apache2 restart printf "[OK] apache2 restarted\n" else echo "PHP version $majorNew.$minorNew was not found." echo "Try \`sudo apt install php@${newversion}\` first." exit 1 fi echo "DONE!"

其他回答

我做了一个bash脚本在Ubuntu上的不同PHP版本之间切换。

希望它能帮助到别人。

剧本如下: (保存在/usr/local/bin/sphp.sh,不要忘记用命令sudo chmod +x /usr/local/bin/sphp.sh)

#!/bin/bash # Usage if [ $# -ne 1 ]; then echo "Usage: sphp [phpversion]" echo "Example: sphp 7.2" exit 1 fi currentversion="`php -r \"error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));\"`" newversion="$1" majorOld=${currentversion:0:1} minorOld=${currentversion:1:1} majorNew=${newversion:0:1} minorNew=${newversion:2:1} if [ $? -eq 0 ]; then if [ "${newversion}" == "${currentversion}" ]; then echo "PHP version [${newversion}] is already being used" exit 1 fi echo "PHP version [$newversion] found" echo "Switching from [php${currentversion}] to [php${newversion}] ... " printf "a2dismod php$majorOld.$minorOld ... " sudo a2dismod "php${majorOld}.${minorOld}" printf "[OK] and " printf "a2enmod php${newversion} ... " sudo a2enmod "php${majorNew}.${minorNew}" printf "[OK]\n" printf "update-alternatives ... " sudo update-alternatives --set php "/usr/bin/php${majorNew}.${minorNew}" printf "[OK]\n" sudo service apache2 restart printf "[OK] apache2 restarted\n" else echo "PHP version $majorNew.$minorNew was not found." echo "Try \`sudo apt install php@${newversion}\` first." exit 1 fi echo "DONE!"

您可以使用下面的脚本轻松地在PHP版本之间切换 我还包括了phpize配置。

https://github.com/anilkumararumulla/switch-php-version

下载脚本文件并运行

sh switch.sh

从PHP 5.6 => PHP 7.1

$ sudo a2dismod php5.6
$ sudo a2enmod php7.1

对于旧的Linux版本

 $ sudo service apache2 restart

对于最新版本

$ systemctl restart apache2

列出所有可用版本并从中进行选择:

sudo update-alternatives --config php

或者手工操作

sudo a2dismod php7.1 // disable
sudo a2enmod php5.6  // enable

这招对我很管用:-

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

只要把PHP版本改为你需要的任何版本,我已经把它改为php7.4