CGI http服务器与其他服务器通信的一种工具
传统CGI缺点:每次http服务器遇到动态数据都要重启解析器,产生结果再返回
FastCGI
既是socket,主要优点将动态语言与http服务器分离开
可以启动多个FastCGI的守护进程(php-fpm)
一般情况下,FastCGI的整个工作流程是这样的:
1、Web Server启动时载入FastCGI进程管理器(IIS ISAPI或Apache Module)
2、FastCGI进程管理器自身初始化,启动多个CGI解释器进程(可见多个php-cgi)并等待WebServer的连接。
3、当客户端请求到达Web Server时,FastCGI进程管理器选择并连接到一个CGI解释器。 Web server将CGI环境变量和标准输入发送到FastCGI子进程php-cgi。
4、FastCGI子进程完成处理后将标准输出和错误信息从同一连接返回Web Server。当FastCGI子进程关闭连接时,请求便告处理完成。FastCGI子进程接着等待并处理来自FastCGI进程管理器(运行在Web Server中)的下一个连接。在CGI模式中,php-cgi在此便退出了。
在本文中讲解下PHP(FastCGI)服务的安装
1.安装php(FastCgi )所需的lib
php 需要安装的lib 有很多,其中libiconv无法通过yum 安装
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make
make install
其他通过yum 安装即可
yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel openssl-devel
yum -y install freetype-devel libpng-devel libcurl-devel libxslt-devel gd-devel
yum -y install mhash mcrypt libmcrypt-devel
二进制安装Php
rz上传本地或下载二进制安装包
二进制包(认系统)
mv /php/php-5.6.9 /usr/local/php-5.6.9
ln -s /usr/local/php-5.6.9 /usr/local/php
源码包
tar xf php-5.5.32.tar.gz
cd php-5.5.32
./configure –prefix=/application/php5.5.32 –with-mysql=mysqlnd –with-pdo-mysql=mysqlnd –with-iconv-dir=/usr/local/libiconv –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –disable-rpath –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –enable-mbregex –enable-fpm –enable-mbstring –with-mcrypt –with-gd –enable-gd-native-ttf –with-openssl –with-mhash –enable-pcntl –enable-sockets –with-xmlrpc –enable-soap –enable-short-tags –enable-static –with-xsl –with-fpm-user=www –with-fpm-group=nginx –enable-ftp –enable-opcache=no –with-gettext –with-mysqli=mysqlnd
启动php-fpm
/usr/local/php/sbin/php-fpm
配置nginx 支持php-fpm
nginx.conf
worker_processes 4;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /application/nginx/logs/access.log main;
(缓存区配置可暂时忽视)
server_names_hash_max_size 512;
server_names_hash_bucket_size 128;
tcp_nopush on;
keepalive_timeout 65;
client_header_timeout 15;
client_body_timeout 15;
send_timeout 60;
client_max_body_size 8m;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key http://$host$request_uri;
include /application/nginx/conf/hjy.conf;
hjy.conf
server {
listen 80;
server_name www.xxx.com;
###必须存在
root html/xiaohuang;
location / {
index index.php index.html index.htm;
}
###配置核心(使nginx支持fpm)
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location模块(可暂时忽视)
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 3650d;
}
location ~ .*\.(js|css)?$ {
expires 365d;
}
location ~ ^/images/.*\.(php|php5|sh|pl|py)$ {
deny all;
}
location ~ ^/static/.*\.(php|php5|sh|pl|py)$ {
deny all;
}
location ~ ^/data/.*\.(php|php5|sh|pl|py)$ {
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
重启nginx
/application/nginx -t
/application/nginx/sbin/nginx -s reload