一,备份原先的php文件

查看现有php版本:

[root@localhost local]# php -v
PHP 5.5.38 (cli) (built: Oct 24 2017 15:58:09) 

备份,由于不是覆盖安装,这里也可以不备份,但是,作为日常操作习惯来说,备份还是很有必要的。

[root@localhost ~]# cd /usr/local/
[root@localhost local]# cp -a php php5538

二,下载解压PHP的最新稳定版7.2.2

[root@localhost ~]# cd soft
[root@localhost soft]# wget http://cn2.php.net/distributions/php-7.2.2.tar.bz2
[root@localhost soft]# tar xf php-7.2.2.tar.bz2
[root@localhost soft]# cd php-7.2.2/

三,查看之前php的configure信息

升级自然要重新安装,那么之前的configure自然要知晓,怎么查看之前的configure信息呢,一般来说有两种方法,第一种,是通过phpinfo()信息:
CentOS7下PHP5.5.38升级PHP7.2.2
第二种,通过命令的方式:

[root@localhost ~]# php -i | grep configure

Configure Command =>  './configure'  '--prefix=/usr/local/php' '--with-config-file-path=/usr/local/php/etc' '--with-config-file-scan-dir=/usr/local/php/conf.d' '--enable-fpm' '--with-fpm-user=www' '--with-fpm-group=www' '--with-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-iconv-dir' '--with-freetype-dir=/usr/local/freetype' '--with-jpeg-dir' '--with-png-dir' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-bcmath' '--enable-shmop' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl' '--enable-mbregex' '--enable-mbstring' '--with-mcrypt' '--enable-ftp' '--with-gd' '--enable-gd-native-ttf' '--with-openssl' '--with-mhash' '--enable-pcntl' '--enable-sockets' '--with-xmlrpc' '--enable-zip' '--enable-soap' '--with-gettext' '--disable-fileinfo' '--enable-opcache' '--enable-intl' '--with-xsl'

通过sed 将configure命令提取出来:

[root@localhost ~]# php -i | grep configure | sed -e "s/Configure Command => //; s/'//g"

 ./configure  --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --enable-intl --with-xsl

四,开始编译安装php7.2.2

[root@localhost php-7.2.2]# ./configure  --prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--with-config-file-scan-dir=/usr/local/php7/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--enable-intl --with-xsl

configure的过程中会报一个警告信息:

configure: WARNING: unrecognized options: --with-mysql, --with-mcrypt, --enable-gd-native-ttf

表示php7.2.2不支持以上三个选项,删掉即可。

下面开始编译:

[root@localhost php-7.2.2]# make && make install

这个过程非常耗时,需要耐心等待。

这里会报一个错误:

/root/soft/php-7.2.2/ext/xmlrpc/libxmlrpc/encodings.c:65: undefined reference to `libiconv_open'
/root/soft/php-7.2.2/ext/xmlrpc/libxmlrpc/encodings.c:73: undefined reference to `libiconv'
/root/soft/php-7.2.2/ext/xmlrpc/libxmlrpc/encodings.c:93: undefined reference to `libiconv_close'
/root/soft/php-7.2.2/ext/xmlrpc/libxmlrpc/encodings.c:93: undefined reference to `libiconv_close'
collect2: error: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1

报错提示缺少libiconv这个库,缺啥就装啥。

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar xf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local/libiconv
make && make install

安装完后需要在configure时加入--with-iconv=/usr/local/libiconv参数,然后再次编译就不会报错了

五,复制修改配置文件,启动php-fpm

[root@localhost local]# cp php/etc/php.ini php7/etc/
[root@localhost local]# cp php/etc/php-fpm.conf php7/etc/
[root@localhost ~]# service php-fpm stop 
Gracefully shutting down php-fpm . done
#关闭原来的php-fpm

修改php-fpm.conf

[global]
pid = /usr/local/php7/var/run/php-fpm.pid
error_log = /usr/local/php7/var/log/php-fpm.log

删除/usr/local/php目录

[root@localhost local]# rm -rf php

创建指向php7的软链接

[root@localhost local]# ln -sv php7 php

启动php-fpm

[root@localhost etc]# service php-fpm start 
Starting php-fpm [23-Feb-2018 20:48:41] NOTICE: PHP message: PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/redis.so' (tried: /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/redis.so (/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/redis.so: undefined symbol: zval_used_for_init), /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718//usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/redis.so.so (/usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718//usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/redis.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
[23-Feb-2018 20:48:41] ERROR: An another FPM instance seems to already listen on /tmp/php-cgi.sock
[23-Feb-2018 20:48:41] ERROR: FPM initialization failed
 failed

报错了,提示redis扩展没有装。

六,安装redis扩展

下载地址
https://github.com/phpredis/phpredis/archive/php7.zip

unzip php7.zip
cd soft/phpredis-php7/
/usr/local/php7/bin/phpize
./configure --with-php-config=/usr/local/php7/bin/php-config
make && make install

在php.ini中添加如下一行

extension=redis.so

重启php-fpm

[root@localhost etc]# service php-fpm restart 
Gracefully shutting down php-fpm . done
Starting php-fpm  done

查看php版本

[root@localhost etc]# php -v
PHP 7.2.2 (cli) (built: Feb 23 2018 19:25:46) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

查看phpinfo()
CentOS7下PHP5.5.38升级PHP7.2.2
至此,php升级完成。