当系统中安装了多个 php 版本时,如何使用 pecl 为特定 php 版本安装 php 扩展?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40419718/
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:10:01  来源:igfitidea点击:

How to install php extension using pecl for specific php version, when several php versions installed in system?

phpubuntuphp-7php-extensionpecl

提问by Oleg Abrazhaev

I have installed both php5.6 and php7.0 from PPA on Ubuntu according to this manual

我已经根据本手册在 Ubuntu 上从 PPA 安装了 php5.6 和 php7.0

http://lornajane.net/posts/2016/php-7-0-and-5-6-on-ubuntu

http://lornajane.net/posts/2016/php-7-0-and-5-6-on-ubuntu

But I didn't get how to install extensions using peclfor php5.6 or php7.0.

但是我不知道如何安装pecl用于 php5.6 或 php7.0 的扩展。

For example I have already installed version of libeventor amqpin php5.6.

例如我已经安装了 php5.6的libevent或版本amqp

Now when I type pecl install libeventand my active php version is php7.0 (using update-alternatives --set php /usr/bin/php7.0),peclreturns message thatlibevent` already installed.

现在,当我输入时pecl install libevent,我的活动 php 版本是 php7.0(使用已经安装的update-alternatives --set php /usr/bin/php7.0),pecl returns message thatlibevent`。

But it was installed only for php5.6 (when this version was active) and now I want to do it for php7.0.

但它只为 php5.6 安装(当此版本处于活动状态时),现在我想为 php7.0 安装它。

Which commands could help me?

哪些命令可以帮助我?

UPD

UPD

I have found this commands for switch pecl to php7.0 and packet them to executable bash scripts:

我找到了将 pecl 切换到 php7.0 并将它们打包到可执行 bash 脚本的命令:

#!/bin/bash

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

sudo pecl config-set php_ini /etc/php/7.0/cli/php.ini
sudo pecl config-set ext_dir /usr/lib/php/20151012/
sudo pecl config-set bin_dir /usr/bin/
sudo pecl config-set php_bin /usr/bin/php7.0
sudo pecl config-set php_suffix 7.0

and for php5.6

和 php5.6

#!/bin/bash

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

sudo pecl config-set php_ini /etc/php/5.6/cli/php.ini
sudo pecl config-set ext_dir /usr/lib/php/20131226/
sudo pecl config-set bin_dir /usr/bin/
sudo pecl config-set php_bin /usr/bin/php5.6
sudo pecl config-set php_suffix 5.6

But they are not help, pecl still gives me listof already installed extensions to php5.6, even if I switched to php7.

但他们没有帮助,list即使我切换到 php7 ,pecl 仍然给我已经安装的 php5.6 扩展。

pecl list
Installed packages, channel pecl.php.net:
=========================================
Package  Version State
amqp     1.7.1   stable
libevent 0.1.0   beta
stats    1.0.3   stable

It should be empty for php7.0 !

php7.0 应该是空的!

How to solve the problem?

如何解决问题?

UPD

UPD

For amqp I have just installed php-amqp package without using pecl.

对于 amqp,我刚刚安装了 php-amqp 包,而没有使用 pecl。

apt-get install php-amqp

And libevent still not exists for php7. But I hadn't found a way to switch pecl installation between 5.6 and 7 version, so question is still open.

对于 php7,libevent 仍然不存在。但是我还没有找到在 5.6 和 7 版本之间切换 pecl 安装的方法,所以问题仍然存在。

回答by Matt Zuba

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

在尝试编写此脚本时,以下是最适合我的方法(以防其他人像我一样遇到此问题):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version>piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -rbit does not actually uninstall it (from the docs):

-d php_suffix=<version>件作品允许您在运行时设置配置值,而不是使用pecl config-set. 该uninstall -r位实际上并没有卸载它(来自文档):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

卸载行是必需的,否则安装它将删除任何以前安装的版本,即使它是用于不同的 PHP 版本(例如:如果软件包仍注册为已安装,则安装 PHP 7.0 的扩展将删除 5.6 版本)。

回答by Panoptik

When pecl throws error is already installed and is the same as the released version

当pecl抛出错误时已经安装并且与发布版本相同

Switch to required php, php-config, phpize versions before installing from pecl

在从 pecl 安装之前切换到所需的 php、php-config、phpize 版本

Just run it installation with force flag

只需使用 force 标志运行它安装

sudo pecl install -f <package-name>

回答by deviantintegral

I ran into this same issue while updating my Vagrant box with XHGui, as XHGui requires mongodb. I wanted to be able to support profiling on both PHP 5.6 and 7.0.

我在用 XHGui更新我的 Vagrant 盒子时遇到了同样的问题,因为 XHGui 需要 mongodb。我希望能够在 PHP 5.6 和 7.0 上支持分析。

I dug into the pecl source code, and found that there's a metadata_dirconfig option. That is a path to a directory where the current state of installed packages. Unfortunately, that isn't already namespaced per PHP version. If you try and set it with pecl config-set, you get an opaque 'failed' error. It turns out that setting isn't whitelisted as being configuable in the \PEAR_Configclass:

我深入研究了 pecl 源代码,发现有一个metadata_dir配置选项。这是已安装包的当前状态所在目录的路径。不幸的是,这不是每个 PHP 版本的命名空间。如果您尝试将其设置为pecl config-set,则会收到不透明的“失败”错误。事实证明,该设置并未在\PEAR_Config类中被列入可配置的白名单:

/**
 * Configuration values that can be set for a channel
 *
 * All other configuration values can only have a global value
 * @var array
 * @access private
 */
var $_channelConfigInfo = array(
    'php_dir', 'ext_dir', 'doc_dir', 'bin_dir', 'data_dir', 'cfg_dir',
    'test_dir', 'www_dir', 'php_bin', 'php_prefix', 'php_suffix', 'username',
    'password', 'verbose', 'preferred_state', 'umask', 'preferred_mirror', 'php_ini'
    );

In PECL's world, 'global' means it can only be set at install time, and not after.

在 PECL 的世界中,“全局”意味着它只能在安装时设置,而不能在安装后设置。

There's an issue in the PPA tracker over at github: https://github.com/oerdnj/deb.sury.org/issues/407

github 上的 PPA 跟踪器存在问题:https: //github.com/oerdnj/deb.sury.org/issues/407

The final suggestion there is to build the extension manually for alternate PHP versions. I ended up using pecl for PHP 7 extensions, and manual builds for 5.6. Make sure you run update-alternativesfor php-config and phpize, and not just php before building:

最后的建议是为替代的 PHP 版本手动构建扩展。我最终将 pecl 用于 PHP 7 扩展,并为 5.6 手动构建。确保update-alternatives在构建之前运行php-config 和 phpize,而不仅仅是 php:

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

Then, extract the extension and build it. These steps from the above issue worked for me with the mongodb extension:

然后,提取扩展并构建它。上述问题中的这些步骤对我使用了 mongodb 扩展:

phpize5.6 && ./configure --with-php-config=php-config5.6 && make && sudo make install

phpize5.6 && ./configure --with-php-config=php-config5.6 && make && sudo make install