从 PHP 7 升级到 PHP 7.2
在 PHP 7.1 发布差不多1年后,PHP 7.2 终于发布。本文是从 PHP 7.1.0 升级到 PHP 7.2.1,从 PHP 7 的其他版本升级到 PHP 7.2.1 也是大同小异。如果是新安装,请参考: http://www.lnmp.cn/installing-php7-mysql57-nginx18-under-centos7.html
我之前是把 PHP 7.1.0 安装在
/usr/local/php-7.1.0
为了方便以后升级,加了这个软链,链接到 php-7.1.0
/usr/local/php
使用到 PHP 路径的时候一般都是使用这条软链路径。
先下载 PHP 7.2.1 源码,我下载的源码都是统一放到 /usr/local/src
[root@lnmp lnmp.cn]# cd /usr/local/src
[root@lnmp src]# wget -c http://cn2.php.net/get/php-7.2.1.tar.gz/from/this/mirror -O php-7.2.1.tar.gz
解压,并进入源码目录
[root@lnmp src]# tar -zxvf php-7.2.1.tar.gz [root@lnmp src]# cd php-7.2.1/
既然是升级,就要保留原来的配置不变,这里要先找到之前安装 PHP 7.1.0 的 configure 选项。这个选项在 phpinfo 中,也可以用 PHP 7.1.0 的 php-config 命令获取:
[root@lnmp php-7.2.1]# /usr/local/php-7.1.0/bin/php-config --configure-options
--prefix=/usr/local/php-7.1.0 --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-pdo-mysql --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-ftp --enable-zip
如果想看下 php-config 的全部信息, 直接执行
[root@lnmp php-7.2.1]# /usr/local/php-7.1.0/bin/php-config Usage: /usr/local/php-7.1.0/bin/php-config [OPTION] Options: --prefix [/usr/local/php-7.1.0] --includes [-I/usr/local/php-7.1.0/include/php -I/usr/local/php-7.1.0/include/php/main -I/usr/local/php-7.1.0/include/php/TSRM -I/usr/local/php-7.1.0/include/php/Zend -I/usr/local/php-7.1.0/include/php/ext -I/usr/local/php-7.1.0/include/php/ext/date/lib] --ldflags [] --libs [-lcrypt -lz -lresolv -lcrypt -lrt -lpng -lz -ljpeg -lz -lrt -lm -ldl -lnsl -lxml2 -lz -lm -ldl -lssl -lcrypto -lcurl -lxml2 -lz -lm -ldl -lssl -lcrypto -lfreetype -lxml2 -lz -lm -ldl -lcrypt -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lssl -lcrypto -lcrypt ] --extension-dir [/usr/local/php-7.1.0/lib/php/extensions/no-debug-non-zts-20170718] --include-dir [/usr/local/php-7.1.0/include/php] --man-dir [/usr/local/php-7.1.0/php/man] --php-binary [/usr/local/php-7.1.0/bin/php] --php-sapis [ cli fpm phpdbg cgi] --configure-options [--prefix=/usr/local/php-7.1.0 --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-pdo-mysql --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-ftp --enable-zip] --version [7.1.0] --vernum [70100]
或者有人会问,configure 有哪些选项?怎么知道哪些是 with?哪些是 enable?可以通过这个命令查看
[root@lnmp php-7.2.1]# ./configure --help
有相当多的选项。可以在 configure 时就添加的扩展其实都在源码目录的 ext/ 目录下,不在这个目录下的可以在安装完 PHP 后用 pecl 或者下载源码再用 phpize 安装。
至于要加哪些选项,哪些扩展,根据自己的实际需要增减,譬如说要支持 Laravel,就需要这些扩展:OpenSSL ,PDO,Mbstring,Tokenizer,XML,如果用MySQL数据,很可能需要 pdo_mysql。当然 PHP 本身默认会装一些扩展,例如 pdo, tokenizer 和 xml。至于默认安装了哪些,可以查看下 configure 选项中的 disable 和 without 部分。
我这里沿用之前的 configure,把安装路径改成 /usr/local/php-7.2.1
[root@lnmp php-7.2.1]# ./configure --prefix=/usr/local/php-7.2.1 --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-pdo-mysql --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-ftp --enable-zip
configure 成功会出现这样的字样
Generating files configure: creating ./config.status creating main/internal_functions.c creating main/internal_functions_cli.c +--------------------------------------------------------------------+ | License: | | This software is subject to the PHP License, available in this | | distribution in the file LICENSE. By continuing this installation | | process, you are bound by the terms of this license agreement. | | If you do not agree with the terms of this license, you must abort | | the installation process at this point. | +--------------------------------------------------------------------+ Thank you for using PHP.
接下来 make
[root@lnmp php-7.2.1]# make
成功后会出现:
Build complete. Don't forget to run 'make test'. [root@lnmp php-7.2.1]#
这里可以执行 make test 跑一下测试,但我这边跑的时候有一些timeout,不过应该问题不大。
接着 make install
[root@lnmp php-7.2.1]# make install Installing shared extensions: /usr/local/php-7.2.1/lib/php/extensions/no-debug-non-zts-20170718/ Installing PHP CLI binary: /usr/local/php-7.2.1/bin/ Installing PHP CLI man page: /usr/local/php-7.2.1/php/man/man1/ Installing PHP FPM binary: /usr/local/php-7.2.1/sbin/ Installing PHP FPM defconfig: /usr/local/php-7.2.1/etc/ Installing PHP FPM man page: /usr/local/php-7.2.1/php/man/man8/ Installing PHP FPM status page: /usr/local/php-7.2.1/php/php/fpm/ Installing phpdbg binary: /usr/local/php-7.2.1/bin/ Installing phpdbg man page: /usr/local/php-7.2.1/php/man/man1/ Installing PHP CGI binary: /usr/local/php-7.2.1/bin/ Installing PHP CGI man page: /usr/local/php-7.2.1/php/man/man1/ Installing build environment: /usr/local/php-7.2.1/lib/php/build/ Installing header files: /usr/local/php-7.2.1/include/php/ Installing helper programs: /usr/local/php-7.2.1/bin/ program: phpize program: php-config Installing man pages: /usr/local/php-7.2.1/php/man/man1/ page: phpize.1 page: php-config.1 Installing PEAR environment: /usr/local/php-7.2.1/lib/php/ [PEAR] Archive_Tar - installed: 1.4.3 [PEAR] Console_Getopt - installed: 1.4.1 [PEAR] Structures_Graph- installed: 1.1.1 [PEAR] XML_Util - installed: 1.4.2 [PEAR] PEAR - installed: 1.10.5 Warning! a PEAR user config file already exists from a previous PEAR installation at '/root/.pearrc'. You may probably want to remove it. Wrote PEAR system config file at: /usr/local/php-7.2.1/etc/pear.conf You may want to add: /usr/local/php-7.2.1/lib/php to your php.ini include_path /usr/local/src/php-7.2.1/build/shtool install -c ext/phar/phar.phar /usr/local/php-7.2.1/bin ln -s -f phar.phar /usr/local/php-7.2.1/bin/phar Installing PDO headers: /usr/local/php-7.2.1/include/php/ext/pdo/ [root@lnmp php-7.2.1]#
这里是一个总结,文件都安装在那个文件夹。扩展文件,可执行文件等等。
[root@lnmp php-7.2.1]# /usr/local/php-7.2.1/bin/php -v PHP 7.2.1 (cli) (built: Jan 15 2018 01:57:19) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
可以看到 PHP 7.2.1 已经安装好。
然后就是配置文件,因为之前的版本可能已经修改了一些配置文件的选项,为了不影响现有网站的运行,这里将沿用 PHP 7.1.0 的配置文件,直接从 PHP 7.1.0 文件夹中拷贝过去
[root@lnmp php-7.2.1]# cp /usr/local/php-7.1.0/lib/php.ini /usr/local/php-7.2.1/lib/php.ini
[root@lnmp php-7.2.1]# cp /usr/local/php-7.1.0/etc/php-fpm.conf /usr/local/php-7.2.1/etc/php-fpm.conf
[root@lnmp php-7.2.1]# cp /usr/local/php-7.1.0/etc/php-fpm.d/www.conf /usr/local/php-7.2.1/etc/php-fpm.d/www.conf
如果之前的版本在安装 PHP 后有用 pecl 或 phpize 新增过扩展的话,因为拷贝过来的 php.ini 中已经引入了那些扩展,在这里要重新安装,是要重新安装,从旧版本中将这些 so 文件拷贝过来也是不行。之前怎么安装,现在又怎么重新安装一遍吧,这里不再累述。
至此 PHP 7.2.1 升级基本完成,在切换到 PHP 7.2.1 之前先将 PHP 7.1.0 的 php-fpm 关闭
[root@lnmp php-7.2.1]# systemctl stop php-fpm
这里要把 PHP 7.2.1 的 php-fpm 服务加到系统服务中。
[root@lnmp php-7.2.1]# cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm-721.service
这里改成 php-fpm-721.service,是为了区分不同版本,最终会软链到 php-fpm.service
这个文件按道理 PHP 在安装的时候就已经把里边的路径配置好,应该不用作修改,但还是要检查下,如果里边出现 ${prefix} 或 ${exec_prefix}/ 但变量,要替换成 /usr/local/php-7.2.1。
最后有两个软链要修改:
一个是 /usr/local/php, 一个是 /usr/lib/systemd/system/php-fpm.service 都要改到新升级的版本路径:
[root@lnmp php-7.2.1]# unlink /usr/local/php [root@lnmp php-7.2.1]# ln -s /usr/local/php-7.2.1 /usr/local/php
[root@lnmp php-7.2.1]# unlink /usr/lib/systemd/system/php-fpm.service [root@lnmp php-7.2.1]# ln -s /usr/lib/systemd/system/php-fpm-721.service /usr/lib/systemd/system/php-fpm.service
重新加载服务
[root@lnmp php-7.2.1]# systemctl daemon-reload
让 php-fpm.service 随开机启动
[root@lnmp php-7.2.1]# systemctl start php-fpm
启动 php-fpm
[root@lnmp php-7.2.1]# systemctl start php-fpm
查看 php-fpm 状态
[root@lnmp php-7.2.1]# systemctl status php-fpm ● php-fpm-721.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/php-fpm-721.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2018-01-15 07:02:37 UTC; 6s ago Main PID: 21939 (php-fpm) CGroup: /system.slice/php-fpm-721.service ├─21939 php-fpm: master process (/usr/local/php-7.2.1/etc/php-fpm.conf) ├─21940 php-fpm: pool www └─21941 php-fpm: pool www Jan 15 07:02:37 localhost.localdomain systemd[1]: Started The PHP FastCGI Process Manager. Jan 15 07:02:37 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Manager... [root@lnmp php-7.2.1]#
可以看到 php-fpm 已经成功启动。
测试下 phpinfo
可以看到已经成功升级到 PHP 7.2.1 版本
至于程序上需要的改动,我这边其实没有,因为 PHP 7.2.1 新建了一些特性,但不兼容的修改并不多,根据自己的实际情况吧,可以参考这里:
PHP 7.2 新特性:
http://php.net/manual/en/migration72.new-features.php
http://php.net/manual/en/migration72.new-functions.php
http://php.net/manual/en/migration72.constants.php
PHP 7.2 不往后兼容修改:
http://php.net/manual/en/migration72.incompatible.php
PHP 7.2 将要废弃的特性(还可以用,但会有WARNING)
http://php.net/manual/en/migration72.deprecated.php
PHP 7.2 其他改动:
http://php.net/manual/en/migration72.other-changes.php
至此,PHP 7.2.1 已经升级完毕,这样升级也保留了原来的 PHP 7.1.0 版本,如果升级后网站出现问题,可以轻松切回到 PHP 7.1.0。又或者同时使用两个版本的 PHP,具体请参考:
http://www.lnmp.cn/multiple-php-versions-under-lnmp.html
前一篇: http 扩展已经加载,但 http 函数全部不可以用
后一篇: guzzle 6.2 在 PHP 7.2 下及升级到 6.3 遇到的问题