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代碼
- #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??/*?注冊模塊的鉤子函數??????????????????????*/??
- };??
#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代碼
- 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代碼
- 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;??
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
總結
以上是生活随笔為你收集整理的Apache模块开发helloworld无错版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天空套多少钱啊?
- 下一篇: 在ApacheHTTPD服务器中使用DS