最近学习windows下apache模块开发,在网上找了很多资料,发现都不是很全,经过一天的折腾,终于搞定,现贡献出来供大家一起学习:)
安装apache 安装包 httpd-2.2.21-win32-x86-no_ssl.msi 一定要custom全部安装,否则就不会有include和lib目录
或者httpd-2.4.27 笔者使用后者非安装包
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.4\bin下生成apxs命令,并且在apache2.4目录下生成了build目录
5)修改在apache2.4下build目录中的config_vars.mk文件
最新版本不需要修改了
将CC = gcc 的gcc改为cl.exe ,LD = g++的g++改为link.exe,CPP = gcc-E的gcc-E删掉
6)设置apxs的路径为环境变量,以放便在不进入具体安装目录下运行apxs
3.编译apache模块
1)运行Visual Studio 2015 命令提示(在开始的Microsoft Visual Studio 2015下可以找到)
2)运行apxs -g -n helloworld(helloworld为模块名),会生成一个叫helloworld的目录和模板代码(一般放在user目录下)
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.4\modules下
6)修改Apache2.4\conf\httpd.conf,在末尾加上
LoadModule helloworld_module \modules\mod_helloworld.so
<Location /helloworld>
setHandler helloworld
</Location>
7)启动apache,在浏览器里输入http://loacalhost:端口号/helloworld,就可以看到我们返回的内容,比如本例“
The sample page from mod_helloworld”:)大功告成
#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, /* create per-dir config structures */ NULL, /* merge per-dir config structures */ NULL, /* create per-server config structures */ NULL, /* merge per-server config structures */ NULL, /* table of config file commands */ helloworld_register_hooks /* register hooks */ };
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
apxs -c C:\helloworld\mod_helloworld.c libapr-1.lib libaprutil-1.lib libapriconv-1.lib libhttpd.lib
PAUSE
微信
支付宝