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代替空格