web开发环境搭建第三篇CentOS7.3编译安装php7.1.6

 

下载地址

 

http://cn2.php.net/distributions/php-7.1.6.tar.gz

 

删除centos自带的php

[root@localhost src]# rpm -qa|grep php
php-process-5.4.16-42.el7.x86_64
php-pear-1.9.4-21.el7.noarch
php-common-5.4.16-42.el7.x86_64
php-xml-5.4.16-42.el7.x86_64
php-devel-5.4.16-42.el7.x86_64
php-cli-5.4.16-42.el7.x86_64
[root@localhost src]# rpm -e php-devel-5.4.16-42.el7.x86_64
一个一个的删除完

 

 

 

安装一些必要的工具和库

yum -y install libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel gcc-c++ autoconf libXpm-devel

 

安装libmcrypt

https://sourceforge.net/projects/mcrypt/files/Libmcrypt/

 

[root@localhost src]# wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz

[root@localhost src]# tar -zxf libmcrypt-2.5.8.tar.gz

[root@localhost src]# ​cd libmcrypt-2.5.8

[root@localhost libmcrypt-2.5.8]#./configure

[root@localhost libmcrypt-2.5.8]# make && make install

 

编译安装PHP

编译有点慢

 

[root@localhost src]# wget http://cn2.php.net/distributions/php-7.1.6.tar.gz

[root@localhost src]# tar -zxf php-7.1.6.tar.gz

[root@localhost src]# cd php-7.1.6

[root@localhost php-7.1.6]# ./buildconf --force

[root@localhost php-7.1.6]# ./configure \
--prefix=/usr/local/php \
--exec-prefix=/usr/local/php \
--with-pdo-mysql=mysqlnd \
--with-mysql-sock=/tmp/mysql.sock \
--with-gd \
--with-png-dir \
--with-jpeg-dir \
--enable-gd-native-ttf \
--with-freetype-dir \
--with-xpm-dir=/usr/lib \
--with-zlib \
--with-iconv \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--enable-opcache \
--enable-pcntl \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-mcrypt \
--with-curl \
--with-mhash
[root@localhost php-7.1.6]# make
[root@localhost php-7.1.6]# make install

 

配置php.ini

一、查看php.ini配置文件的路径​

 

[root@localhost php-7.1.6]# /usr/local/php/bin/php --ini

Configuration File (php.ini) Path: /usr/local/php/lib

显示在lib下面,所有我们应该拷贝源码包里面配置文件到/usr/local/php/lib下面

[root@localhost php-7.1.6]# cp php.ini-production /usr/local/php/lib/php.ini

 

二、配置php.ini

 

A.关闭在http头中显示php版本信息

 

expose_php = Off

 

B.错误的处理

 

error_reporting = E_ALL​ & ~E_NOTICE

display_errors = Off

log_errors = On

error_log = /data/log/php/php_errors.log

 

注意:一定要保证php_errors.log有可写权限,有两种方式:

 

下面我们会配置php-fpm运行的用户和用户组为nginx,所以,我们这里可以将php_errors.log的所有者和所属组改成nginx

mkdir -p /data/log/php
touch /data/log/php/php_errors.log
chown nginx:nginx /data/log/php/php_errors.log

 

C. 设置时区

 

date.timezone = PRC

linux的配置是不用去开启curl、mysql之类的扩展的,configure配置编译的时候已经默认开启了

 

配置php-fpm

一、创建日志目录

 

mkdir -p /data/log/php-fpm

 

二、php-fpm.conf

 

[root@localhost php-7.1.6]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf


① 去掉;pid = run/php-fpm.pid  前面的分号,使之生效

② 设置错误日志路径

error_log = /data/log/php-fpm/php-fpm.log

 

三、www.conf

 

[root@localhost php-7.1.6]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

;设置用户和用户组
user = nginx
group = nginx

;设置监听
;listen = 127.0.0.1:9000
listen = /dev/shm/php-fpm.sock

listen.owner = nginx​
listen.group = nginx
listen.mode = 0660

;进程相关,这个得根据硬件情况来设置
pm.max_children = 80
pm.start_servers = 30
pm.min_spare_servers = 20
pm.max_spare_servers = 50

;开启慢日志
slowlog = /data/log/php-fpm/$pool-slow.log
request_slowlog_timeout = 10s

;开启抓去php的错误输出
catch_workers_output = yes

 

原因是php-fpm的配置文件php-fpm.conf中默认是关闭worker进程的错误输出,直接把他们重定向到/dev/null,所以我们在nginx的error log 和php-fpm的errorlog都看不到php的错误日志,直接开启catch_workers_output = yes 即可

 

检测php-fpm配置文件

 

[root@localhost php-7.1.6]# /usr/local/php/sbin/php-fpm -t

[30-Mar-2017 12:20:36] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful

 

设置php-fpm开机自启动

 

[root@localhost php-7.1.6]# cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

[root@localhost php-7.1.6]# chmod 0744 /etc/init.d/php-fpm
[root@localhost php-7.1.6]# chkconfig --add php-fpm
[root@localhost php-7.1.6]# chkconfig --level 35 php-fpm on

chkconfig --list 来查看是否将php-fpm添加到了自启动​

 

启动php-fpm

方式一:

 

[root@localhost php-7.1.6]# /usr/local/php/sbin/php-fpm

 

方式二:

 

当然,第3步我们已经把php-fpm加入了开机自启动,也就是加入了服务,所有,我就可以用这样的命令来控制php-fpm了:

 

[root@localhost php-7.1.6]# service php-fpm start|restart|stop|reload|status|force-quit

 

方式三:

 

[root@localhost php-7.1.6]# /etc/init.d/php-fpm start|restart|stop|reload|status|force-quit

 

配置nginx支持php脚本

 

Nginx + PHP-FPM设置虚拟主机 + 开启PATHINFO模式

 

 

 


安装mysqlnd phpize cd /usr/local/src/php7.1.6/ ./ext/mysqlnd/configure --with-php-config=/usr/loal/php/bin/php-config export PHP_OPENSSL_DIR=yes make make install