Apache模块开发helloworld无错版

环境:CentOS 5.4

第一步:安装Apache的apxs

首先来介绍下apache的一个工具apxs。apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象,使之可以用由mod_so提供的LoadModule指令在运行时加载到Apache服务器中。

apxs可参考官方文档

http://lamp.linux.gov.cn/Apache/ApacheMenu/programs/apxs.html

输入命令查看是否有httpd-devel这个包,如果没有需要安装

#rpm -qa|grep httpd

# yum -y install httpd-devel

利用指令确认其已经安装

# which apxs
/use/sbin/apxs

也可以这样查找全部

#find / | grep apxs

第二步:apxs -g -n helloworld

上面的命令可以帮助我们产生一个模块名字为helloworld的模板。
上面的命令会产生以下代码

C代码

  1. #include"httpd.h"
  2. #include"http_config.h"
  3. #include"http_protocol.h"
  4. #include"ap_config.h"
  5. /*Thesamplecontenthandler*/
  6. staticinthelloworld_handler(request_rec*r)
  7. {
  8. if(strcmp(r->handler,"helloworld")){
  9. returnDECLINED;
  10. }
  11. r->content_type="text/html";
  12. if(!r->header_only)
  13. ap_rputs("Thesamplepagefrommod_helloworld.c\n",r);
  14. returnOK;
  15. }
  16. staticvoidhelloworld_register_hooks(apr_pool_t*p)
  17. {
  18. ap_hook_handler(helloworld_handler,NULL,NULL,APR_HOOK_MIDDLE);
  19. }
  20. /*DispatchlistforAPIhooks*/
  21. moduleAP_MODULE_DECLARE_DATAhelloworld_module={
  22. STANDARD20_MODULE_STUFF,//用于编译后的模块产生版本信息
  23. NULL,/*创建目录配置结构*/
  24. NULL,/*合并目录配置结构*/
  25. NULL,/*创建主机配置结构*/
  26. NULL,/*合并主机配置结构*/
  27. NULL,/*为模块配置相关指令*/
  28. helloworld_register_hooks/*注册模块的钩子函数*/
  29. };

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"

/* The sample content handler */
static int helloworld_handler(request_rec *r)
{
if (strcmp(r->handler, "helloworld")) {
return DECLINED;
}
r->content_type = "text/html";

if (!r->header_only)
ap_rputs("The sample page from mod_helloworld.c\n", r);
return OK;
}

static void helloworld_register_hooks(apr_pool_t *p)
{
ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA helloworld_module = {
STANDARD20_MODULE_STUFF, //用于编译后的模块产生版本信息
NULL, /* 创建目录配置结构*/
NULL, /* 合并目录配置结构 */
NULL, /* 创建主机配置结构 */
NULL, /* 合并主机配置结构 */
NULL, /* 为模块配置相关指令 */
helloworld_register_hooks /* 注册模块的钩子函数 */
};

我们来看下helloworld_module这个结构体,它没个成员的具体作用请看注释。
它最关键的参数为最后一个,这个参数是一个注册钩子函数指针,也就是说当我们把模块加入到apache里面去的时候,他会执行这个注册函数。在这个函数里面我们将会注册我们所要添加的钩子。
本例子中我们用的是

C代码

  1. ap_hook_handler(helloworld_handler,NULL,NULL,APR_HOOK_MIDDLE);

ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);

这个处理函数,这个处理函数注册了helloworld_handler这个函数。这个函数用于处理我们的请求。
我们来讲下执行的顺序,模块加载-》执行helloworld_register_hooks函数-》注册helloworld_handler这个函数到钩子上去。
这样一来:当一个http请求来的时候,我们就会自动去执行helloworld_handler这个函数。本例子是一个非常简单的内容生成器。

C代码

  1. if(strcmp(r->handler,"helloworld")){//判断是否是这个helloworldhandler
  2. returnDECLINED;//
  3. }
  4. r->content_type="text/html";
  5. if(!r->header_only)
  6. ap_rputs("Thesamplepagefrommod_helloworld.c\n",r);//内容生成
  7. returnOK;

if (strcmp(r->handler, "helloworld")) {//判断是否是这个helloworld handler
return DECLINED;//
}
r->content_type = "text/html";
if (!r->header_only)
ap_rputs("The sample page from mod_helloworld.c\n", r);//内容生成
return OK;

第三步:编译

# apxs -c mod_helloworld.c
/usr/lib/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -fno-strict-aliasing -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -pthread -I/usr/include/httpd -I/usr/include/apr-1 -I/usr/include/apr-1 -c -o mod_helloworld.lo mod_helloworld.c && touch mod_helloworld.slo
/usr/lib/apr-1/build/libtool --silent --mode=link gcc -o mod_helloworld.la -rpath /usr/lib/httpd/modules -module -avoid-version mod_helloworld.lo

ls后发现目录下的确多了几个文件,其中就有一个mod_helloworld.la的,于是再调用

#apxs -i mod_helloworld.la

apache的Module目录下就多了一个mod_helloworld.so

再在httpd.conf中加入这一Module:

LoadModule helloworld_module /usr/lib/httpd/modules/mod_helloworld.so

<Location /helloworld>
SetHandler helloworld
</Location>

重启apache 然后输入 http://loacalhost/helloworld 就可以看到
The sample page from mod_helloworld.c

当然这里这里只是输出一句话,我们也可以打印很多html信息,就类似于servlet一样。

这样一来一个简单的apache内容生成器模块已经开发好了,当然应用比较广泛的是过滤器模块的开发,最近项目主要也是用过滤器来实现的。

apache 可以开发出一些功能非常强大的模块来,可以为我们定制更好的apache,比如容器中应用的流量统计,cpu统计等。

网上还有几篇写的可参考的:

http://www.cnblogs.com/ithurricane/archive/2009/01/01/1366312.html

http://www.cnblogs.com/DeadKnight/archive/2010/04/08/1707444.html

http://hi.baidu.com/kylelee/blog/item/f13fef2c7366bceb8b1399c9.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值