下载并安装最新版PHP7.2.4:
查看PHP最新版本,目前最新稳定版是2018年3月29日发布的PHP 7.2.4:官网下载
Current Stable PHP 7.2.4 (Changelog)
php-7.2.4.tar.bz2 (sig) [14,273Kb] 29 Mar 2018
php-7.2.4.tar.gz (sig) [17,876Kb] 29 Mar 2018
php-7.2.4.tar.xz (sig) [11,750Kb] 29 Mar 2018
我们选择下载最小的一个格式的“PHP-7.2.4.tar.xz”:
使用 wget 命令下载文件,再使用 xz 命令解压称 .tar 格式的文件,再使用 tar 命令解压。
$cd /usr/local/
$wget http://am1.php.net/get/php-7.2.4.tar.xz/from/this/mirror php-7.2.4.tar.xz
$xz -d php-7.2.4.tar.xz
$tar -xvf php-7.2.4.tar
查看上个安装版本的配置
$ php -i | grep configure
Configure Command => './configure' '--prefix=/usr/local/php7.2.4' '--with-config-file-path=/usr/local/php7.2.4/etc' '--with-config-file-scan-dir=/usr/local/php7.2.4/etc/php.d' '--with-apxs2=/usr/local/apache/bin/apxs' '--enable-opcache' '--disable-fileinfo' '--enable-mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-iconv-dir=/usr/local' '--with-iconv=/usr/local/libiconv' '--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' '--enable-mbregex' '--enable-mbstring' '--with-mcrypt' '--with-gd' '--enable-gd-native-ttf' '--with-openssl' '--with-mhash' '--enable-pcntl' '--enable-sockets' '--with-xmlrpc' '--enable-ftp' '--enable-intl' '--with-xsl' '--with-gettext' '--enable-zip' '--enable-soap' '--disable-debug'
格式一下命令,之前使用反斜线(\)执行的命令,显示成了单引号,再将单引号转换成反斜线,在新版本根目录执行配置编译。具体配置命令如下:
./configure --prefix = / usr / local / php7.2.4 \
--with-config-file-path = / usr / local / php7.2.4 / etc \
--with-config-file-scan-dir = / usr / local / php7.2.4 / etc / php.d \
--with-apxs2 = / usr / local / apache / bin / apxs \
- 启用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 \
- 启用shmop \
--enable-exif \
--enable-sysvsem \
- 启用内联优化
--with-curl = / usr / local \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
- 启用套接字
--with-xmlrpc \
--enable-ftp \
--enable-intl \
--with-xsl \
--with-gettext \
- 支持拉链
- 启用肥皂
- 禁用调试
稍等一会,等配置执行完毕后,再接着编译和安装命令,执行:
$make
$make install
这个过程非常耗时,需要耐心等待。
这里会报一个错误:
collect2: error: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1
报错提示缺少libiconv的这个库,缺啥就装啥。
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
tar xf libiconv-1.15.tar.gz
cd libiconv-1.15/
./configure --prefix=/usr/local/libiconv
make && make install
安装完后需要在配置时加入
--with-iconv=/usr/local/libiconv \
参数,然后再次编译就不会报错了。
使用全路径查看PHP版本:
$ /usr/local/php7.2.4/bin/php -v
PHP 7.2.4 (cli) (built: Apr 8 2018 14:33:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
修改环境变量,改为新版本路径,然后需要重启服务器。
$ vim /etc/profile
现在 php -v 就可以看到新版本了。