php版本切换脚本

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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容