centos7下源码编译安装php7.1最新版本

1.安装前准备

1.检查是否安装了gcc ,没有的话执行yum install gcc

2.检查是否安装了libxml2 ,没有的话执行yum install libxml2

3.检查是否安装了libxml2-devel,没有的话执行yum install libxml2-devel

4.检查是否安装了openssl,没有的话执行yum install openssl openssl-devel

5.curl安装 yum -y install curl-devel

6.图片库安装 yum install libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 -y

7.图片库安装 yum install libjpeg-devel

8.解压软件安装 yum install bzip2-devel.x86_64 -y

9.yum install libXpm-devel 10.yum install gmp-devel

10.yum install -y icu libicu libicu-devel

11.yum install php-mcrypt libmcrypt libmcrypt-devel

12.yum install postgresql-devel

13.yum install libxslt-devel

2.下载php7.1最新版,并解压

下载php7.1.22

1
2
3
4
5
mkdir -p /opt/tools/php
cd /opt/tools/php
wget http://cn2.php.net/distributions/php-7.1.22.tar.gz

解压

1
2
3
tar -xzxvf php-7.1.22.tar.gz
cd php-7.1.22

3.编译参数配置

‘./configure’ ‘–prefix=/usr/local/php’ ‘–with-pdo-pgsql’ ‘–with-zlib-dir’ ‘–with-freetype-dir’ ‘–enable-mbstring’ ‘–with-libxml-dir=/usr’ ‘–enable-soap’ ‘–enable-calendar’ ‘–with-curl’ ‘–with-mcrypt’ ‘–with-gd’ ‘–with-pgsql’ ‘–disable-rpath’ ‘–enable-inline-optimization’ ‘–with-bz2’ ‘–with-zlib’ ‘–enable-sockets’ ‘–enable-sysvsem’ ‘–enable-sysvshm’ ‘–enable-pcntl’ ‘–enable-mbregex’ ‘–enable-exif’ ‘–enable-bcmath’ ‘–with-mhash’ ‘–enable-zip’ ‘–with-pcre-regex’ ‘–with-pdo-mysql’ ‘–with-mysqli’ ‘–with-jpeg-dir=/usr’ ‘–with-png-dir=/usr’ ‘–enable-gd-native-ttf’ ‘–with-openssl’ ‘–with-fpm-user=www-data’ ‘–with-fpm-group=www-data’ ‘–with-libdir=/lib/x86_64-linux-gnu/‘ ‘–enable-ftp’ ‘–with-gettext’ ‘–with-xmlrpc’ ‘–with-xsl’ ‘–enable-opcache’ ‘–enable-fpm’ ‘–with-iconv’ ‘–with-xpm-dir=/usr’

如果出现错误:No package libmcrypt-devel available.
解决办法:
1.yum install epel-release //扩展包更新包
2.yum update //更新yum源

出现报错 Cannot find OpenSSL’s
则执行 yum install openssl openssl-devel

出现报错 Please reinstall the libcurl distribution
则执行 yum -y install curl-devel

出现报错 jpeglib.h not found
则执行 yum install libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 -y
和执行yum install libjpeg-devel

checking for BZip2 in default path… not found
configure: error: Please reinstall the BZip2 distribution
这是bzip2软件包没有安装
解决办法
yum install bzip2-devel.x86_64 -y

configure: error: xpm.h not found.
yum install libXpm-devel

error: Unable to locate gmp.h
Fix: yum install gmp-devel

现象:Unable to detect ICU prefix or /usr//bin/icu-config failed. Please verify ICU install
prefix and make sure icu-config works
解决办法:yum install -y icu libicu libicu-devel

错误:mcrypt.h not found. Please reinstall libmcrypt.
解决办法:yum install php-mcrypt libmcrypt libmcrypt-devel

错误: configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path
解决办法:yum install postgresql-devel

错误 : configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
解决: yum install libxslt-devel

3.编译和安装

配置的checking 结束后,执行:
make && make install

4、php配置

安装完成后,我们要把源码包中的配置文件复制到PHP安装目录下,源码包中有两个配置 php.ini-development php.ini-production ,看名字就知道,一个是开发环境,一个是生产环境,我们这里就复制开发环境的
cp php.ini-development /usr/local/php/lib/php.ini

另外还需要设置环境变量 :
修改/etc/profile文件使其永久性生效,并对所有系统用户生效,在文件末尾加上如下两行代码
PATH=$PATH:/usr/local/php/bin
export PATH
然后执行 命令 source /etc/profile

php -v 就可以看到PHP版本信息了。

4、php-fpm配置

此时还需要配置php-fpm:

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

启动php-fpm:
/etc/init.d/php-fpm start

如果出现错误:ERROR: [pool www] cannot get uid for user ‘www-data’
则新建www-data 用户组:
groupadd www-data
useradd -g www-data www-data

然后再启动php-fpm:
service php-fpm restart

5、php-fpm参数优化

vi /usr/local/php/etc/php-fpm.d/www.conf

1.进程数

php-fpm初始/空闲/最大worker进程数
pm.max_children = 300
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

2.最大处理请求数

最大处理请求数是指一个php-fpm的worker进程在处理多少个请求后就终止掉,master进程会重新respawn一个新的。
这个配置的主要目的是避免php解释器或程序引用的第三方库造成的内存泄露。
pm.max_requests = 10240

3.最长执行时间

最大执行时间在php.ini和php-fpm.conf里都可以配置,配置项分别为max_execution_time和request_terminate_timeout

6、Unix域Socket通信

之前简单介绍过Unix Domain Socket这种通信方式,参见:Nginx+PHP-FPM的域Socket配置方法

Unix域Socket因为不走网络,的确可以提高Nginx和php-fpm通信的性能,但在高并发时会不稳定。
Nginx会频繁报错:
connect() to unix:/dev/shm/php-fcgi.sock failed (11: Resource temporarily unavailable) while connecting to upstream

可以通过下面两种方式提高稳定性:
1)调高nginx和php-fpm中的backlog
配置方法为:在nginx配置文件中这个域名的server下,在listen 80后面添加default backlog=1024。
同时配置php-fpm.conf中的listen.backlog为1024,默认为128。
2)增加sock文件和php-fpm实例数
再新建一个sock文件,在Nginx中通过upstream模块将请求负载均衡到两个sock文件背后的两套php-fpm实例上。