FAST-CGI安装与使用
FastCGI 像是一個常駐 (long-live) 型的 CGI,它可以一直執行著,只要激活后,不會每次都要花費時間去 fork 一次 (這是 CGI 最為人詬病的 fork-and-execute 模式)。
1.下載安裝fcgi
# wget? http://www.fastcgi.com/dist/fcgi.tar.gz
# tar zxvf? fcgi.tar.gz
# cd ./fcgi-2.4.1-SNAP-0311112127
# ./configure
#??? make
#??? make install
注意安裝會報錯
fcgio.cpp:50: error: 'EOF' was not declared in this scope
參考:
http://qichunren.iteye.com/blog/609351
https://bugs.gentoo.org/256654?id=256654
解決辦法:
在/include/fcgio.h文件中加上 #include <cstdio>,然后再編譯安裝就通過了。
--------------------
php-fpm?
她同樣也是一個PHP?FastCGI管理服務器,是只用于PHP的
可以在?http://php-fpm.org/download?下載得到
她是PHP源代碼的一個補丁,必須將她patch到你的PHP源代碼中,在編譯安裝PHP后才可以使用
2.spawn-fcgi運行fcgi
安裝spawn-fcgi
spawn-fcgi是一個通用的FastCGI管理服務器
她是lighttpd中的一部份,但目前已經單獨成為一個項目,最新的lighttpd沒有這一塊,但可以在以前版本中找到她
在lighttpd-1.4.15(?http://www.lighttpd.net/download/lighttpd-1.4.15.tar.gz?)中就有她
Note注:最新的spawn-fcgi可以到lighttpd.net網站搜索“spawn-fcgi”找到她的最新版本發布地址
目前她的下載地址是http://redmine.lighttpd.net/news/2?最新版本是
# wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
# tar?-zxvf?spawn-fcgi-1.6.3.tar.gz
# cd spawn-fcgi-1.6.3
# ./configure
# make
# make install
=====或者安裝lighttpd==========
# cd?lighttpd-1.4.15
#./configure
# make?
因為我不需要安裝lighttp而是只需要他其中的某個文件,所以只make就可以了,不需要make?install
# cp?src/spawn-fcgi?/usr/local/bin/spawn-fcgi?
取出spawn-fcgi的程序
=============================
執行PHP-cgi
#??/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u nobody -g nobody -f /usr/local/php/bin/php-cgi
執行成功返回pid
查看進程
# ps aux??
或者
# ps -A|grep php-cgi
?8929 ???????? 00:00:00 php-cgi
?8930 ???????? 00:00:00 php-cgi
?8931 ???????? 00:00:00 php-cgi
?8932 ???????? 00:00:00 php-cgi
?8933 ???????? 00:00:00 php-cgi
?8934 ???????? 00:00:00 php-cgi
綁定nginx
nginx默認web文件夾在配置文件中
#cd /usr/local/nginx/conf
修改nginx.conf,加入如下語句:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
啟動nginx
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意這里
| Server API | CGI/FastCGI |
64位Linux編譯安裝Nginx+PHP(phpfpm)?
=============================================================
啟動其他cgi
參數含義如下
?-f?<fcgiapp>?指定調用FastCGI的進程的執行程序位置,根據系統上所裝的CGI的情況具體設置
?-a?<addr>?綁定到地址addr
?-p?<port>?綁定到端口port
?-s?<path>?綁定到unix?socket的路徑path
?-C?<childs>?指定產生的FastCGI的進程數,默認為5(僅用于PHP)
?-P?<path>?指定產生的進程的PID文件路徑
?-u和-g?FastCGI使用什么身份(-u?用戶?-g?用戶組)運行,如nobody、apache等
3.安裝fcgiwrap
其實fcgiwrap與spawn-fcgi的關系。spawn-fcgi是網上現成的封裝了fcgiwrap的工具,可以下載直接使用。如果下載fcgiwrap,可以自己編寫腳本。有些帖子讓spawn-fcgi啟動fcgiwrap是錯誤的。
如果要研究fcgiwrap可以如下安裝
??? wget http://github.com/gnosek/fcgiwrap/tarball/master? -P /usr/locl/src ?
??? tar zxvf gnosek-fcgiwrap-1.1.0-0-g333ff99.tar.gz -P /usr/local/src ?
??? cd /usr/local/src/gnosek-fcgiwrap-333ff99 ?
??? autoreconf -i ?
??? ./configure ?
??? make ?
??? # 復制fcgiwrap ?
??? cp fcgiwrap /usr/local/bin
中間有報錯:
undefined?reference?to?rpl_malloc
解決辦法:
??? vi config.h.in ?
??? undef malloc ?
??? # 這這行注釋 ?
??? /* #undef malloc */
運行CGI
# /usr/local/bin/spawn-fcgi?-f?/usr/local/bin/fcgiwrap?-a?192.168.1.10?-p?10000?-F?32?-P?/tmp/fastcgi-c.pid?-u?nobody?-g?nobody
spawn-fcgi: child exited with: 8spawn-fcgi: child spawned successfully: PID: 5277
spawn-fcgi: child spawned successfully: PID: 5278
spawn-fcgi: child spawned successfully: PID: 5279
4.spawn-fcgi運行fastcgi代碼
#include "fcgi_stdio.h"
#include <stdio.h>
#include <stdlib.h>
int count;
void initialize(void)
{count=0;
}
void main(void)
{initialize();while (FCGI_Accept() >= 0)?? {printf("Content-type: text/html "" ""<title>FastCGI Hello! (C, fcgi_stdio library)</title>""<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>""Request number %d running on host <i>%s</i> ",++count, getenv("SERVER_HOSTNAME"));}
}
編譯
#? gcc -o tinyfastcgi.cgi tinyfastcgi.c -lfcgi
或者這樣編譯
# gcc -I/usr/include/fastcgi -lfcgi hello.cpp -o echo.fastcgi
編譯生成hello.cgi程序,然后調用spawn-fcgi使得程序以fastcgi方式運行(具體底層運行方式由fastcgi實現,我們不用考慮)
運行
#?/usr/local/bin/spawn-fcgi -f? -a 192.168.1.10 -p 10000 -F 4 -P /usr/www/tinyfastcgi.fastcgi -u nobody -g nobody
綁定
然后配置nginx.conf文件
location ~ \.cgi$ {fastcgi_pass 127.0.0.1:2222;#rewrite (.*).cgi /$1 break;fastcgi_param SCRIPT_FILENAME /usr/local/nspnp/html/$fastcgi_script_name;fastcgi_index index.cgi;}
location ~ \.cgi$ {fastcgi_pass 127.0.0.1:2222;#rewrite (.*).cgi /$1 break;fastcgi_param SCRIPT_FILENAME /usr/local/nspnp/html/$fastcgi_script_name;fastcgi_index index.cgi;}location指令中指定將cgi后綴名的請求轉發給2222端口(上面spawn-fcgi指定的端口),然后用瀏覽器訪問127.0.0.1/hello.cgi,就可以看到結果了。
這里僅僅是一個最簡單的功能演示,表單的交互還在研究中,cgicc封裝了很好用的處理網頁元素的方法,推薦使用。
參考:
spawn-fcgi運行fcgiwrap???http://wiki.nginx.org/Fcgiwrap??nginx c/c++ fastcgi支持
C++寫一個簡單的fastcgi程序
FastCGI服務器的安裝和使用(spawn-fcgi和php-fpm)
fastcgi中的多線程使用
FastCGI 技術介紹
總結
以上是生活随笔為你收集整理的FAST-CGI安装与使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 轻量级简单队列服务HTTPSQS安装与使
- 下一篇: Linux网络编程中的几组类似功能的区别