標籤:des style blog http io ar color os 使用
一. spawn_fastcgi的安裝、部署與配置.
1. 下載spawn_fastcgi. https://github.com/lighttpd/spawn-fcgi 這裡使用的是1.6.3的版本 https://github.com/lighttpd/spawn-fcgi/releases/tag/v1.6.3 2. 解壓並安裝(請記得看README). (1) 如果沒有configure,請先執行./autogen.sh,產生configure (2) ./configure (3) make 注意: 如果通過上面安裝不成功, 試試直接 sudo apt-get install spawn_fastcgi 3. 編譯好以後,將可執行檔移動到nginx的sbin目錄下. cp ./src/spawn-fcgi /usr/local/nginx/sbin/ (cp到nginx的安裝目錄下) 注意: 1. 第3步, 如果拷貝過程中提示找不到./src/spawn-fcgi, 則用which spawn-fcgi找到該二進位拷貝; 2. 第3步, 如果拷貝過程中提示找不到/usr/local/nginx/sbin/, 則手動建立目錄. 二. fastcgi庫的安裝. 1. 下載 http://www.fastcgi.com/dist/fcgi.tar.gz(從這下載的可能有問題, 最好用本文給出的.) 2. $./configure $make $make install 注意: 1. 安裝過程中如果出現如下錯誤, 則在fcgio.cpp中添加標頭檔 #include <stdio.h>, 重新編譯即可. 參考http://stackoverflow.com/questions/8833718/installing-fastcgi-dev-kit 2. 如果make通過, 但是make install有錯, 嘗試用本文給的檔案, 不用步驟1連結中的.
fcgio.cpp: In destructor ‘virtual fcgi_streambuf::~fcgi_streambuf()‘:
fcgio.cpp:50:14: error: ‘EOF‘ was not declared in this scope
fcgio.cpp: In member function ‘virtual int fcgi_streambuf::overflow(int)‘:
fcgio.cpp:70:72: error: ‘EOF‘ was not declared in this scope
fcgio.cpp:75:14: error: ‘EOF‘ was not declared in this scope
fcgio.cpp: In member function ‘virtual int fcgi_streambuf::sync()‘:
fcgio.cpp:86:18: error: ‘EOF‘ was not declared in this scope
fcgio.cpp:87:41: error: ‘EOF‘ was not declared in this scope
fcgio.cpp: In member function ‘virtual int fcgi_streambuf::underflow()‘:
fcgio.cpp:113:35: error: ‘EOF‘ was not declared in this scope
make[2]: *** [fcgio.lo] Error 1
make[2]: Leaving directory `/home/lucasysfeng/workspace/web/download/nginx_spawn_fcgi_install_package/fcgi-2.4.1-SNAP-0311112127/libfcgi‘
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/lucasysfeng/workspace/web/download/nginx_spawn_fcgi_install_package/fcgi-2.4.1-SNAP-0311112127‘
make: *** [all] Error 2
三. Demo程式. 1. demo.cc如下所示:
#include <fcgi_stdio.h>#include <stdlib.h>#include <unistd.h>int main() { int count = 0; while (FCGI_Accept() >= 0) { printf("Content-type: text/html\r\n" "\r\n" "" "FastCGI Hello!" "Request number %d running on host%s " "Process ID: %d\n", ++count, getenv("SERVER_NAME"), getpid()); } return 0;}
2. 編譯.
g++ demo.cc -o demo -lfcgi?
注意:1. 如果編譯不通過, 可能是庫-lfcgi? 沒找到, 那就是<二. fastcgi庫的安裝.>有問題 2. 直接運行可執行檔,看看能否正常運行。如果出現缺少庫libfcgi.so.0, 則自己需要手動把/usr/local/lib/libfcgi.so.0庫建立一個連結到/usr/lib/目錄下: ln -s /usr/local/libfcgi.so.0 /usr/lib/(或者把so的庫路徑添加到/etc/ld.so.conf,並執行ldconfig更新一下) 四. demo發布. 1)將CGI可執行程式移動到nginx的安裝目錄下 /usr/local/nginx/cgibin (檔案夾不存在則自己建立) cp ***/demo /usr/local/nginx/cgibin 2)啟動spawn-fcgi管理進程,並綁定server IP和連接埠(不要跟nginx的監聽連接埠重合) /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088 -f /usr/local/nginx/cgibin/demo
查看一下8088連接埠是否已成功:netstat -na | grep 8088? 3)更改nginx.conf設定檔(或者是/etc/nginx/sites-available/default),讓nginx轉寄請求
在http節點的子節點-"server節"點中下添加配置 location ~ \.cgi$ {
fastcgi_pass 127.0.0.1:8088;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME fcgi$fastcgi_script_name;
include fastcgi_params; } 4)重啟nginx或者重新載入設定檔 sudo service nginx restart
5)開啟瀏覽器訪問一下吧 http://localhost/demo.cgi
web(五)---spawn-fastcgi