美文网首页
php版本切换脚本

php版本切换脚本

作者: 猿来是八阿哥 | 来源:发表于2019-07-28 11:36 被阅读0次

php 版本切换脚本

假设你在机器上安装了许多php版本(都安装到了 /var/ 目录下),这时为了 debug 代码在不同版本下产生的问题,需要切换 php 版本,下面的脚本的思路是:

  1. 输入版本号,判断 /var/php-$version 是否存在
  2. 验证目标版本是否是合法的 php 安装目录
  3. /usr/bin/ 下的 php 相关可执行文件的软连删除
  4. 将目标版本的可执行文件软连到 /usr/bin/
  5. 删除 php-fpm 进程,并启动目标版本的 php-fpm 进程
#! /bin/bash
version=$1

# check version to switch is validate
echo start checking php with version=$version...
version_path='/var/php-'$version
if [ ! -d $version_path ] ; then
    echo php with version=$version is not installed...
    exit 0
fi
bin_files='/bin/php /bin/php-config /bin/phpize /sbin/php-fpm'
for bin_file in $bin_files
do
    version_bin_file=$version_path$bin_file
    if [ ! -f $version_bin_file ] ; then
        echo lost file $version_bin_file, switch php version failed...
        exit 0
    fi
done
echo checking php with version=$version successfully, start switching...

# remove execute files in /usr/bin/
remove_exe_files='/usr/bin/php /usr/bin/phpize /usr/bin/php-config /usr/bin/php-fpm'
for remove_file in $remove_exe_files
do
    if [ -f $remove_file ] ; then
        rm -f $remove_file
        if [ ! -f $remove_file ] ; then
            echo removed old bin file success, $remove_file...
        else
            echo removed old bin file failed, $remove_file...
        fi
    fi
done

# link bin file to swith version
for bin_file in $bin_files
do
    version_bin_file=$version_path$bin_file
    ln -s $version_bin_file /usr/bin/
done
for linked_file in $remove_exe_files
do
    if [ ! -f $linked_file ] ; then
        echo link from $version_path to $linked_file failed...
        exit 0
    else
        echo link from $version_path to $linked_file successed...
    fi
done

# kill & restart php-fpm process
php_fpm_process_number_1=$(ps aux | grep php-fpm | grep -v grep | wc -l)
if [ $php_fpm_process_number_1 -gt 0 ] ; then
    ps aux | grep php-fpm | grep -v grep | awk '{print "kill -9 "$2}' | sh
    php_fpm_process_number_2=$(ps aux | grep php-fpm | wc -l)
    if [ $php_fpm_process_number_1 -lt 1 ] ; then
        echo killed old php-fpm process success...
    fi
else
    echo no old php-fpm process to kill, skiped...
fi
php-fpm -c $version_path/php.ini
php_fpm_process_number_3=$(ps aux | grep php-fpm | grep -v grep  | wc -l)
if [ $php_fpm_process_number_3 -gt 0 ] ; then
    echo restart php-fpm successfully, switch php version successfully...
else
    echo restart php-fpm failed, you may need to run: 'php-fpm -c $version_path/php.ini'...
fi

相关文章

  • php版本切换脚本

    php 版本切换脚本 假设你在机器上安装了许多php版本(都安装到了 /var/ 目录下),这时为了 debug ...

  • Ubuntu20下切换php版本

    以php7.4切换到php7.0为例,首先执行: 键入需要切换的版本的序号,此时执行php -v依然显示之前的版本...

  • ubuntu 中,同时安装了多个php时,切换如何切换cli模式

    安装了多个php时,通过cli模式遇到版本太低,执行失败的情况,需要切换php版本查看当前PHP版本 php5.6...

  • php 版本切换

    php 版本切换 brew install homebrew/php/php-version. 细节: 指令: p...

  • tp6安装

    要求:PHP 版本>= 7.1.0 1:PHP版本切换到7.1及以上 Windows命令行执行php -v 如下图...

  • node 版本管理 n

    安装 下载 删除 切换版本 以指定的版本来执行脚本

  • mac平台多个php版本快速切换

    ** 要求所有php版本都是由brew安装 ** 使用brew安装php多版本方法 安装切换工具: 查看当前安装的...

  • mac 配置相关

    ①mac php版本切换 ②修改 Mac 默认 PHP 运行环境,给 MAMP 配置全局 Composer ③Up...

  • macOS 切换php版本

    macOS有自带的php,但是安装了集成开发环境之怎样才能切换到集成开发环境的php版本呢? 1.打开终端输入: ...

  • ubuntu 切换php版本

    原因: linux: 方法: 执行命令 你会看到所有的php版本 提示你键入选择的编号,这里我已经切换到了7.2的...

网友评论

      本文标题:php版本切换脚本

      本文链接:https://www.haomeiwen.com/subject/mvmurctx.html