教你编译PHP7 (nginx+mysql+php7)

布尔教育 PHP教研组 著
http://www.itbool.com
转载请保留版权

PHP7正式版已经发布,性能是PHP5.6的两倍!
PHP大狮班的课堂上,已经开始使用PHP7!   QQ群:316656955

操作系统: CentOS Linux, 6.5 64位
服务器: 阿里云
空的操作系统,我们从0开始.
在开始前,请确保你的Linux已联网,已联网,已联网!


# 安装编译工具:
yum install gcc automake autoconf libtool gcc-c++

# 安装基础库
yum install gd zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt libmcrypt-devel

# 安装mysql
yum install mysql mysql-server

# 下载pcre 正则库
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.bz2
tar jxf pcre-8.37.tar.bz2

#编译nginx
cd /usr/local/src

wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxf nginx-1.8.0.tar.gz

cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.37
make && make install

# 编译PHP
cd /usr/local/src
wget http://cn2.php.net/get/php-7.0.0.tar.bz2/from/this/mirror
mv mirror php-7.0.0.tar.bz2
tar jxf php-7.0.0.tar.bz2

cd php-7.0.0

./configure --prefix=/usr/local/php \
--with-gd \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-mcrypt \
--enable-mbstring \
--enable-zip \
--enable-fpm

make && make install

# 复制配置文件
cp /usr/local/src/php-7.0.0/php.ini-development /usr/local/php/lib/php.ini
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


# 整合nginx+php
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
    include        fastcgi_params;
}

# 启动php 和nginx
/usr/local/php/sbin/php-fpm
/usr/local/nginx/sbin/nginx

写测试PHP页面


<?php
# /usr/local/nginx/html/a.php
phpinfo();

打开浏览器: http://xxxxxx/a.php

让Nginx支持pathinfo

Nginx服务器默认不支持pathinfo, 在需要pathinfo支持的程序中(如thinkphp),则无法支持”/index.php/Home/Index/index”这种网址.

网上流传的解决办法很多,这里提供一种比较简洁的写法(只需要改动2行代码)


# 典型配置
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
    include        fastcgi_params;
}

# 修改第1,6行,支持pathinfo

location ~ \.php(.*)$ { # 正则匹配.php后的pathinfo部分
    root html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
    fastcgi_param PATH_INFO $1; # 把pathinfo部分赋给PATH_INFO变量
    include        fastcgi_params;
}

Nginx+php-fpm 502 504问题

近期自学it论坛(www.zixue.it) 常出现502,504错误

环境:

主机: 阿里云 双核CPU 2G内存
OS: centos 6.4 64位

思路:

  • 增大nginx与php-fpm通信时的缓存文件大小与个数
  • 静态绑定5个php-fpm进程,减少频率启动进程的开销
  • 利用sock文件代替tcp端口通信(本地机约可提高10%的性能)

解决办法:

调整Nginx 和 php-fpm的参数如下


# nginx.conf location段
unix:/usr/local/php/var/run/php-fpm.sock

# fastcgi.conf 修改内容
fastcgi_buffers 32 128k;
fastcgi_buffer_size 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;

#php-fpm.conf
listen = /usr/local/php/var/run/php-fpm.sock
#提醒:别忘给php-fpm.sock加读写权限

pm = static # 静态绑定5个进程
pm.max_children = 5

效果:

论坛首页速度快了约30毫秒,且经过2周的临床观察,未再发生502 ,504错误