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"
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
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
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", )) )
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); } } } }
…… 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>
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