2009年10月24日 星期六

FastCGI

FastCGI是一個網頁伺服器端應用程式介面,CGI有一個很大的缺點就是CGI程式每次執行就會產生一個新的process,當同時很多人上線時,會對伺服器造成很大負擔;而FastCGI程式是常駐型的CGI程式,不會因為Http_Request結束而終止。

在Debian 5安裝Apache2 + FastCGI
# apt-get install apache2 libapache2-mod-fastcgi
# /etc/init.d/apache2 restart

接著用c寫一個helloFCGI的程式來驗証:
#include "fcgi_stdio.h"
#include <stdlib.h>

int main()
{
int count = 0;
while(FCGI_Accept() >= 0) {
printf("Content-type: text/html\r\n"
"\r\n"
"<title>Hello FastCGI</title>"
"<h1>Hello FastCGI</h1>"
"Request number %d running on host <i>%s</i>\n",
++count, getenv("SERVER_NAME"));
}
return 0;
}

編譯時要加上-lfcgi參數
# gcc -lfcgi -o helloFCGI.fcgi helloFCGI.c

將helloFCGI.fcgi放到所設定的FastCGI執行的資料夾內(Debian安裝好的預設資料夾在/usr/lib/cgi-bin),在網頁執行http://localhost/cgi-bin/helloFCGI.fcgi即可看到結果。

沒有留言:

張貼留言