標籤:
1、fastcgi簡介
fastcgi解決了cgi程式處理請求每次都要初始化和結束造成的效能問題。fastcgi並且是獨立於webserver的,fastcgi的crash並不影響webserver,然後他們之間通過soket通訊。與fastcgi不同的另一種解決cgi程式反覆建立,銷毀的方法是讓webserver開放api,然後編寫cgi的時候,把cgi嵌入到webserver中,這樣有個不好的地方就是cgi的crash會影響到webserver。
支援fastcgi的伺服器有很多比如,nginx IIS什麼的。他們都是把http請求轉換為stdin和一些環境變數傳遞給fastcgi程式,然後返回stdout。編寫fastcgi程式最終要的一個api就是int FCGI_Accept(void);當一個請求被送達的時候返回。
2、fastcgi安裝步驟:
step1:
:http://www.fastcgi.com/drupal/node/5 點擊Current: download | docs | browse 中的download連結即可。
step2:
tar -zxvf fcgi.tar.gz
cd fcgi-2.4.1-SNAP-0311112127/
./configure –prefix=/etc/fcgi
make && make install
出現fcgio.cpp:50: error: ‘EOF’ was not declared in this scope的話只需要 在/include/fcgio.h檔案中加上 #include
到此就安裝完了。
3、編寫並測試fcgi程式
1)Demo測試程式fcgi_test2.c如下:
#include "fcgi_stdio.h"#include <stdlib.h>int main(void){int count = 0;while(FCGI_Accept() >= 0){printf("Content-type: text/html\r\n""\r\n""<title>FastCGI Hello!</title>""<h1>FastCGI Hello!</h1>""Request number %d running on host <i>%s</i>\n",++count, getenv("SERVER_NAME"));}return 0;}
2)編譯
[[email protected] cgi-bin]# gcc -g fcgi_test2.c -o fcgi_test2.fcgi -lfcgi
//此時直接運行 fcgi_test2.fcgi 會報如下錯:
error while loading shared libraries: libfcgi.so.0:
3)連結庫錯誤解決方案:
參考:https://www.apachelounge.com/viewtopic.php?p=8160
方案核心——“Link echo with the static library, libfcgi.a, instead of the shared library.”連結到靜態庫,而非共用庫。
[[email protected] cgi-bin]# gcc fcgi_test2.c -o fcgi_test2.fcgi -I/etc/fcgi/include /usr/local/lib/libfcgi.a
4)查看關聯連結
[[email protected] cgi-bin]# ldd fcgi_test2.fcgi
linux-vdso.so.1 => (0x00007fff7fdc3000)
libc.so.6 => /lib64/libc.so.6 (0x00007fa0229ed000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa022d86000)
4、正確運行結果如下
1)程式運行結果如下
[[email protected] cgi-bin]# ./fcgi_test2.fcgi
Content-type: text/html<title>FastCGI Hello!</title><h1>FastCGI Hello!</h1>Request number 1 running on host <i>(null)</i>
2)瀏覽器運行結果如下:
Centos 安裝fastcgi詳解與用例