標籤:nginx fastcgi c++
1.Nginx1.1.安裝
- Nginx 的中文維基 http://wiki.codemongers.com/NginxChs 下載 Nginx 0.6.26(開發版)(請下載最新版本)
- tar zxvf nginx-0.6.26.tar.gz
- ./configure,注意了類似checking for *** ... not found項,可能是依賴包沒有,則需要安裝依賴包
- 缺少PCRE,yum install pcre安裝。或者去:http://www.pcre.org/
- 如果缺少OpenSSL,yum install libssl-dev,或者去:http://www.openssl.org
- 如果缺少zlib,可以yum install zlib1g,或者http://www.zlib.net/
- make & make install
- 建議安裝在/usr/local/nginx下
1.2 啟動 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2.FastCGI開發庫安裝2.1 安裝fastcgi庫下載://www.fastcgi.com/dist/fcgi.tar.gz./configure
make
make install
2.2 樣本程式
#include <fcgi_stdio.h>int main( int argc, char *argv[] ){ while( FCGI_Accept() >= 0 ) { FCGI_printf( "Status: 200 OK\r\n" ); FCGI_printf( "Content-Type: text/html\r\n\r\n" ); FCGI_printf( "Hello world in +++++++\n" ); } return 0;}
編譯後的二進位程式為t.cgi
3.spawn-fcgi先有必要有這樣第一個認識:ngxin作為一個webserver,本身並不包含CGI的解譯器,需要通過一個中介軟體【例如php-fpm】來運行CGI。他們之間的模式大致是:
nginx <-- socket --> php-fpm-------->php
那麼nginx既然要運行c寫的CGI那麼也應該有類似php-fpm的東西。baidu, google了下,發現有一個spawn-fcgi的東西。原本是lighttp 內的fastcgi進程管理器
3.1 安裝 spawn-fcgi
wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gztar zxvf lighttpd-1.4.19.tar.gz cd lighttpd-1.4.19./configuremake
複製 編譯好的spawn-fcgi到 nginx目錄,很主要!!!!
cp ./src/spawn-fcgi /usr/local/nginx/sbin/
3.2 啟動spawn-fcgi
spawn-fcgi -a 127.0.0.1 -p 7000 -f /home/jiangf/t.cgi
?
4.配置nginx.conf
location ~ \.cgi$ { fastcgi_pass 127.0.0.1:7000; fastcgi_index index.cgi; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; }
最後在瀏覽器中輸入http://192.168.19.103:9990/1.cgi
Nginx + FastCgi + Spawn-fcgi + C 架構的伺服器環境搭建