在命令行 ubuntu 16.04 上切换 php 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42619312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:39:23  来源:igfitidea点击:

Switch php versions on commandline ubuntu 16.04

phpapachecommand-line-interfacephp-5.6php-7.1

提问by salimsaid

I have installed php 5.6 and and php 7.1 on my Ubuntu 16.04

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

I know with Apache as my web server, I can do

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

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

When I disable php7.1 in Apache modules and enable php 5.6, Apache recognizes the change and uses php 5.6 interpreter as expected.

当我在 Apache 模块中禁用 php7.1 并启用 php 5.6 时,Apache 会识别更改并按预期使用 php 5.6 解释器。

But when I run internal php web server from the commandline:

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

php -S localhost:8888

php handles requests using php 7, how do I switch between php 6.6 and php 7.1 in the commandline ?

php 使用 php 7 处理请求,如何在命令行中在 php 6.6 和 php 7.1 之间切换?

回答by Stevie G

Interactive switching mode

交互切换方式

sudo update-alternatives --config php

sudo update-alternatives --config php

Manual Switching

手动切换

From PHP 5.6 => PHP 7.1

从 PHP 5.6 => PHP 7.1

Default PHP 5.6 is set on your system and you need to switch to PHP 7.1.

您的系统上设置了默认 PHP 5.6,您需要切换到 PHP 7.1。

Apache:

阿帕奇:

$ sudo a2dismod php5.6
$ sudo a2enmod php7.1
$ sudo service apache2 restart

Command Line:

命令行:

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

From PHP 7.1 => PHP 5.6

从 PHP 7.1 => PHP 5.6

Default PHP 7.1 is set on your system and you need to switch to PHP 5.6.

您的系统上设置了默认 PHP 7.1,您需要切换到 PHP 5.6。

Apache:

阿帕奇:

$ sudo a2dismod php7.1
$ sudo a2enmod php5.6
$ sudo service apache2 restart

Command Line:

命令行:

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

Source

来源

回答by GiorgosK

type this in your shell

在你的 shell 中输入这个

$ sudo update-alternatives --config php

and this is what you will get

这就是你会得到的

There are 4 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php7.2   72        auto mode
  1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php7.0   70        manual mode
  3            /usr/bin/php7.1   71        manual mode
  4            /usr/bin/php7.2   72        manual mode
Press <enter> to keep the current choice[*], or type selection number:

Choose the appropriate version

选择合适的版本

回答by Rupinder Sohal

I think you should try this

我想你应该试试这个

From php5.6 to php7.1

从 php5.6 到 php7.1

sudo a2dismod php5.6
sudo a2enmod php7.1
sudo service apache2 restart

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

From php7.1 to php5.6

从 php7.1 到 php5.6

sudo a2dismod php7.1
sudo a2enmod php5.6
sudo service apache2 restart

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

回答by Kamal Kumar

To list all available versions and choose from them :

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

sudo update-alternatives --config php

Or do manually

或者手动执行

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

回答by ihakoz

You can create a script to switch from versions: sudo nano switch_phpthen type this:

您可以创建一个脚本来从版本切换:sudo nano switch_php然后输入:

#!/bin/sh
#!/bin/bash
echo "Switching to PHP..."
case  in
    "7")
        sudo a2dismod php5.6
        sudo a2enmod php7.0
        sudo service apache2 restart
        sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php;;
    "5.6")
        sudo a2dismod php7.0
        sudo a2enmod php5.6
        sudo service apache2 restart
        sudo ln -sfn /usr/bin/php5.6 /etc/alternatives/php;;
esac
echo "Current version: $( php -v | head -n 1 | cut -c-7 )"

exit and save make it executable: sudo chmod +x switch_php

退出并保存使其可执行: sudo chmod +x switch_php

To execute the script just type ./switch_php [VERSION_NUMBER]where the parameter is 7 or 5.6

要执行脚本,只需键入./switch_php [VERSION_NUMBER]参数为 7 或 5.6 的位置

That's it you can now easily switch form PHP7 to PHP 5.6!

就是这样,您现在可以轻松地从 PHP7 切换到 PHP 5.6!

回答by FatBoyXPC

I actually wouldn't recommend using a2enmodfor php 5 or 7. I would use update-alternatives. You can do sudo update-alternatives --config phpto set which system wide version of PHP you want to use. This makes your command line and apache versions work the same. You can read more about update-alternativeson the man page.

我实际上不建议使用a2enmodphp 5 或 7。我会使用update-alternatives. 您可以sudo update-alternatives --config php设置要使用的系统范围的 PHP 版本。这使您的命令行和 apache 版本的工作方式相同。您可以update-alternatives手册页上阅读更多信息。

回答by Ashish Viradiya

You can use below command lines to switch between two PHP version.

您可以使用以下命令行在两个 PHP 版本之间切换。

E.g.

例如

I want to switch PHP Versionfrom 7.1to 7.2we can use below command

我想改用PHP Version7.17.2我们可以使用下面的命令

sudo a2dismod php7.1 &&  sudo update-alternatives --set php /usr/bin/php7.2 && sudo a2enmod php7.2 && sudo service apache2 restart

a2dismodis use to disable the current php version and a2enmodis use to enable the version

a2dismod用于禁用当前的 php 版本,a2enmod用于启用该版本

回答by Dinesh Suthar

May be you might have an old PHP version like PHP 5.6 in your system and you installed PHP 7.2 too so thats multiple PHP in your machine. There are some applications which were developed when older PHP 5.6 was latest version, they are still live and you working on those applications, You might be working on Laravel simultaneously but Laravel requires PHP 7+ to get started. Getting the picture ?

可能您的系统中可能有一个旧的 PHP 版本,例如 PHP 5.6,并且您也安装了 PHP 7.2,因此您的机器中有多个 PHP。有一些应用程序是在较旧的 PHP 5.6 为最新版本时开发的,它们仍然存在并且您正在开发这些应用程序,您可能同时在 Laravel 上工作,但 Laravel 需要 PHP 7+ 才能开始。得到图片?

In that case you can switch between the PHP versions to suit your requirements.

在这种情况下,您可以在 PHP 版本之间切换以满足您的要求。

Switch From PHP 5.6 => PHP 7.2

从 PHP 5.6 => PHP 7.2 切换

Apache:-

阿帕奇:-

sudo a2dismod php5.6
sudo a2enmod php7.2
sudo service apache2 restart

Command Line:-

命令行:-

sudo update-alternatives --set php /usr/bin/php7.2
sudo update-alternatives --set phar /usr/bin/phar7.2
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.2
sudo update-alternatives --set phpize /usr/bin/phpize7.2
sudo update-alternatives --set php-config /usr/bin/php-config7.2

And vice-versa, Switch From PHP 7.2 => PHP 5.6

反之亦然,从 PHP 7.2 切换 => PHP 5.6

Apache:-

阿帕奇:-

sudo a2dismod php7.2
sudo a2enmod php5.6
sudo service apache2 restart

Command Line:-

命令行:-

sudo update-alternatives --set php /usr/bin/php5.6
sudo update-alternatives --set phar /usr/bin/phar5.6
sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6
sudo update-alternatives --set phpize /usr/bin/phpize5.6
sudo update-alternatives --set php-config /usr/bin/php-config5.6

回答by Sonpal singh Sengar

Type given command in your terminal..

在终端中输入给定的命令..

For disable the selected PHP version...

要禁用选定的 PHP 版本...

    • sudo a2dismod php5
    • sudo service apache2 restart
  1. For enable other PHP version....

    • sudo a2enmod php5.6
    • sudo service apache2 restart
    • 须藤 a2dismod php5
    • 须藤服务 apache2 重启
  1. 用于启用其他 PHP 版本....

    • 须藤 a2enmod php5.6
    • 须藤服务 apache2 重启

It will upgrade Php version, same thing reverse if you want version downgrade, you can see it by PHP_INFO();

它将升级Php版本,如果你想要版本降级,你可以通过PHP_INFO()看到它;

回答by user1560627

Switch from PHP 5.6 to PHP 7.2 using:

使用以下命令从 PHP 5.6 切换到 PHP 7.2:

sudo a2dismod php5.6 && sudo a2enmod php7.2 && sudo service apache2 restart


Switch from PHP 7.2 to PHP 5.6 using:

使用以下命令从 PHP 7.2 切换到 PHP 5.6:

sudo a2dismod php7.2 && sudo a2enmod php5.6 && sudo service apache2 restart