FastCGI的编译和移植

1、编译和安装:

1)、下载:从http://www.fastcgi.com/drupal/下载fcgi.tar.gz;

2)、解压:tar xzvf fcgi.tar.gz

3)、配置:

./configure --prefix=/home/pub/johnny/network/install-fcgi --host=mips-linux-gnu --disable-FEATURE --without-PACKAGE "CC=mips-linux-gnu-gcc -EL" "CFLAGS=-EL" "LDFLAGS=-EL" "CXX=mips-linux-gnu-g++ -EL" "CXXFLAGS=-EL"

4)、make;在make时出现以下错误:

fcgio.cpp: In destructor 'virtual fcgi_streambuf::~fcgi_streambuf()':
fcgio.cpp:50: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::overflow(int)':
fcgio.cpp:70: error: 'EOF' was not declared in this scope
fcgio.cpp:75: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::sync()':
fcgio.cpp:86: error: 'EOF' was not declared in this scope
fcgio.cpp:87: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::underflow()':
fcgio.cpp:113: error: 'EOF' was not declared in this scope

解决办法:
在/include/fcgio.h文件中加上 #include <cstdio>,然后再编译安装就通过了。
是由于GCC版本问题导致。

5)、make install;

在/home/pub/johnny/network/install-fcgi有 bin/、include/、lib/三个文件夹;

copy到开发板上;

2、测试:

1)、代码:

http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModFastCGI

#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <alloca.h>
#include <fcgiapp.h>
#define LISTENSOCK_FILENO 0
#define LISTENSOCK_FLAGS 0
int main(int argc, char** argv) {
        openlog("testfastcgi", LOG_CONS|LOG_NDELAY, LOG_USER);
        int err = FCGX_Init(); /* call before Accept in multithreaded apps */
        if(err){
                syslog (LOG_INFO, "FCGX_Init failed: %d", err); return 1;
        }
        FCGX_Request cgi;
        err = FCGX_InitRequest(&cgi, LISTENSOCK_FILENO, LISTENSOCK_FLAGS);
        if(err){
                syslog(LOG_INFO, "FCGX_InitRequest failed: %d", err);
                return 2;
        }

        while(1){
                err = FCGX_Accept_r(&cgi);
		syslog(LOG_INFO, "Receive Request!");
                if(err){
                        syslog(LOG_INFO, "FCGX_Accept_r stopped: %d", err);
                        break;
                }
                char** envp;
                int size = 200;
                for (envp = cgi.envp; *envp; ++envp)
                        size += strlen(*envp) + 11;
                char*  result = (char*) alloca(size);
                strcpy(result, "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n");
                strcat(result, "<html><head><title>testcgi</title></head><body><ul>\r\n");

                for (envp = cgi.envp; *envp; ++envp) {
                        strcat(result, "<li>"); 
                        strcat(result, *envp); 
                        strcat(result, "</li>\r\n");
                }

                strcat(result, "</ul></body></html>\r\n");
                FCGX_PutStr(result, strlen(result), cgi.out);// = FCGX_FPrintF(cgi.out,"%s",result);
        }

        return 0;
}

编译:

mips-linux-gnu-gcc -EL -I/home/pub/johnny/network/install-fcgi/include -L/home/pub/johnny/network/install-fcgi/lib -lfcgi testfastcgi.c -o test.fastcgi

2)、copy到开发板tmp/bin/下;

3)、修改conf.d/fastcgi.conf:

fastcgi.server = (
        "/test.php" =>
        ((
                "socket" => "/tmp/lighttpd.player.server.socket",
                "bin-path" => "/tmp/bin/test.fastcgi",
                "max-procs" => 1,
#               "host" => "127.0.0.1",
#               "port" => 8081,
                "check-local" => "disable",
        ))
)

4)、在PC上运行http://192.168.9.*/test.php,在网页上显示fastcgi的运行环境,而且每刷新一次网页,打印一次“Receive Request!”;

3、post和ajax测试:

1)、在2中的测试代码中加上:

void getData(FCGX_Request cgi){
        const char * request_method;
        request_method = FCGX_GetParam("REQUEST_METHOD", cgi.envp);
        printf("request_method = %s \n",request_method);
        if(0 == strcasecmp(request_method, "POST")){
                printf("REQUEST_METHOD = POST\n");
                const char *p_content_len =FCGX_GetParam("CONTENT_LENGTH", cgi.envp);
                if (p_content_len != NULL) {
                        int len = atoi(p_content_len);
                        printf("%d \n",len);
                        if(len > 0){
                                char buf[len];
                                FCGX_GetStr(buf,len,cgi.in);
                                printf("%s\n",buf);
                        }
                }
        }

}

在main()中加上:

……
                strcat(result, "</ul></body></html>\r\n");
                getData(cgi);
                FCGX_PutStr(result, strlen(result), cgi.out);
……

重新编译后复制到开发板对应目录;

2)、把index.html 复制到开发板的/home/pub/johnny/network/install/webpages/目录:

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>lighttpd</title>
		<script type="text/javascript" src="./Global/jquery.min.js"></script>
		<script type="text/javascript" src="./Global/jquery.json.min.js"></script>
		<script>
			//test post
			$.post("test.php", $.toJSON({method: "test"}), function(data) {
				//var result = $.parseJSON(data);
				console.log("test post sucess!");
				console.log(data);
			});
			
			//test ajax
			var post_data = $.toJSON({method : "test",data : "test"});
			$.ajax({
				type: 'POST',
  			url: 'test.php',
  			data: post_data,
  			success: function(data){
  				console.log("test ajax sucess!");
  				console.log(data);
  			},
  			timeout:1000,
  			error: function(){
  				console.log("ajax error!!!!");
  			}
			});
	
		</script>
	</head>
	<body>
		<p>lighttpd fastcgi test(for mips-linux)</p>
		<hr>
		<p>this is a test</p>
	</body>
</html>

3)、在PC上打开http://192.168.9.*/index.html,每次刷新网页,开发板打印:

testfastcgi: Receive Request!
request_method = POST 
REQUEST_METHOD = POST
17 
{"method":"test"}
testfastcgi: Receive Request!
request_method = POST 
REQUEST_METHOD = POST
31 
{"method":"test","data":"test"}

4、服务器压力测试:

在某台linux主机上输入:

ab -c 1000 -n 1000 http://192.168.8.*/test.php

会打印出测试的结果;



你可能感兴趣的:(fastcgi)