fastcgi apache fcgi

 FastCGI 安装与配置
相关软件包:
httpd 2.2.14      //注意版本 这个版本不会出问题   注:apache httpd安装 

fcgi-2.4.0.zip
mod_fastcgi-2.4.6.zip 请仔细阅读其中的README 

php 5.2.17

配置apache: (默认安装在/usr/local 文件夹下)

#配置httpd.conf 尾部添加 :
LoadModule fastcgi_module modules/mod_fastcgi.so
<IfModule fastcgi_module>
AddHandler fastcgi-script .fcgi # you can put whatever extension you want
</IfModule>
FastCgiIpcDir /tmp

#可以限制fcgi数目 和 链接时间:
FastCgiServer /root/fcgi/test.fcgi -processes 1 -idle-timeout 1000

# : -processes 1 只允许开启一个进程 适合gdb调试 -idle-timeout 1000 连接超时时间1000s 适合gdb调试

#修改fcgi目录
ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
<Directory "<Directory "/usr/local/apache2/cgi-bin">
</Directory>

 

配置参考地址:http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiServer

test.c:(简单示例不推荐)

#include <fcgi_stdio.h>
#include
<stdlib.h>
int $count =0;
int main( int argc, char*argv[] )
{
while (FCGI_Accept() >=0) {
FCGI_printf(
"Content-Type:text/html\n\n");
FCGI_printf(
"hello world\n\n");
}
}

  编译:

gcc -Wall -g -O0  test.c -o test.fcgi -lfcgi

 

访问: http://localhost/cgi-bin/test.fcgi

test2.c(推荐)

 

/* Compile with: gcc -Wall -lfcgi fastcgi.c -o fastcgi 
*/

#include
<stdio.h>
#include
<stdlib.h>
#include
<fcgiapp.h>

#define printf(...) FCGX_FPrintF(request->out, __VA_ARGS__)
#define get_param(KEY) FCGX_GetParam(KEY, request->envp)

void handle_request(FCGX_Request *request) {
char*value;

printf(
"Content-Type: text/plain\r\n\r\n");
if ((value = get_param("REQUEST_METHOD")) != NULL) {
printf(
"%s ", value);
}
if ((value = get_param("REQUEST_URI")) != NULL) {
printf(
"%s", value);
}
if ((value = get_param("QUERY_STRING")) != NULL) {
printf(
"?%s", value);
}
if ((value = get_param("SERVER_PROTOCOL")) != NULL) {
printf(
" %s", value);
}
printf(
"\n");
}

int main(void) {
//int sock;
FCGX_Request request;

FCGX_Init();
//sock = FCGX_OpenSocket(":2005", 5);
FCGX_InitRequest(&request, 0, 0);

while (FCGX_Accept_r(&request) >=0) {
handle_request(
&request);
FCGX_Finish_r(
&request);
}

return EXIT_SUCCESS;
}

 

  

 

编译  Makefile:

all:main
main: test.c
gcc
-g -Wall -O0 -lfcgi -o test.fcgi test.c

//注意gcc前面用tab代替空格

posted @ 2011-06-02 16:46  wangkangluo1  阅读(3221)  评论(0编辑  收藏  举报