简体   繁体   English

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

[英]Switch php versions on commandline ubuntu 16.04

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. So how do I switch between php 5.6 and php 7.1 in the command line ? php 使用 php 7 处理请求。那么如何在命令行中在 php 5.6 和 php 7.1 之间切换?

Interactive switching mode交互切换方式

sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar

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
$ sudo update-alternatives --set phar /usr/bin/phar7.1
$ sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.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 来源

$ sudo update-alternatives --config php

should work for all ubuntu versions after 16.04 ( 18.04 and 20.04 )应该适用于16.0418.0420.04 )之后的所有 ubuntu 版本

This is what you should see as a response这是你应该看到的回应

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选择合适的版本

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

I actually wouldn't recommend using a2enmod for php 5 or 7. I would use update-alternatives .我实际上不建议在 php 5 或 7 中使用a2enmod 。我会使用update-alternatives You can do sudo update-alternatives --config php to set which system wide version of PHP you want to use.您可以执行sudo update-alternatives --config php来设置要使用的系统范围的 PHP 版本。 This makes your command line and apache versions work the same.这使您的命令行和 apache 版本的工作方式相同。 You can read more about update-alternatives on the man page .您可以在手册页上阅读有关update-alternatives更多信息。

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

You can create a script to switch from versions: sudo nano switch_php then type this:您可以创建一个脚本来从版本切换: sudo nano switch_php然后输入:

#!/bin/sh
#!/bin/bash
echo "Switching to PHP$1..."
case $1 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!

in ubuntu 20.04 switching between PHP 8.0 and PHP 7.4 version:ubuntu 20.04切换PHP 8.0PHP 7.4版本:

DOWNGRADE PHP 8.0 to PHP 7.4PHP 8.0降级PHP 7.4

sudo a2dismod php8.0
sudo a2enmod php7.4
sudo service apache2 restart
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo service apache2 restart

UPGRADE PHP 7.4 to PHP 8.0PHP 7.4升级PHP 8.0

sudo a2dismod php7.4
sudo a2enmod php8.0
sudo service apache2 restart
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo service apache2 restart

check the changes:检查更改:

  1. run php -v in the console and you will got:在控制台运行php -v ,你会得到:

PHP 8.0.3 (cli) (built: Mar 5 2021 07:54:13) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.3, Copyright (c) Zend Technologies with Zend OPcache v8.0.3, Copyright (c), by Zend Technologies PHP 8.0.3 (cli) (built: Mar 5 2021 07:54:13) ( NTS ) 版权所有 (c) The PHP Group Zend Engine v4.0.3, 版权所有 (c) Zend Technologies with Zend OPcache v8.0.3, 版权所有 ( c),由 Zend Technologies

OR或者

PHP 7.4.16 (cli) (built: Mar 5 2021 07:54:38) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies PHP 7.4.16 (cli) (built: Mar 5 2021 07:54:38) ( NTS ) 版权所有 (c) The PHP Group Zend Engine v3.4.0, 版权所有 (c) Zend Technologies with Zend OPcache v7.4.16, 版权所有 ( c),由 Zend Technologies

  1. add a PHP file in your runnable local environment like /var/www/html/ path by adding phpinfo();通过添加phpinfo();在可运行的本地环境中添加一个 PHP 文件,例如/var/www/html/路径phpinfo(); and get PHP info in the browser (in top of the page the version of PHP is available to see)并在浏览器中获取 PHP 信息(在页面顶部可以查看 PHP 版本)

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

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

You can use below command lines to switch between two PHP version.您可以使用以下命令行在两个 PHP 版本之间切换。

Eg例如

I want to switch PHP Version from 7.1 to 7.2 we can use below command我想将PHP Version7.1切换到7.2我们可以使用以下命令

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

a2dismod is use to disable the current php version and a2enmod is use to enable the version a2dismod用于禁用当前的 php 版本, a2enmod用于启用该版本

this worked for me:-这对我有用:-

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

just change the PHP version to whichever version you need I have changed it to php7.4只需将 PHP 版本更改为您需要的任何版本我已将其更改为 php7.4

Type given command in your terminal..在终端中输入给定的命令..

For disable the selected PHP version...要禁用选定的 PHP 版本...

    • sudo a2dismod php5须藤 a2dismod php5
    • sudo service apache2 restart须藤服务 apache2 重启
  1. For enable other PHP version....用于启用其他 PHP 版本....

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

It will upgrade Php version, same thing reverse if you want version downgrade, you can see it by PHP_INFO();它将升级Php版本,如果你想要版本降级,你可以通过PHP_INFO()看到它;

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

You could use these open source PHP Switch Scripts, which were designed specifically for use in Ubuntu 16.04 LTS.您可以使用这些开源 PHP 切换脚本,它们专为在 Ubuntu 16.04 LTS 中使用而设计。

https://github.com/rapidwebltd/php-switch-scripts https://github.com/rapidwebltd/php-switch-scripts

There is a setup.sh script which installs all required dependencies for PHP 5.6, 7.0, 7.1 & 7.2.有一个setup.sh脚本,它为 PHP 5.6、7.0、7.1 和 7.2 安装所有必需的依赖项。 Once this is complete, you can just run one of the following switch scripts to change the PHP CLI and Apache 2 module version.完成后,您只需运行以下切换脚本之一即可更改 PHP CLI 和 Apache 2 模块版本。

./switch-to-php-5.6.sh
./switch-to-php-7.0.sh
./switch-to-php-7.1.sh
./switch-to-php-7.2.sh

I made a bash script to switch between different PHP versions on Ubuntu.我制作了一个 bash 脚本来在 Ubuntu 上的不同 PHP 版本之间切换。

Hope it helps someone.希望它可以帮助某人。

Here's the script: (save it in /usr/local/bin/sphp.sh, don't forget to add +x flag with command: sudo chmod +x /usr/local/bin/sphp.sh)这是脚本:(将其保存在 /usr/local/bin/sphp.sh 中,不要忘记使用命令添加 +x 标志: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!"

From PHP 5.6 => PHP 7.1从 PHP 5.6 => PHP 7.1

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

for old linux versions对于旧的 linux 版本

 $ sudo service apache2 restart

for more recent version最新版本

$ systemctl restart apache2

When installing laravel on Ubuntu 18.04, be default PHP 7.3.0RC3 install selected, but laravel and symfony will not install properly complaining about missin php-xml and php-zip, even though they are installed.在 Ubuntu 18.04 上安装 laravel 时,默认选择 PHP 7.3.0RC3 安装,但 laravel 和 symfony 不会正确安装,抱怨缺少 php-xml 和 php-zip,即使它们已安装。 You need to switch to php 7.1, using the instructions above or,您需要使用上述说明切换到 php 7.1,或者,

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

now, running laravel new blog, will proceed correctly现在,运行 laravel 新博客,将正确进行

please follow the steps :请按照以下步骤操作:

i.e : your current version is : current_version = 7.3 , and you want to change it to : new_version = 7.2

1) sudo a2dismod php(current_version) 
2) sudo a2enmod php(new_version)
3) sudo update-alternatives --config php (here you need to select php version number) 
4) restart apache through : 
  sudo /etc/init.d/apache2 restart OR
  sudo service apache2 restart

You can use the below script to switch between PHP version easily I have included phpize configuration too.您可以使用下面的脚本轻松地在 PHP 版本之间切换,我也包含了 phpize 配置。

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

Download the script file and run下载脚本文件并运行

sh switch.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM