apache模块开发
解决公司jboss集群的问题,开始学习apache模块开发,开发的平台采用windows平台,是希望大家更加容易能够进入开发阶段,公司大部分都有vs2008.1.安装apache 安装包 httpd-2.2.21-win32-x86-no_ssl.msi
一定要custom全部安装,否则就不会有include和lib目录
2.配置apxs
1)安装apxs 安装包apxs_win32
2)安装Strawberry Perl 安装包strawberry-perl-5.16.3.1-32bit.msi
3)进入dos命令提示符,转到apxs安装目录下,输入perl Configure.pl,按要求填写apache的安装目录...\apache2.2和命令名称“httpd.exe”
4)通过上一步,就会在apache2.2\bin下生成apxs命令,并且在apache2.2目录下生成了build目录
5)修改在apache2.2下build目录中的config_vars.mk文件
将CC = gcc 的gcc改为cl.exe ,LD = g++的g++改为link.exe,CPP = gcc-E的gcc-E删掉
6)设置apache下的bin的命令apxs的路径为环境变量,以放便在不进入具体安装目录下运行apxs
3.编译apache模块
1)运行Visual Studio 2008 命令提示(在开始的Microsoft Visual Studio 2008下可以找到)
2)运行apxs -g -n helloworld(helloworld为模块名),会生成一个叫helloworld的目录和模板代码
3)进入helloworld目录,编辑mod_helloworld.c
4)运行apxs -c -i -a mod_helloworld.c libapr-1.lib libaprutil-1.lib libapriconv-1.lib libhttpd.lib,生成mod_helloworld.so
5)将mod_helloworld.so拷贝到Apache2.2\modules下
6)修改Apache2.2\conf\httpd.conf,在末尾加上
LoadModule helloworld_module \modules\mod_helloworld.so
<Location /helloworld>
setHandler helloworld
</Location>
7)启动apache,在IE里输入http://loacalhost/helloworld
/*
**mod_helloworld.c -- Apache sample helloworld module
**
**
**To play with this sample module first compile it into a
**DSO file and install it into Apache's modules directory
**by running:
**
** $ apxs -c -i mod_helloworld.c
**
**Then activate it in Apache's httpd.conf file for instance
**for the URL /helloworld in as follows:
**
** # httpd.conf
** LoadModule helloworld_module modules/mod_helloworld.so
** <Location /helloworld>
** SetHandler helloworld
** </Location>
**
**Then after restarting Apache via
**
** $ apachectl restart
**
**you immediately can request the URL /helloworld and watch for the
**output of this module. This can be achieved for instance via:
**
** $ lynx -mime_header http://localhost/helloworld
**
**The output should be similar to the following one:
**
** HTTP/1.1 200 OK
** Date: Tue, 31 Mar 1998 14:42:22 GMT
** Server: Apache/1.3.4 (Unix)
** Connection: close
** Content-Type: text/html
**
** The sample page from mod_helloworld.c
*/
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
static int printitem(void *rec,const char *key,const char *value)
{
request_rec *r = rec;
ap_rprintf(r,"<tr><th scope=\"row\">%s</th><td>%s</td></tr>\n",ap_escape_html(r->pool,key),ap_escape_html(r->pool,value));
return 1;
}
static void printtable(request_rec *r,apr_table_t *t,const char *caption,const char *keyhead,const char *valhead)
{
ap_rprintf(r,"<table><caption>%s</caption></table><thead><tr><th scope=\"col\">%s</th><th scope=\"col\">%s</th></tr></thead><tbody>",caption,keyhead,valhead);
apr_table_do(printitem,r,t,NULL);
ap_rputs("</tbody></table>\n",r);
}
/* The sample content handler */
static int helloworld_handler(request_rec *r)
{
ap_set_content_type(r,"text/html;charset=ascii");
ap_rputs("<!DOCUMENT HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n"
"<html><head><title>Apache module devlop</title></head><body><h1>apache is back</h1>",r);
printtable(r,r->headers_in,"request headers","header","value");
printtable(r,r->headers_out,"response headers","header","value");
ap_rputs("apache module develop from ibyoung.c\n", r);
if (strcmp(r->handler, "helloworld")) {
return DECLINED;
}
r->content_type = "text/html";
if (!r->header_only)
ap_rputs("run success.c\n", r);
ap_rputs("</body></html>",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, /* create per-dir config structures */
NULL, /* mergeper-dir config structures */
NULL, /* create per-server config structures */
NULL, /* mergeper-server config structures */
NULL, /* table of config file commands */
helloworld_register_hooks/* register hooks */
};
页:
[1]