zoukankan      html  css  js  c++  java
  • Ubuntu 下apache2 增加新的module

    http://andrew913.iteye.com/blog/398648

    首先来介绍下apache的一个工具apxs。apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象,使之可以用由mod_so提供的LoadModule指令在运行时加载到Apache服务器中。 

    注意在Ubuntu apache2下 apxs为apxs2,另外非源码包安装的apache2 是不带有apxs2 的。

    解决办法:
    apt-get install apache2-prefork-dev


    1.apxs -g -n helloworld 

    上面的命令可以帮助我们产生一个模块名字为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
    ", 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里面去的时候,他会执行这个注册函数。在这个函数里面我们将会注册我们所要添加的钩子。 
    本例子中我们用的是 

    ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);  

    这个处理函数,这个处理函数注册了helloworld_handler这个函数。这个函数用于处理我们的请求。 
    我们来讲下执行的顺序,模块加载-》执行helloworld_register_hooks函数-》注册helloworld_handler这个函数到钩子上去。 
    这样一来:当一个http请求来的时候,我们就会自动去执行helloworld_handler这个函数。本例子是一个非常简单的内容生成器。 

    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
    ", r);//内容生成
        return OK;

    下面我们将来进行编译 
    执行:apxs -c mod_helloworld.c 
    执行成功以后我们可以发现在.libs下面会出现mod_helloworld.so这个模块文件。这就是我们所需要的,对于apache一些自带的模块一般都放在安装目录的modules/下面。 

    下面我们来对其进行配置 
    打开httpd.conf文件,添加以下信息 

    LoadModule helloworld_module   *****// 指定.so文件的路径。
    
    <Location /helloworld>
        setHandler helloworld
    </Location>

    Ok ,重启apache 然后输入 http://loacalhost/helloworld 就可以看到 
    The sample page from mod_helloworld.c 

    当然这里这里只是输出一句话,我们也可以打印很多html信息,就类似于servlet一样。 

    这样一来一个简单的apache内容生成器模块已经开发好了,当然应用比较广泛的是过滤器模块的开发,最近项目主要也是用过滤器来实现的。 

    Ubuntu apache2下默认的模块都在 /usr/lib/apache2/modules/下面,

    重启apache时,需要sudo  /etc/init.d/apache2 restart,才能成功,否则会出现错误

  • 相关阅读:
    【转】ServletContext介绍及用法
    【转】UML之类图和对象图
    【转】UML各种图总结
    解决win10下 matplotlib绘图时中文乱码问题
    修改表、字段的默认字符集
    MySQL报错Incorrect date value: '0000-00-00' for column 'hirrdate' at row 1
    用vs code将qt designer的.ui文件转换为.py文件
    MySQL多表数据查询记录
    MySQL中统计函数和分组数据查询
    lambda匿名函数
  • 原文地址:https://www.cnblogs.com/diegodu/p/4250364.html
Copyright © 2011-2022 走看看