自己写apache module
2009-09-14 9:08 am最近工作需要,需要在apache上做一些东西,找了不少的模块,没有特别满意的,所以有自己写module的想法,
当然知道并不是那么简单,不过就做个开始吧,今天下午抽空做了第一个module(其实完全就是测试写module的流程,没有实质的功能),过程如下:
1,找到apxs(apache的扩展工具),/path/apxs -g -n hello
这样会生成一个hello的目录,cd,能看到有个mod_hello.c的文件和make文件;
2,打开c文件,代码如:
40 #include "httpd.h"
41 #include "http_config.h"
42 #include "http_protocol.h"
43 #include "ap_config.h"
44
45 /* The sample content handler */
46 static int hello_handler(request_rec *r)
47 {
48 if (strcmp(r->handler, "hello")) {
49 return DECLINED;
50 }
51 r->content_type = "text/html";
52
53 if (!r->header_only)
54 ap_rputs("hello world,my first apache mod.n", r);
55 return OK;
56 }
很容易看懂,虽然不了解apache提供的函数,修改后保存;
3,编译,/path/apxs -c -i mod_hello.c,再make install .然后再apache的module目录下能找到
mod_hello.so文件;
4,在httpd.conf里面加入:
417 LoadModule hello_module modules/mod_hello.so 418 419 SetHandler hello 420
5,重启apache,在http://localhost/hello 就能看到输出的内容了。
这只是开始,接下来需要去了解apache的提供的函数,接着就用你的c去自由发挥吧。