Ubuntu Server 编译升级 PHP 至 7.3.8 版本

8 月 1 日 PHP 释出了 7.3.8 稳定版本。昨天折腾好了 Nginx 今天就来升级 PHP 吧!其实是昨天弄了好久都没成功今天才成功了

备份当前 PHP

为了避免翻车,先对当前的 PHP 安装目录进行备份:

[email protected]:~# which php
/usr/local/php/bin/php

cp -r /usr/local/php /usr/local/php_backup

获取当前 PHP 编译参数

为了避免新装的 PHP 缺少已经在使用的功能造成错误,需要使用以前编译的相同参数(当然也可以加上新的参数)进行全新编译。

# From https://stackoverflow.com/questions/9520957/get-current-php-install-settings
php -i | head -n 10

我的 PHP 是 Oneinstack 一键包安装的,输出的参数如下:

phpinfo()
PHP Version => 7.3.6

System => Linux castle 4.15.0-54-generic #58-Ubuntu SMP Mon Jun 24 10:55:24 UTC 2019 x86_64
Build Date => Jul  3 2019 14:18:49
Configure Command =>  './configure'  '--prefix=/usr/local/php' '--with-config-file-path=/usr/local/php/etc' '--with-config-file-scan-dir=/usr/local/php/etc/php.d' '--with-fpm-user=www' '--with-fpm-group=www' '--enable-fpm' '--disable-opcache' '--disable-fileinfo' '--enable-mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-iconv-dir=/usr/local' '--with-freetype-dir' '--with-jpeg-dir' '--with-png-dir' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-bcmath' '--enable-shmop' '--enable-exif' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl=/usr/local/curl' '--enable-mbregex' '--enable-mbstring' '--with-password-argon2' '--with-sodium=/usr/local' '--with-gd' '--with-openssl=/usr/local/openssl' '--with-mhash' '--enable-pcntl' '--enable-sockets' '--with-xmlrpc' '--enable-ftp' '--enable-intl' '--with-xsl' '--with-gettext' '--enable-zip' '--without-libzip' '--enable-soap' '--disable-debug'
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /usr/local/php/etc
Loaded Configuration File => /usr/local/php/etc/php.ini

由于输出的编译参数是带 '' 的,复制到代码编辑器里把 ' 替换掉就可以了。

仔细阅读编译参数,看是否有需要外部库的部分,确认各种 DIR 参数是否有相应目录,例如

ls /usr/local/openssl
bin  cert.pem  certs  include  lib  man  misc  openssl.cnf  private

下载 PHP 源码

新建临时文件夹,在 https://www.php.net/downloads.php 页面下载 PHP 的最新 Stable 版本,解压缩,进入源码目录。

cd ~ && mkdir php_temp && cd php_temp
wget https://www.php.net/distributions/php-7.3.8.tar.gz
tar -xzf php-7.3.8.tar.gz
cd php-7.3.8

开始编译

确认编译参数无误以后开始编译。

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

如果没有出错,大概可以看到这样的输出:

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.

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

此时可以先执行 make 命令;但是这一步在我的机器上有出错,错误如下:

ext/xmlrpc/libxmlrpc/encodings.o: In function `convert':
/root/php_temp/php-7.3.8/ext/xmlrpc/libxmlrpc/encodings.c:65: undefined reference to `libiconv_open'
/root/php_temp/php-7.3.8/ext/xmlrpc/libxmlrpc/encodings.c:72: undefined reference to `libiconv'
/root/php_temp/php-7.3.8/ext/xmlrpc/libxmlrpc/encodings.c:88: undefined reference to `libiconv_close'
/root/php_temp/php-7.3.8/ext/xmlrpc/libxmlrpc/encodings.c:88: undefined reference to `libiconv_close'
collect2: error: ld returned 1 exit status
Makefile:293: recipe for target 'sapi/cli/php' failed
make: *** [sapi/cli/php] Error 1

搜索以后,根据 https://fukun.org/archives/10102487.html 里的命令解决了问题:

make ZEND_EXTRA_LIBS='-liconv'

make 就成功了:

Build complete.
Don't forget to run 'make test'.

最后执行 make install 即可完成编译:

[email protected]:~/php_temp/php-7.3.8# make install
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM defconfig:     skipping
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing phpdbg binary:         /usr/local/php/bin/
Installing phpdbg man page:       /usr/local/php/php/man/man1/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/php/php/man/man1/
Installing build environment:     /usr/local/php/lib/php/build/
Installing header files:          /usr/local/php/include/php/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php/lib/php/
[PEAR] Archive_Tar    - already installed: 1.4.7
[PEAR] Console_Getopt - already installed: 1.4.2
[PEAR] Structures_Graph- already installed: 1.1.1
[PEAR] XML_Util       - already installed: 1.4.3
[PEAR] PEAR           - already installed: 1.10.9
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/root/php_temp/php-7.3.8/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/

使用 php -v 命令查看当前 PHP 版本:

[email protected]:~/php_temp/php-7.3.8# php -v
PHP 7.3.8 (cli) (built: Aug 10 2019 10:23:35) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.8, Copyright (c) 1998-2018 Zend Technologies

升级成功了;最后重启 PHP 以应用更改。

service php-fpm restart

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据