`
marsprj
  • 浏览: 65082 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

CentOS上配置lighttpd/fastcgi环境(2)

    博客分类:
  • GIS
阅读更多
在lighttpd上开发fastcgi程序需要安装两个库
一、安装开发库
1) fcgi
   http://fastcgi.com/dist/fcgi-2.4.0.tar.gz
   fcgi用于处理fastCGI请求
2) rudeCGI
   http://rudeserver.com/cgiparser/download/rudecgi-5.0.0.tar.bz2
   rudeCGI用于解析http request参数的KVP对
   例如:一个WMS的GetCapabilities请求(http://www.opengeospatial.org/standards/wms)
http://10.0.0.136:2087/world/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities
http request参数为:SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities
通过rudeCGI可以这样方位KVP对
rude::CGI cgi;
cgi["SERVICE"];
cgi["VERSION"];
cgi["REQUEST"];
值分别是:
WMS
1.3.0
GetCapabilities
并且rudecgi可以从接受从stdin传输的字符串,从而可以在命令行下调试fastcgi程序

rudecgi-5.0.0.tar.bz2源码本身有点问题,需要做一点修改。
修改AbstractImplementation.cpp文件的instance()函数为
	AbstractImplementation* AbstractImplementation::instance()
	{
		if(!s_instance)
		{
			finished=false;
			s_instance = new Implementation();
		}
		return s_instance;
	}

1、安装fcgi
#tar zxvf fcgi-2.4.0.tar.gz
#cd fcgi-2.4.0
#./configure
#make
#make install
2、安装rudecgi-5.0.0.tar.bz2
修该rudecgi的源码后,安装rudecgi
#tar jxvf rudecgi-5.0.0.tar.bz2
#./configure
#make
#make install

二、编写fastcgi程序
安装完rudecgi和fcgi以后就可以用C/C++写fastcgi程序了,下面是个fastcgi的小程序
#vi fcgitest.cpp
	#include <rude/cgi.h>
	#include <fcgi_stdio.h>
	#include <fcgi_config.h>
	#include "stdlib.h"
	#include "stdio.h"

	int main(int argc, char* argv[])
	{
		int count = 0;
		while(FCGI_Accept_t() >= 0)
		{	
			rude::CGI cgi(); 
			
			//输出http头
			//mime type是text/plain类型
			//http head和body之间要有一个空行,所以输出两个\r\n
			printf("ContentType: text/plain \r\n" "\r\n");
			
			//输入计数器
			printf("count:%d ", count++);
			printf("<br>");
			//输出http requst的kvp的值
			printf("Service:%s", cgi["SERVICE"]);
			printf("<br>");
			printf("Version:%s", cgi["VERSION"]);
			printf("<br>");
			printf("Request:%s", cgi["REQUEST"]);
			printf("<br>");
			
			//http response结束
			printf("\n\n");
			
			cgi.finish(); 
		}
		return 0;
	}

编译fcgitest.cpp
#g++ -o fcgitest.cgi fcgitest.cpp -lfcgi++ -lrudecgi
生成fcgitest.cgi程序
在命令行下就可以运行fcgitest.cgi程序
#./fcgitest.cgi
就可以运行

三、将fcgitest.cgi程序配置到lighttpd中
在lighttpd中配置fastcgi需要打开fastcgi的mod
打开lighttpd的配置文件lighttpd.conf
去掉server.modules下面的"mod_fastcgi"模块的注释符“#”。
在lighttpd.conf中找到
#fastcgi.server = (
一行,我这里是在225行,这里就是lighttpd配置fastcgi的部分。默认有一个php的配置。
现在就把刚才写的fcgitest.cgi程序配置到lighttpd中去。
配置文件如下:
"/fastcgi" =>
    ( "localhost" =>
      (
        #unix socket的路径
        "socket" => "/tmp/fcgitest-fcgi.socket",
        "check-local" => "disable",
        #fastcgi应用程序的路径
        "bin-path" => "/opt/fcgitest/fcgitest.fcgi",	
        "idle-timeout" => 10,
        #当访问量增大的时候lighttpd启动的最小和最大进程数的设置	
        #这里设置最少启动一个进程,最多启动10个进程
        "min-procs" => 1,
        "max-procs" => 10
      )
    )

配置完以后的fastcgi.server节点为:
#fastcgi.server= ( 
#   ".php" =>
#     ( "localhost" =>
#       (
#         "socket" => "/var/run/lighttpd/php-fastcgi.socket",
#         "bin-path" => "/usr/local/bin/php-cgi"
#       )
#     )
#     ,
    "/fastcgi" =>
      ( "localhost" =>
        (
          "socket" => "/tmp/fcgitest-fcgi.socket",
          "check-local" => "disable",
          "bin-path" => "/opt/fcgitest/fcgitest.fcgi",
          "idle-timeout" => 10,			
          "min-procs" => 1,
          "max-procs" => 10
        )
      )
    )

配置完以后重新启动lighttpd
#/etc/init.d/lighttpd restart
然后就可以在浏览器里面访问fcgitest服务了
URL:
		http://10.0.0.136:2087/fcgitest?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics