您的位置:首页 > 运维架构 > Nginx

nginx+fastcgi+c/c++源码安装配置

2014-01-28 11:20 375 查看
参考:/article/4980062.html

由于以前安装过apache,已经安装了很多依赖库,现在只需要安装以下软件包:

nginx-1.4.4.tar.gz

spawn-fcgi-1.6.3.tar.gz (fastcgi进程管理程序)

fcgi.tar.gz ( fastcgi库与头文件)

安装nginx

# tar -xvf ./nginx-1.4.4.tar.gz

# cd nginx-1.4.4

# ./configure --prefix=/usr/local/nginx --error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --lock-path=/var/lock/nginx.lock--pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module
--with-http_ssl_module

# make; make install

安装完毕,开启服务:

# /usr/local/nginx/sbin/nginx

浏览器中访问:
http://ip
出现欢迎页面,说明安装nginx成功

nginx详细配置这里不作介绍

安装spawn-fcgi

nginx是支持fastcgi的。然而我们需要下一个fastcgi进程管理器,启动它才能执行fastcgi程序。

这里有几个概念要搞清楚:nginx是nginx,fastcgi是fastcgi,前者支持后者,但是前者本身没有集成后者的功能)。对于ngingx,我们要配置conf.nginx来设置如何支持fastcgi。而对于fastcgi,我们写的fastcgi程序需要一个调度者:fastcgi进程管理器,即spawn-fcgi。——纯属个人理解。

这个spawn-fcgi就是fastcgi进程管理器。是lighttpd中的一个子项目。

# tar -xvf ./spawn-fcgi-1.6.3.tar.gz

# cd ./spawn-fcgi-1.6.3

# ./configure; make;

#cp ./src/spawn-fcgi /usr/local/nginx/sbin/

安装fastcgi库

# tar -xvf ./fcgi.tar.gz

# cd ./fcgi-2.4.1-SNAP-0311112127

# ./configure;

如果直接编译会报错,如下处理:

# vi ./include/fcgio.h

添加如下头文件:

#include <cstdio>

# make; make install

测试

# cd /usr/local/nginx

# mkdir fastcgitest

#cd fastcgitest
编写如下代码:

#include <stdio.h>
#include <fcgi_stdio.h>
#include <stdlib.h>
#include <unistd.h>

//using namespace std;

int main()
{
int count = 0;

while(FCGI_Accept() >= 0)
{

printf( "Content-type:text/html\r\n\r\n" );
printf( "<p> Hello FastCGI !  </ p>" );
printf( "<br /> Request number = [%d]", ++count );
printf( "<br /> Process ID: %d ", getpid() );

}

return 0;
}


保存成test1.c

编译:

# gcc -Werror-Wall ./test1.c -o ./test1.cgi -lfcgi -Wl,-R /usr/local/lib

启动:

# sudo spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/local/nginx/fastcgitest/test1.cgi -F 1-P /var/log/nginx/myfastcgi

修改/usr/local/nginx/conf/nginx.conf,在server中加入如下:

location ~\.cgi$

{

root fastcgitest;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.cgi;

fastcgi_param $fastcgi_script_name;

include fastcgi_params;

}

重新加载配置文件:

# /usr/local/nginx/sbin/nginx-s reload

浏览器中访问:
http://ip/test1.cgi
成功

几个概念

1. .ginx收到cgi请求后,会看有多少个该cgi程序的进程(spawn-fcgi -F指定的参数),然后根据并发量来调用(调度)cgi程序。

2. 原版spawn-fcgi(可参考下面七参考资料里daemon版spawn-fcgi)在fork了cgi程序后,自己就退出了。这时fork了的cgi程序的父进程ID都是1了,即init系统进程。这样,如果想并发就需要你的fastcgi去支持并发,可google:fastcgi并发

3. 关于php,nginx是用fastcgi来解析php的。这个负责解析的fastcgi程序并不多,好像就1个,因此这cgi不能大并发,但是没关系nginx支持cgi缓存~所以php网页的并发请求跟fastcgi关系不大。其实可以把fastcgi对于php的作用当作一个编译器,编译完后,php都有了缓存,再请求就不需要再次跑fastcgi来解析php脚本了,php就是个该死的脚本啊
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: