Apache Fast CGI C++程序开发

Apache Fast CGI开发

Author: Kagula

发起日期:2014-11-08

最后更新日期:2016-02-29

环境:

[1]Win 8.1 64bits、 VMWare9,Cent OS 6.532bits, fcgid 2.3.9, boost 1.55, Apache 2.2.25,

[2] Cent OS 6.6,Apache 2.2.15, CMake 2.8.12.2,  GCC 4.4.7,  boost 1.57

 

   Fast CGI相比CGI,响应速度提高了好几倍(免去了每次收到HTTP客户端请求,创建释放一次进程的开销),所以这里只记录Fast CGI的学习笔记。

准备

         Apache是一个后台运行的程序,没有界面。所有的配置,都包含在配置文件里。主配置文件是httpd.conf

Windows 8平台

         从官网下载httpd-2.2.25-win32-x86-openssl-0.9.8y.msi文件,选择自定义安装,勾选"Build Headers and Libraries"。这里假设默认路径安装。

   Apache上有两种FastCGI模块,本文选择mod_fcgid

 

加载mod_fcgid模块

    从参考资料[7]下载mod_fcgid-2.3.9-crlf.zip文件,解压后用VisualStudio打开,为项目添加头文件search path

“C:\Program Files (x86)\Apache Software Foundation\Apache2.2\include”,

library path

" C:\Program Files (x86)\ApacheSoftware Foundation\Apache2.2\lib"

 

    Release模式下编译出mod_fcgid.so文件后复制到”C:\Program Files (x86)\Apache Software Foundation\Apache2.2\modules “目录下。

 

在httpd.conf文件加入下面这行代码,加载fastcgi模块

LoadModule  fcgid_module  modules/mod_fcgid.so

装载新的module需要重启Apache服务器。

 

正文

第一个程序,为了说明流程。

   下载http://www.fastcgi.com/dist/fcgi.tar.gz,解压后编译。

 

新建项目

头文件搜索路径

“D:\SDK\fcgi-2.4.1-SNAP-0311112127\include”

库文件搜索路径

“D:\SDK\fcgi-2.4.1-SNAP-0311112127\libfcgi\Debug”

添加依赖库,libfcgi.lib

并把“libfcgi.dll”放在EXE输出所在路径上。

TestFastCGI.exe源文件清单如下:

#include <fcgi_stdio.h>
 
int _tmain(int argc, _TCHAR* argv[])
{
         FCGX_Stream*in, *out, *err;
         FCGX_ParamArrayenvp;
 
         while(FCGX_Accept(&in, &out, &err, &envp) >= 0)
         {
                   FCGX_PutS("content-type:text/plain\r\n\r\nHello World From FastCGI\r\n", out);
         }
         return0;
}


 

源代码编译成功后(为了方便调试程序,这里使用DEBUG模式编译),输出到下面的路径中

D:\Workspace\TestFastCGI\Debug

现在这个路径下应该有下面两个文件

TestFastCGI.exe(编译输出文件)

libfcgi.dll(从fcgi工程中复制过来的动态库)

注意:FastCGI可执行程序所依赖的DLL(或则so)文件必须在同一个目录中。

 

修改Apache的 httpd.conf文件

添加下面的代码

LoadModule fcgid_module  modules/mod_fcgid.so

... ...

<IfModule  mod_fcgid.c>

 <Directory "D:\Workspace\TestFastCGI\Debug">

   SetHandler fcgid-script

   Order allow,deny

   Allow from all

 </Directory>

  ScriptAlias  /myfcgid "D:\Workspace\TestFastCGI\Debug\TestFastCGI.exe"

 IdleTimeout 3600

 ProcessLifeTime 7200

 MaxProcessCount 64

 DefaultMaxClassProcessCount 8

 IPCConnectTimeout 300

 IPCCommTimeout 7200

 BusyTimeout 300

</IfModule>

 

现在在浏览器中输入“localhost/myfcgid就打印出了“HelloWorld From FastCGI”字符串。

我们看到/myfcgid路径被映射到了TestFastCGI.exe程序

 

Q:如何调试程序?

[S1]在任务管理器中停止Apache2.2服务。

[S2]调用“C:\Program Files(x86)\Apache Software Foundation\Apache2.2\bin\httpd.exe”启动Apache服务,现在可以找到程序AttachProcess了。


Q CentOS下FCGI程序没有对/www/var/fcgi-bin下的写权限.

把“fcgi-bin”目录的属主改为apache配置文件中apache的启动用户名,通常为“apache”或“www”。

例如用下面的命令:chown  -R  apache  fcgi-bin

 

 

第二个程序,PUT/GET,Cookie,JSON

示例代码:

TestFastCGI.cpp(主程序)

/*!
 * \file TestFastCGI.cpp
 * \date 2014/11/09 20:47
 *
 * \author kagula
 * Contact: lee353086@163.com
 *
 * Fast CGI Demo
 */


#include <stdio.h>




#include "FCGIHelper.h"

int main(int argc, char* argv[])
{
	FCGX_Stream *in, *out, *err;
	FCGX_ParamArray envp;

	while (FCGX_Accept(&in, &out, &err, &envp) >= 0)
	{
		kagula::CFCGIHelper fcgi(in, out, err, envp);

		//first run, set cookie
#ifdef WIN32
		std::string strTest = fcgi.GetCookie("TEST");
#else
		//It's strange, difference from windows!
		std::string strTest = fcgi.GetCookie("TES");
#endif
		std::string strKagula = fcgi.GetCookie("kagula");

		std::string strJSON = "=====>\r\n";

		if (strTest.empty() == true)
		{
			fcgi.SetCookie("TEST", "first&
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值