zoukankan      html  css  js  c++  java
  • Nginx与PHP(FastCGI)的安装、配置与优化

    FastCGI的介绍和工作原理

      首先简单的介绍下FastCGI:

      FastCGI是语言无关的、可伸缩结构的CGI开放扩展,其主要行为是将CGI解释器进行保持在内存中并因此获得较高的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail-Over特性等。

      FastCGI的工作原理是:

      (1)FastCGI进程管理器自身初始化,启动多个CGI解释器进程(多个php-cgi进程)并等待来自Web Server的连接。在文本中,采用PHP-FPM进程管理器启动多个php-cgi FastCGI进程。启动php-cgi FastCGI进程时,可以配置以TCP和UNIX套接字两种方式启动。

      (2)当客户端请求达到Web服务器(Nginx)时,Web服务器将请求采用TCP协议或UNIX套接字方式转发到FastCGI主进程,FastCGI主进程选择并连接到一个CGI解释器(子进程)。Web服务器将CGI环境变量和标准输入发送到FastCGI子进程php-cgi。

      (3)FastCGI子进程完成处理后将标准输出和错误信息从同一连接返回Web服务器(Nginx)。当FastCGI子进程关闭连接时,请求便告知处理完成。FastCGI子进程接着等待并处理来自FastCGI进程管理的下一个连接。而在一般的普通CGI模式中,php-cgi在此便退出了。

    PHP-FPM

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

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

      安装前准备:

    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-fpm安装(推荐安装方式)

    wget http://us1.php.net/get/php-5.5.10.tar.gz/from/this/mirror
    tar zvxf php-5.5.10.tar.gz
    cd php-5.5.10
    ./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
    
    make all install
    

      完成php-fpm后,对其运行用户进行配置:

    cd /usr/local/php
    cp etc/php-fpm.conf.default etc/php-fpm.conf
    vi etc/php-fpm.conf
    
    修改:
    user = nginx
    group = nginx
    
    如果nginx用户不存在,那么先添加nginx用户
    groupadd nginx
    useradd -g nginx nginx
    

      修改nginx配置文件以支持php-fpm

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    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文件

      在/usr/local/nginx/html下创建index.php文件,输入如下内容

    <?php
        echo phpinfo();
    ?>
    

      启动php-fpm和nginx

    /usr/local/php/sbin/php-fpm 
    /usr/local/nginx/nginx
    

      访问http://你的服务器ip/index.php,皆可以见到php信息了。

  • 相关阅读:
    寒假作业:第三次作业
    markdown笔记
    c#基类继承
    atom插件安装
    git命令
    vue2.3时使用手机调试,提示媒体已断开的解决方案
    vue中使用hotcss--stylus
    JS调试工具
    Facebook的bigpipe
    xss--攻击方式
  • 原文地址:https://www.cnblogs.com/txtfashion/p/3669524.html
Copyright © 2011-2022 走看看