UP | HOME

pecl 切换 php 版本

Table of Contents

开发环境或线上环境多个 php 版本是正常的现象,很经常为了方便我们会使用 pecl 安装一些库类。往往 pecl 只有一个,总不能针对每个 php 版本安装
一个吧,如: pecl7 pecl8 pecl5… 吧。这样的话使用 pecl 的时候就要切换 php 版本了。

前提

我们先查看 peclphp 相关安装路径情况。

➜  ~ php --version
PHP 8.0.3 (cli) (built: Mar  4 2021 05:33:14) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.3, Copyright (c) Zend Technologies
➜  ~ whereis php
php: /usr/bin/php /usr/lib/php /etc/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz
➜  ~ whereis php7
php7: /usr/bin/php7 /usr/lib/php7 /etc/php7 /usr/include/php7 /usr/share/php7 /usr/share/man/man1/php7.1.gz
➜  ~ whereis pecl
pecl: /usr/bin/pecl

脚本

#! /bin/sh
#
# pecl_switch.sh
# Copyright (C) 2021 josephzeng <josephzeng@lxt>
#
# Distributed under terms of the MIT license.
#


if [[ $1 == 'php8' ]];then
    sudo pecl config-set ext_dir /usr/lib/php/modules
    sudo pecl config-set php_dir /usr/share/php
    sudo pecl config-set php_bin /usr/bin/php
    sudo pecl config-set php_ini /etc/php/php.ini
    sudo pecl config-set php_suffix 8
elif [[ $1 == 'php7' ]];then
    sudo pecl config-set ext_dir /usr/lib/php7/modules
    sudo pecl config-set php_dir /usr/share/php7
    sudo pecl config-set php_bin /usr/bin/php7
    sudo pecl config-set php_ini /etc/php7/php.ini
    sudo pecl config-set php_suffix 7
fi

保存为 pecl_switch.sh 文件

使用

➜  ~ sh pecl_switch.sh php8
config-set succeeded
config-set succeeded
config-set succeeded
config-set succeeded
config-set succeeded
➜  ~ sh pecl_switch.sh php7
config-set succeeded
config-set succeeded
config-set succeeded
config-set succeeded
config-set succeeded

核查

➜  ~ sudo pecl config-show
CONFIGURATION (CHANNEL PECL.PHP.NET):
=====================================
Auto-discover new Channels     auto_discover    0
Default Channel                default_channel  pecl.php.net
HTTP Proxy Server Address      http_proxy       <not set>
PEAR server [DEPRECATED]       master_server    pear.php.net
Default Channel Mirror         preferred_mirror pecl.php.net
Remote Configuration File      remote_config    <not set>
PEAR executables directory     bin_dir          /usr/bin
PEAR documentation directory   doc_dir          /usr/share/pear/doc
PHP extension directory        ext_dir          /usr/lib/php7/modules
PEAR directory                 php_dir          /usr/share/php7
PEAR Installer cache directory cache_dir        /tmp/pear/cache
PEAR configuration file        cfg_dir          /usr/share/pear/cfg
directory
PEAR data directory            data_dir         /usr/share/pear/data
PEAR Installer download        download_dir     /tmp/pear/download
directory
Systems manpage files          man_dir          /usr/share/pear/local/man
directory
PEAR metadata directory        metadata_dir     <not set>
PHP CLI/CGI binary             php_bin          /usr/bin/php7
php.ini location               php_ini          /etc/php7/php.ini
--program-prefix passed to     php_prefix       <not set>
PHP's ./configure
--program-suffix passed to     php_suffix       7
PHP's ./configure
PEAR Installer temp directory  temp_dir         /tmp/pear/temp
PEAR test directory            test_dir         /usr/share/pear/test
PEAR www files directory       www_dir          /usr/share/pear/htdocs
Cache TimeToLive               cache_ttl        3600
Preferred Package State        preferred_state  stable
Unix file mask                 umask            22
Debug Log Level                verbose          1
PEAR password (for             password         <not set>
maintainers)
Signature Handling Program     sig_bin          /bin/gpg
Signature Key Directory        sig_keydir       /etc/php/pearkeys
Signature Key Id               sig_keyid        <not set>
Package Signature Type         sig_type         gpg
PEAR username (for             username         <not set>
maintainers)
User Configuration File        Filename         /root/.pearrc
System Configuration File      Filename         /etc/php/pear.conf

Author: josephzeng

Lastmod: <2021-03-16 Tue>

License: CC BY-NC-ND 4.0

First created: 2021-03-16 Tue 00:00
Last updated: 2021-11-25 Thu 23:21
Power by Emacs 27.1 (Org mode 9.4)
© 2017 – 2021 by josephzeng