1、fastcgi介绍

         FastCGI:快速通用网关接口(Fast Common Gateway Interface/FastCGI)是一种让交互程序与Web服务器通信的协议。

         FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。

         FastCGI工作方式是接受Web服务器的请求,以HTTP Request的方式进行响应,实现了应用程序与Web服务器的分离。它的使用需要在Web服务器中安装支持组件。
         利用nginx、spawn-fcgi、fcgi就可搭建一个高并发高性能的FastCGI框架。

2、优点

         稳定性,fastcgi是以独立的进程池运行来cgi,单独一个进程死掉,系统可以很轻易的丢弃,然后重新分配新的进程来运行逻辑
         安全性,fastcgi和宿主的server完全独立,fastcgi怎么down也不会把server搞垮
         性能, fastcgi把动态逻辑的处理从server中分离出来, 大负荷的IO处理还是留给宿主server, 这样宿主server可以一心一意作IO,对于一个普通的动态网页来说,,逻辑处理可能只有一小部分,大量的图片等静态IO处理完全不需要逻辑程序的参与

         扩展性,fastcgi是一个中立的技术标准,完全可以支持任何语言写的处理程序(php、java、python、C++)

3、实验环境

          nginx服务器ip:192.168.11.100

          fastcgi服务器:192.168.11.7

          需要安装的软件包:nginx服务器:nginx-1.2.3   fastcgi服务器:php-5.4.34

4、安装nginx服务器

   #tar zxvf nginx-1.2.3.tar.gz
   #cd nginx-1.2.3
   #./configure --prefix=/usr/local/nginx --with-http_stub_status_module
   #make
   #make install
   #cd /usr/local/nginx/sbin
   #./nginx

    此时在浏览器中可以看到it's work的界面

5、安装fastcgi服务器

  #tar zxvf php-5.4.34.tar.gz
  #tar -zxvf php-5.4.34.tar.gz
  #cd php-5.4.34
  #./configure --prefix=/usr/local/php --enable-fastcgi --enable-fpm
  #make
  #make install

6、配置fastcgi服务器

[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice
emergency_restart_threshold = 60
emergency_restart_interval = 60
process_control_timeout = 0
daemonize = yes

[www]
user = nobody
group = nobody
listen = 192.168.11.7:9000

listen.allowed_clients = 192.168.11.100,127.0.0.1
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

   修改玩配置后重启fpm服务:/usr/local/php/sbin/php-fpm

7、修改nginx配置

# cat nginx.conf |grep -v '^$' |grep -v [#]
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   192.168.11.7:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

  配置完成后重启nginx服务:service nginx restart

8、至此,fastcgi配置已完成,在浏览器中输入http://192.168.11.100/test.php,就可以看到解析到php测试页了!