Linux 下 Apache 模块开发

10 篇文章 0 订阅

转载自 http://blog.csdn.net/21aspnet/article/details/6621621


环境:CentOS

第一步:安装Apache的apxs

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

 

输入命令查看是否有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.   
  6. /* The sample content handler */  
  7. static int helloworld_handler(request_rec *r)   
  8. {   
  9.     if (strcmp(r->handler, "helloworld")) {   
  10.         return DECLINED;   
  11.     }   
  12.     r->content_type = "text/html";         
  13.   
  14.     if (!r->header_only)   
  15.         ap_rputs("The sample page from mod_helloworld.c\n", r);   
  16.     return OK;   
  17. }   
  18.   
  19. static void helloworld_register_hooks(apr_pool_t *p)   
  20. {   
  21.     ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);   
  22. }   
  23. /* Dispatch list for API hooks */  
  24. module AP_MODULE_DECLARE_DATA helloworld_module = {   
  25.     STANDARD20_MODULE_STUFF, //用于编译后的模块产生版本信息   
  26.     NULL,                  /* 创建目录配置结构*/  
  27.     NULL,                  /* 合并目录配置结构 */  
  28.     NULL,                  /* 创建主机配置结构 */  
  29.     NULL,                  /* 合并主机配置结构 */  
  30.     NULL,                  /* 为模块配置相关指令       */  
  31.     helloworld_register_hooks  /* 注册模块的钩子函数                      */  
  32. };  

#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")) {//判断是否是这个helloworld  handler   
  2.         return DECLINED;//   
  3.     }   
  4.     r->content_type = "text/html";         
  5.     if (!r->header_only)   
  6.         ap_rputs("The sample page from mod_helloworld.c\n", r);//内容生成   
  7.     return OK;  

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统计等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值