PHP7编译升级及问题解决

2019年12月11日15:49:35 1 1,016

PHP 7+ 版本极大地改进了性能,在一些WordPress基准测试当中,性能可以达到PHP 5.6的3倍。而PHP 5.6版本在2018年底官方已经不再维护,升级PHP 7是必然选择。

一、编译安装

1、下载php7

$ wget http://cn2.php.net/distributions/php-7.0.0.tar.gz

2、解压编译

因为机器上已经安装了PHP的5.6或更老的版本,所以我们可以编译安装到一个新的路径,到时候用软连接的方式生效

$ tar -zxvf php-7.0.0.tar.gz

$ cd php-7.0.0

$ CFLAGS= CXXFLAGS= ./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc \
--with-fpm-user=www --with-fpm-group=www --enable-fpm --enable-opcache \
--disable-fileinfo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-jpeg-dir \
--with-iconv-dir=/usr/local/libiconv-1.16 --with-freetype-dir --with-png-dir --with-zlib --disable-rpath \
--with-libxml-dir=/usr --enable-xml --enable-bcmath --enable-shmop --enable-exif --with-curl \
--enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-inline-optimization \
--enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl \
--with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-ftp \
--with-gettext --enable-zip --enable-soap --disable-ipv6 --disable-debug

#继续make
make ZEND_EXTRA_LIBS='-liconv'

#最后make install完成安装
make install

3、设置参数

在php-7.0.0路径下创建一个脚本,添加好下面内容后,运行下脚本即可生成php.ini文件

#!/bin/bash

php_install_dir=/usr/local/php7
cp php.ini-production $php_install_dir/etc/php.ini
Mem=`free -m | awk '/Mem:/{print $2}'`
if [ $Mem -gt 1024 -a $Mem -le 1500 ];then
Memory_limit=192
elif [ $Mem -gt 1500 -a $Mem -le 3500 ];then
Memory_limit=256
elif [ $Mem -gt 3500 -a $Mem -le 4500 ];then
Memory_limit=320
elif [ $Mem -gt 4500 ];then
Memory_limit=448
else
Memory_limit=128
fi
sed -i "s@^memory_limit.*@memory_limit = ${Memory_limit}M@" $php_install_dir/etc/php.ini
sed -i 's@^output_buffering =@output_buffering = On\noutput_buffering =@' $php_install_dir/etc/php.ini
sed -i 's@^;cgi.fix_pathinfo.*@cgi.fix_pathinfo=0@' $php_install_dir/etc/php.ini
sed -i 's@^short_open_tag = Off@short_open_tag = On@' $php_install_dir/etc/php.ini
sed -i 's@^expose_php = On@expose_php = Off@' $php_install_dir/etc/php.ini
sed -i 's@^request_order.*@request_order = "CGP"@' $php_install_dir/etc/php.ini
sed -i 's@^;date.timezone.*@date.timezone = Asia/Shanghai@' $php_install_dir/etc/php.ini
sed -i 's@^post_max_size.*@post_max_size = 50M@' $php_install_dir/etc/php.ini
sed -i 's@^upload_max_filesize.*@upload_max_filesize = 50M@' $php_install_dir/etc/php.ini
sed -i 's@^;upload_tmp_dir.*@upload_tmp_dir = /tmp@' $php_install_dir/etc/php.ini
sed -i 's@^max_execution_time.*@max_execution_time = 600@' $php_install_dir/etc/php.ini
sed -i 's@^;realpath_cache_size.*@realpath_cache_size = 2M@' $php_install_dir/etc/php.ini
sed -i 's@^disable_functions.*@disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen@' $php_install_dir/etc/php.ini
sed -i 's@^session.cookie_httponly.*@session.cookie_httponly = 1@' $php_install_dir/etc/php.ini
sed -i 's@^mysqlnd.collect_memory_statistics.*@mysqlnd.collect_memory_statistics = On@' $php_install_dir/etc/php.ini
[ -e /usr/sbin/sendmail ] && sed -i 's@^;sendmail_path.*@sendmail_path = /usr/sbin/sendmail -t -i@' $php_install_dir/etc/php.ini
#如果编译时去掉了 --enable-opcache,则以下包含opcache的都请忽略
sed -i 's@^\[opcache\]@[opcache]\nzend_extension=opcache.so@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.enable=.*@opcache.enable=1@' $php_install_dir/etc/php.ini
sed -i "s@^;opcache.memory_consumption.*@opcache.memory_consumption=$Memory_limit@" $php_install_dir/etc/php.ini
sed -i 's@^;opcache.interned_strings_buffer.*@opcache.interned_strings_buffer=8@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.max_accelerated_files.*@opcache.max_accelerated_files=4000@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.revalidate_freq.*@opcache.revalidate_freq=60@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.save_comments.*@opcache.save_comments=0@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.fast_shutdown.*@opcache.fast_shutdown=1@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.validate_timestamps.*@opcache.validate_timestamps=1@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.enable_cli.*@opcache.enable_cli=1@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.use_cwd.*@opcache.use_cwd=1@' $php_install_dir/etc/php.ini
sed -i 's@^opcache.max_accelerated_files.*@opcache.max_accelerated_files=100000@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.max_wasted_percentage.*@opcache.max_wasted_percentage=5@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.consistency_checks.*@opcache.consistency_checks=0@' $php_install_dir/etc/php.ini
sed -i 's@^;opcache.optimization_level.*@;opcache.optimization_level=0@' $php_install_dir/etc/php.ini

php-fpm直接沿用之前的配置即可

$ cp -f /usr/local/php/etc/php-fpm.conf /usr/local/php7/etc/

4、版本替换

使用软连接方式可以使两个版本同时存在,万一升级有问题还能切换回旧版本

#停止php-fpm
$ etc/init.d/php-fpm  stop

#重命名php老版本
$ cd /usr/local

$ mv php php5

#建立php7的软链接
$ ln -s php7 php

#启动php-fpm
$etc/init.d/php-fpm  start

启动完成后运行php -v可以查看php版本已经是7.0了

 

二、问题解决

1、configure: error: off_t undefined; check your library configuration

off_t 类型是在 头文件 unistd.h中定义的,在32位系统 编译成 long int ,64位系统则编译成 long long int ,这里题主的系统应该是 64位的吧,在进行编译的时候 是默认查找64位的动态链接库,但是默认情况下 centos 的动态链接库配置文件/etc/ld.so.conf里并没有加入搜索路径,这个时候需要将 /usr/local/lib64 /usr/lib64 这些针对64位的库文件路径加进去

# 添加搜索路径到配置文件
$ echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf
# 更新配置
$ ldconfig -v

2、 /usr/bin/ld: cannot find -liconv

意思是系统找不到iconv相应的lib库文件,也就是libiconv.so文件。会报这个错一般是系统没有安装libiconv或者libiconv.so的软链接文件不对

如果没有安装,使用编译安装,然后再用上面的php编译命令重新编译

$ wget -c https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz

$ tar -zxvf libiconv-1.16.tar.gz -C /usr/local

$ cd /usr/local/libiconv-1.16

$ ./configure --prefix=/usr/local/libiconv-1.16

$ make && make install

$ ln -s /usr/local/libiconv-1.16/lib/libiconv.so.2.6.1 /usr/lib/libiconv.so

$ ln -s /usr/local/libiconv-1.16/lib/libiconv.so.2 /usr/lib/libiconv.so.2

 

weinxin
聂扬帆博客
一个分享IT运维相关工作经验和实战技巧的个人博客

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  1   博主  0

    • avatar wordpress建站吧 4

      学习了