怎样编译安装PHP5.6.*

2018年1月27日 | 分类: 【技术】

【介绍】

在2018年12月31日之后,PHP 5.6.x的官方安全支持将正式停止。

参考:https://amon.org/php56

【安装】

下载:http://php.net/downloads.php

最新版本:php-5.6.40

wget http://php.net/distributions/php-5.6.40.tar.xz && tar -xvf php-5.6.40.tar.xz && cd php-5.6.40
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=apache --with-fpm-group=apache --disable-debug --with-curl --with-iconv --with-pcre-regex --with-zlib --enable-bcmath --enable-ftp --enable-sockets --enable-mbstring --enable-mbregex --enable-soap --with-xsl --with-openssl --enable-opcache --with-mysql --with-mysqli --with-mysql-sock --with-pdo-mysql --enable-mysqlnd --disable-fileinfo --with-gd=/usr/local/gd --enable-gd-jis-conv --with-jpeg-dir=/usr/local/jpeg/lib --with-png-dir=/usr/local/png --with-freetype-dir=/usr/local/freetype --with-libzip=/usr/local/libzip --enable-zip --with-iconv-dir=/usr/local/lib --with-readline --with-apxs2=/usr/local/apache2/bin/apxs LDFLAGS="-L/usr/lib -lz"
make ZEND_EXTRA_LIBS='-liconv'
make install

运行:

libtool --finish /root/php-5.6.40/libs
chmod 755 /usr/local/apache2/modules/libphp5.so
/root/php-5.6.40/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar

创建软连接:

ln -s /usr/local/php/bin/php /usr/local/bin/
ln -s /usr/local/php/bin/phpize /usr/local/bin/
ln -s /usr/local/php/bin/php-config /usr/local/bin/

查看版本:

php -v

输出:

PHP 5.6.40 (cli) (built: Jan  9 2019 03:04:35)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

重启Apache:

service httpd stop
service httpd start

【排错】

如果make时报错:undefined reference to `gdFontCacheShutdown’

参考:https://www.twblogs.net/a/5b88b8832b71775d1cde003b/zh-cn
参考:http://bbs.51cto.com/archiver/tid-825678.html

编译 PHP , make 时报错:

undefined reference to `gdFontCacheShutdown'

暂时不知什么原因导致此。

注释掉 /root/php-5.6.40/ext/gd/gd.c 中的:

	gdFreeFontCache();

然后 make 通过,make install 通过。查看 PHP 的 phpinfo ,GD库已正常加载。

如果configure时报错:Cannot find ldap libraries

configure: error: Cannot find ldap libraries in /usr/lib

参考:http://www.111cn.net/sys/linux/44746.htm

提示在/usr/lib 下找不到相关模块,这是因为64位的linux默认把以上文件都存在 /usr/lib64 文件夹下。

cp -frp /usr/lib64/libldap* /usr/lib/

如果configure时报错:off_t undefined

checking size of off_t… 0
configure: error: off_t undefined; check your library configuration

参考:https://stackoverflow.com/questions/44941513/php-compile-off-t-undefined
参考:https://stackoverflow.com/questions/44941513/php-compile-off-t-undefined
参考:http://ju.outofmemory.cn/entry/319609

添加搜索路径到配置文件:

echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf

更新配置:

ldconfig -v
ln -s -f /usr/include/zipconf.h /usr/local/include/zipconf.h

make时报错:#error unsupported size of off_t

在make的时候又出现了编译错误:

In file included from /home/data/php-5.6.38/ext/zip/lib/zip_add.c:36:0:
/home/data/php-5.6.38/ext/zip/lib/zipint.h:126:2: error: #error unsupported size of off_t
 #error unsupported size of off_t
  ^
make: *** [ext/zip/lib/zip_add.lo] Error 1

https://bugs.php.net/bug.php?id=68302找到了答案:

执行命令:

export CFLAGS="-L/opt/xml2/lib"

然后清除上一次编译的错误:

make clean

重新执行configure。

如果make时报错:undefined reference to symbol ‘ber_scanf’

/usr/bin/ld: ext/ldap/.libs/ldap.o: undefined reference to symbol 'ber_scanf'
/usr/bin/ld: note: 'ber_scanf' is defined in DSO /lib64/liblber-2.4.so.2 so try adding it to the linker command line
/lib64/liblber-2.4.so.2: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1

参考:http://www.oschina.net/question/56833_128548?sort=time

编辑MakeFile,找到 110行 开头是 ‘EXTRA_LIBS’ 这一行 在结尾加上 ‘-llber’ :

EXTRA_LIBS = -lcrypt -lcrypto -lssl -lcrypto -lzip -lz -lexslt -lcrypt -lreadline -lncurses -lrt -lmcrypt -lltdl -lldap -lstdc++ -lgmp -lgd -lpng -lz -ljpeg -lpng -lz -ljpeg -lcurl -lbz2 -lz -lcrypto -lssl -lcrypto -lrt -lm -ldl -lnsl -lxml2 -lz -lm -ldl -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lcurl -lxml2 -lz -lm -ldl -lfreetype -lfreetype -ldl -lm -licui18n -licuuc -licudata -ldl -lm -licuio -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lcrypt -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxslt -lxml2 -lz -ldl -lm -lcrypt -llber

重新make(注意不能make clean,因为这样的话MakeFile会还原):

make ZEND_EXTRA_LIBS='-liconv'

输出:

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

如果Apache启动时报错:/etc/init.d/httpd: line 81: 3668 Segmentation fault $HTTPD -k $ARGV

原因在于Apache配置文件中模块libphp5.so还在加载中,但是5已经被删除了,新安装了7,所以需要将libphp5.so这一行注释掉,只加载libphp7.so。

打开httpd.conf文件:

/usr/local/apache2/conf/httpd.conf

将libphp5.so这一行注释掉:

#LoadModule php5_module modules/libphp5.so
LoadModule php7_module modules/libphp7.so

重启Apache。

通过源码编译安装zlib/libzip,必须在configure中指定以下两个参数的路径:

--with-zlib-dir=DIR
--with-libzip=DIR

否则会出现报错。

PHP7中不需要–with-mysql

中间只报告了一个mysql无法扩展无法安装。

因为PHP7已经移除mysql了,现在主要使用mysqli和pdo连接mysql。

在configure 命令中 删除 –with-mysql 这一项即可。

备注2:–enable-opcache=no

使用大于php5.4版本的时候–enable-opcache可能会在./configure阶段就报错:

configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no

这句话的意思是让你关闭opcache,但是既然要使用新版本为何不开启opcache?执行下面这2句:

echo "/usr/local/lib">> /etc/ld.so.conf
ldconfig -v

以上操作是更新动态库,只有这样设置之后才能安装opcache选项。