Nginx+FastCgi配置过程

 

   Nginx目前是性能较高的Web服务器,支持负载功能,并且可以进行扩展开发,占用资源也比Apache小。

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

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

Nginx+FastCgi的模式已经成为Web应用的最常见的模式,而PHP快速开发的突出优势,促成Nginx+FastCgi+PHP成为主流。

 

Nginx

1. 下载、编译,编译参数如下,注意要支持SSL协议,并指定openssl库。

 

   ./configure --sbin-path=/usr/sbin --conf-path=/etcl/nginx --error-log-path=/tmp/nginx --pid-path=/tmp/nginx --lock-path=/tmp/

nginx --with-http_ssl_module --with-openssl=/root/threepart/openssl-0.9.8o/

 

注意在编译的过程中会提示以下警告,不理会:

objs/src/os/unix/ngx_process.o(.text+0x6cf): In function `ngx_process_get_status':

src/os/unix/ngx_process.c:492: `sys_errlist' is deprecated; use `strerror' or `strerror_r' instead

objs/src/os/unix/ngx_process.o(.text+0x6c6):src/os/unix/ngx_process.c:492: `sys_nerr' is deprecated; use `strerror' or `strerror_r' instead

编译之后strip一下nginx,否则放上设备的时候可能空间不足。

2.  放在设备上的/usr/sbin,同时创建以下目录

/usr/local/nginx/

/usr/local/nginx/logs/

/usr/local/nginx/client_body_temp

3.  nginx的配置文件放上设备,我的配置文件目录是/etc/conf,需要针对nginx.conf做以下配置:

     listen       1001;

     location / {

            root   /usr/sbin/html;

            index  index.html index.htm;

        }

4.  放开防火墙端口,即可以通过1001端口去访问/usr/sbin/html下面的静态文件

iptables -A INPUT -p tcp -i eth0 --dport 1001 -j ACCEPT

 

 启动nginx/usr/sbin/nginx -c /etc/conf/nginx.conf

 

Spawn-FCGI

   Spawn-FCGI是一个通用的FastCGI管理服务器,曾经是lighttpd中的一部份,现在已经一个独立的工程。

1. 下载、编译即可。configure->make->strip。后面将作为运行php-cgi的管理器。

2.  Spawn-FCGI常用的参数说明

/usr/sbin/spawn-fcgi -a 127.0.0.1 -p 9002 -u nobody -g nobody -C 1 -f /usr/sbin/hello.cgi

-a 绑定的主机地址,可以绑定到其他主机。

-p 绑定的端口,与nginx.conf的配置约定好,其实就是通过socket做进程间通信。

-u 运行的用户id

-g 运行的组id

-C 启动的进程数,普通的CGI都是1

-f  运行的CGI文件,如php5php-cgi,fastcgi的测试例子hello.cgi

 

PHP-CGI

   PHP5中,php-cgiPHPFastCGI程序,同时也可以当作本身的FastCGI的管理程序。我使用之前运行在boaphp-cgi,并不需要重新做编译,即可用上面的Spawn-FCGI来运行。但需要改一下/usr/local/lib/php.ini的配置,将cgi.fix_pathinfo启用,见下面:

 

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's

; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok

; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting

; this to 1 will cause PHP CGI to fix it's paths to conform to the spec.  A setting

; of zero causes PHP to behave as before.  Default is zero.  You should fix your scripts

; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.

cgi.fix_pathinfo=1

 

1. Spawn-FCGI来运行php-cgi,约定端口用9000

/usr/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nobody -g nobody -C 5 -f /usr/sbin/cgi-bin/php-cgi

   nginx.conf需要做以下的改动:

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        location ~ /.php$ {

            root           /usr/sbin/html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi_params;

        }

2. 另外fastcgi_params 需要加SCRIPT_FILENAME一行,不然运行不了PHP脚本,这也是让初次配置的人纠结的地方。

fastcgi_param  QUERY_STRING       $query_string;

fastcgi_param  REQUEST_METHOD     $request_method;

fastcgi_param  CONTENT_TYPE       $content_type;

fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

FastCGI开发包

    普通的cgi,如果不按照fastcgi的格式来写,同样也是运行不起来,所以需要引入FastCGI开发包,主要改动是在main函数的地方,加上代码:

#include <fcgi_stdio.h>

int main( int argc, char *argv[] )

{

    while( FCGI_Accept() >= 0 ) {

        处理代码;

    }

}

1. 安装FastCGI也简单configure->make->make install即可。

如附件的hello.c,直接引入库和头文件编译即可。

gcc -o hello hello.c  -I/root/zhk/fcgi-2.4.0/include -L/root/zhk/fcgi-2.4.0/libfcgi/.libs/ -lfcgi

 

2. nginx.conf需要加上运行hello.cgi的配置,约定端口

            location ~ /hello.cgi$ {

            fastcgi_pass   127.0.0.1:9001;

            fastcgi_param  SCRIPT_FILENAME  /usr/sbin$fastcgi_script_name;

            include        fastcgi.conf;

        }

3. 启动hello.cgi

   /usr/sbin/spawn-fcgi -a 127.0.0.1 -p 9001 -u nobody -g nobody -C 1 -f /usr/sbin/hello.cgi


 

结果

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值