1、编译安装Apache、比如安装了httpd-2.4.4版本

2、写好模块文件、如新增一个 mod_helloworld.c

/*    
02     * File:   helloworld.c    
03     * Author: luojie    
04     *    
05     * Created on March 25, 2015, 4:18 PM    
06     */    
07    
08    #include <httpd.h>    
09    #include <http_protocol.h>    
10    #include "http_config.h"    
11    
12    /*    
13     *    
14     */    
15    static int helloworld_handler(request_rec *r){    
16    
17        if(!r->handler || strcmp(r->handler,"helloworld")){    
18            return DECLINED;    
19        }    
20    
21        ap_set_content_type(r,"text/html;charset=ascii");    
22        ap_rputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD/ HTML 4.01//EN\">\n",r);    
23        ap_rputs("<html><head><title>Apache HelloWorld Module </title></head>",r);    
24        ap_rputs("<body><h1>Hello world ! HelloWorld Module </h1> </body></html>",r);    
25    
26        return OK;    
27    }    
28    
29    /*    
30     *    
31     */    
32    static void helloworld_hooks(apr_pool_t *pool){      
33        ap_hook_handler(helloworld_handler,NULL,NULL,APR_HOOK_MIDDLE);    
34    }    
35    
36    
37    /*    
38     *    
39     */    
40    module AP_MODULE_DECLARE_DATA helloworld = {    
41        STANDARD20_MODULE_STUFF,    
42        NULL,    
43        NULL,    
44        NULL,    
45        NULL,    
46        NULL,    
47        helloworld_hooks    
48    };


3、编译、利用apxs工具 


cd /usr/local/httpd/bin

./apxs -a -c mod_helloworld.c

./apxs -i -a -n helloworld mod_helloworld.la


4、修改HTTP配置文件http.conf

LoadModule helloworld  modules/mod_helloworld.so


5、重启Apache

./apachectl restart


6、其中编译参数

-n:指定装载模块的名字。如果不使用这个选项,那么装载进来的模块将是以so为后缀的文件名。
     -q:查询Apache的一些基本配置。例如,/usrflocal/apache/bin/apxs -q PREFIX将显示PREFIX的路径。 
     -s:更改用.q参数查询到的参数。例如,/usrfloca/apache/apxs -sPREFIX-/usr/local/apachel.
     -c:编译模块。
     -i:安装模块。
     -a:自动添加LoadModule到hrtpd.conf文件中。


7、访问测试

http://127.0.0.1/helloworld

cbf5b1efea4180971b57b617f0065a78.png


8、常见错误

apxs:Error: Sorry, cannot determine bootstrap symbol name. apxs:Error: Please specify one with option `-n'.
加个-n参数,给模块起个名字

httpd: Syntax error on line 164 of /usr/local/httpd/conf/httpd.conf: Can't locate API module structure `helloworld_module' in file /usr/local/httpd/modules/mod_helloworld.so: /usr/local/httpd/modules/mod_helloworld.so: undefined symbol: helloworld_module

编辑http.conf文件