Centos 7 PHP7.0升级至7.2.11方案

升级原因

Laravel/Lumen框架新版要求PHP>=7.1.3,开发环境、测试环境的PHP版本已经为7.2,web3、web4的也已经升级为7.2,但是web1、web2的机器为7.0。这给开发测试带来了一定的麻烦,对开发来说环境不一样导致部分语法不兼容、扩展不兼容,出现各种一些异常问题。现预发布环境只有一个,放在web1机器,导致使用新框架的项目在预发布测不了。所以便有了升级web1、web2的需要。

升级原则

  1. 对外服务不间断,升级期间保证业务服务能正常访问。
  2. 架构组件影响最小。保证现有扩展能用,不用替代方案。
  3. 尽量不改变业务代码,现有项目尽量不用改代码便可在新环境运行。

大体方案

  1. 摸清需要装的扩展、插件;
  2. 先升级一台机器机器,将负载均衡机器全部导流至另一台机器;
  3. 开始在在一台机器编译升级PHP7.2,编译到新目录;
  4. 装扩展;
  5. 测试PHP7.2是否正常,正常将默认PHP命令软链到7.2;
  6. 完成升级工作,测试项目是否正常,将负载均衡切换到刚升级的机器;
  7. 按以上步骤开始升级另一台机器。

需安装的环境配置

  1. PHP 7.2.11
  2. 扩展 swoole、uuid、pdo_odbc、pdo_sqlsrv、mysqli

前置动作

修改守护进程的PHP版本

cd /etc/supervisord.d/

把里边的配置文件的php版本指定为现在的php具体路径
/usr/local/php7/bin/php

//重启supervisorctl
supervisorctl -c /etc/supervisord/supervisord.conf
update

修改定时任务的PHP版本

crontab -e

/usr/local/php7/bin/php

执行方案

负载均衡

将负载均衡导流至web1;

下载需要的包

下载PHP7.2.11、swoole、uuid、pdo_sqlsrv

wget http://hk2.php.net/distributions/php-7.2.11.tar.gz
wget https://github.com/swoole/swoole-src/archive/v4.2.3.tar.gz
wget http://pecl.php.net/get/uuid-1.0.4.tgz
wget http://pecl.php.net/get/pdo_sqlsrv-5.3.0.tgz

安装PHP 7.2

  1. 查看旧PHP编译配置

    php -i | grep configure
    
    //输出
    './configure'  '--prefix=/usr/local/php7' '--exec-prefix=/usr/local/php7' '--bindir=/usr/local/php7/bin' '--sbindir=/usr/local/php7/sbin' '--includedir=/usr/local/php7/include' '--libdir=/usr/local/php7/lib/php' '--mandir=/usr/local/php7/php/man' '--with-config-file-path=/usr/local/php7/etc' '--with-mysql-sock=/var/run/mysql/mysql.sock' '--with-mhash' '-with-openssl=/usr/local/ssl' '--with-mysqli=mysqlnd' '--with-pdo-mysql' '--with-gd' '--with-iconv' '--with-zlib' '--enable-zip' '--enable-inline-optimization' '--disable-debug' '--disable-rpath' '--enable-shared' '--enable-xml' '--enable-bcmath' '--enable-shmop' '--enable-sysvsem' '--enable-mbregex' '--enable-mbstring' '--enable-ftp' '--enable-pcntl' '--enable-sockets' '--with-xmlrpc' '--enable-soap' '--without-pear' '--with-gettext' '--enable-session' '--with-curl' '--with-jpeg-dir' '--with-freetype-dir' '--enable-opcache' '-enable-fpm' '--with-fpm-user=nginx' '--with-fpm-group=nginx' '--without-gdbm' '--enable-fileinfo'
    

    整理以上命令并将目录指向/usr/local/php7.2.11

    ./configure  --prefix=/usr/local/php7.2.11 --exec-prefix=/usr/local/php7.2.11 --bindir=/usr/local/php7.2.11/bin --sbindir=/usr/local/php7.2.11/sbin --includedir=/usr/local/php7.2.11/include --libdir=/usr/local/php7.2.11/lib/php --mandir=/usr/local/php7.2.11/php/man --with-config-file-path=/usr/local/php7.2.11/etc --with-mysql-sock=/var/run/mysql/mysql.sock --with-mhash -with-openssl --with-mysqli=mysqlnd --with-pdo-mysql --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache -enable-fpm --with-fpm-user=user_00 --with-fpm-group=users --without-gdbm --enable-fileinfo
    
  2. 解压

    //解压
    tar -zvxf php-7.2.11.tar.gz
    cd php-7.2.11
    
  3. 配置

    ./configure  --prefix=/usr/local/php7.2.11 --exec-prefix=/usr/local/php7.2.11 --bindir=/usr/local/php7.2.11/bin --sbindir=/usr/local/php7.2.11/sbin --includedir=/usr/local/php7.2.11/include --libdir=/usr/local/php7.2.11/lib/php --mandir=/usr/local/php7.2.11/php/man --with-config-file-path=/usr/local/php7.2.11/etc --with-mysql-sock=/var/run/mysql/mysql.sock --with-mhash -with-openssl --with-mysqli=mysqlnd --with-pdo-mysql --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache -enable-fpm --with-fpm-user=user_00 --with-fpm-group=users --without-gdbm --enable-fileinfo
    
  4. 编译

    //编译生成安装文件
    make
    
    //运行安装文件
    make install
    
    //查看是否安装成功
    /usr/local/php7.2.11/bin/php -v
    
  5. 复制旧配置文件

    //备份新的etc文件夹
    mv /usr/local/php7.2.11/etc /usr/local/php7.2.11/etc_back
    
    //复制旧有php的配置
    cp -r /usr/local/php7/conf /usr/local/php7.2.11/etc
    

安装swoole

tar -zxvf v4.2.3.tar.gz
cd swoole-src-4.2.3

/usr/local/php7.2.11/bin/phpize

./configure --with-php-config=/usr/local/php7.2.11/bin/php-config --enable-openssl

make

make install

//添加php.ini
extension=swoole.so

//查看swoole版本
/usr/local/php7.2.11/bin/php -r "echo swoole_version();"

安装uuid

tar -zxvf uuid-1.0.4.tgz
cd uuid-1.0.4

/usr/local/php7.2.11/bin/phpize

./configure --with-php-config=/usr/local/php7.2.11/bin/php-config

make

make install

如果出现Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

yum install autoconf

安装pdo_odbc

//安装之前得先安装unixODBC
yum install msodbcsql mssql-tools unixODBC-devel
curl https://packages.microsoft.com/config/rhel/7/prod.repo >> /etc/yum.repos.d/mssqlrelease.repo 


cd ~/php-7.2.11/ext/pdo_odbc/

/usr/local/php7.2.11/bin/phpize

./configure --with-php-config=/usr/local/php7.2.11/bin/php-config --with-pdo-odbc=unixODBC,/usr/

make

make install

//添加至php.ini
extension=pdo_odbc.so

安装pdo_sqlsrv

tar -zxvf pdo_sqlsrv-5.3.0.tgz
cd pdo_sqlsrv-5.3.0

/usr/local/php7.2.11/bin/phpize

./configure --with-php-config=/usr/local/php7.2.11/bin/php-config

make

make install

//添加至php.ini
extension=pdo_sqlsrv.so

安装mysqli

cd ~/php-7.2.11/ext/mysqli/

/usr/local/php7.2.11/bin/phpize

./configure --with-php-config=/usr/local/php7.2.11/bin/php-config

make

make install

//添加至php.ini
extension=mysqli.so

安装EXIF

cd ~/php-7.2.11/ext/exif/

/usr/local/php7.2.11/bin/phpize

./configure --with-php-config=/usr/local/php7.2.11/bin/php-config

make

make install

//添加至php.ini
extension=exif.so

修改软链并重启

  1. 修改软链

    /usr/local/php7/bin/php
    
    rm /usr/bin/phar
    ln -s /usr/local/php7.2.11/bin/phar /usr/bin/phar
    
    rm /usr/bin/phar.phar
    ln -s /usr/local/php7.2.11/bin/phar.phar /usr/bin/phar.phar
    
    rm /usr/bin/php
    ln -s /usr/local/php7.2.11/bin/php /usr/bin/php
    
    rm /usr/bin/php-cgi
    ln -s /usr/local/php7.2.11/bin/php-cgi /usr/bin/php-cgi
    
    rm /usr/bin/php-config
    ln -s /usr/local/php7.2.11/bin/php-config /usr/bin/php-config
    
    rm /usr/bin/phpdbg
    ln -s /usr/local/php7.2.11/bin/phpdbg /usr/bin/phpdbg
    
    rm /usr/bin/phpize
    ln -s /usr/local/php7.2.11/bin/phpize /usr/bin/phpize
    
    rm /usr/bin/phpdbg
    ln -s /usr/local/php7.2.11/bin/phpdbg /usr/bin/phpdbg
    
    
    //修改环境变量
    vim /etc/profile.d/php7.sh
    source /etc/profile
    
  2. 重启

    //查看php-fpm主进程ID
    ps aux | grep php-fpm
    
    //kill掉该进程ID,假如是666
    kill 666
    
    //启动新的
    /usr/local/php7.2.11/sbin/php-fpm
    

升级完成后

  1. 把本机host解析到相应的服务器,并观察是否正常;

  2. 把守护进程管理、crontab的PHP版本修改为新的;

出现问题及解决

perl: warning: Setting locale failed.

vim ~/.bashrc

//在文件尾加上
export LC_ALL=C

//更新生效
source ~/.bashrc

应急预案

升级失败则把软链改回来,并kill掉新的php-fpm,启动原有的

/usr/local/php7/sbin/php-fpm