开发者社区> 问答> 正文

阿里云Linux下Nginx服务器整合php

听说镜像要收费了?不知是真是假?所以说自己动手丰衣足食,前几天主要说的一些Nginx和tomcat的基础知识以及集群的基础配置。


今天重点说一些php-fpm的安装配置。


nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。    

nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx 本文以php-fpm为例介绍如何使nginx支持PHP       


PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到. PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。     


新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM,其它参数都是配置php的,具体选项含义可以查看这里


一、安装前准备
centos下执行
yum -y install gcc automake autoconf libtool make


yum -y install gcc gcc-c++ glibc


yum -y install libmcrypt-devel mhash-devel 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



二、安装php
下载地址:http://php.net/downloads.php 可以选择适合你的版本、这里选择5.6.13。


1、wget http://cn2.php.net/distributions/php-5.6.13.tar.gz


2、tar zvxf php-5.6.13.tar.gz


3、cd php-5.6.13


4、./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt \
--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \
--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \
--with-gd --with-jpeg-dir --disable-fileinfo  


5、make


6、make  install

三、启动php服务五、启动服务
启动php-fpm
/usr/local/php/sbin/php-fpm start
检测是否启动 ps -ef|grep php,如果出现以下说明启动成功。





四、nginx整合php- fpm
新建 phpfpm.conf文件 加入:
fastcgi_connect_timeout 300; #指定连接到后端FastCGI的超时时间
fastcgi_send_timeout 300; #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
fastcgi_read_timeout 300; #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收#FastCGI应答的超时时间。
fastcgi_buffer_size 64k;#这里可以设置为fastcgi_buffers指令指定的缓冲区大小
fastcgi_buffers 16 16k; #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答
fastcgi_busy_buffers_size 128k;#建议为fastcgi_buffers的两倍
fastcgi_temp_file_write_size 128k; #在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍,设置上述数值设置太小时若负载上来时可能报 502 Bad Gateway
fastcgi_cache dingtm; #开启FastCGI缓存并且为其制定一个名称,有效降低CPU负载,并且防止#502错误
fastcgi_cache_valid 200 302 1h; #指定应答代码缓存时间为1小时
fastcgi_cache_valid 301 1d; #1天
fastcgi_cache_valid any 1m; #其它为1分钟
fastcgi_cache_min_uses 1; #缓存在fastcgi_cache_path指令inactive参数值时间内的最少使用次数
将phpfpm.conf导入nginx.conf的http模块中。
include  phpfpm.conf 最后导入 server
server {

        listen       80;
        server_name  weixin.52itstyle.com;
        charset utf8;
        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;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
新建 index.php文件 放到nginx的html目录下:

<?php
  date_default_timezone_set(  'Asia/Shanghai'  );
    echo phpinfo();?>



重启Nginx   ./nginx -s reload





如果出现以上画面 说明配置成功。

展开
收起
小柒2012 2015-10-16 20:33:39 11236 0
2 条回答
写回答
取消 提交回答
  • 一个程序员,欢迎骚扰!!!
    回 1楼(51干警网) 的帖子
    问句  哈哈 听说 也是
    2015-10-17 09:48:14
    赞同 展开评论 打赏
  • 不错的教程。镜像也要收费?太那啥了吧。
    2015-10-16 23:48:43
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-直面PHP微服务架构挑战-高驰涛 立即下载
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载