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\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里面去的时候,他会执行这个注册函数。在这个函数里面我们将会注册我们所要添加的钩子。 
本例子中我们用的是

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\n", 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,才能成功,否则会出现错误

Ubuntu 下apache2 增加新的module的更多相关文章

  1. ubuntu下Apache2配置

    Ubuntu下Apache2的CGI简单配置:http://blog.csdn.net/a623891391/article/details/47170355 Ubuntu Apache的安装和配置以 ...

  2. Ubuntu下apache2安装配置(内含数字证书配置)

    Ubuntu下apache2安装配置(内含数字证书配置)安装命令:sudo apt-get updatesudo apt-get install apache2 配置1.查看apache2安装目录命令 ...

  3. Ubuntu 下 Apache2 和 PHP 服务器环境配置

    Ubuntu 下 Apache2 和 PHP 服务器环境配置 1.简介 本文主要是 Ubuntu 下 Apache2 和 PHP 服务器环境配置方法,同样适用于 Debian 系统:Ubuntu 20 ...

  4. ubuntu下apache2 安装 配置 卸载 CGI设置 SSL设置

    一.安装.卸载apache2      apache2可直接用命令安装           sudo apt-get install apache2      卸载比较麻烦,必须卸干净,否则会影响ap ...

  5. Ubuntu下apache2启动、停止、重启、配置

    Linux系统为Ubuntu 一.Start Apache 2 Server /启动apache服务# /etc/init.d/apache2 startor$ sudo /etc/init.d/ap ...

  6. 在Ubuntu下创建一个新的用户

    Step1:添加新用户useradd -r -m -s /bin/bash 用户名 Step2:配置新用户密码passwd 用户名 Step3:给新添加的用户增加ROOT权限vim /etc/sudo ...

  7. Ubuntu 下apache2开启rewrite隐藏index.php

    为了实现 http://www.example.com/route/route 而不是 http://www.example.com/index.php/route/route 需要开启apache2 ...

  8. ubuntu下apache2使用的简单总结

        一. 修改apache2原80端口为90端口 1. 修改/etc/apache2/ports.conf, 将端口80改为90,443,改为444 2. 修改/etc/apache2/sites ...

  9. Ubuntu下Apache2+Tomact7安装、配置及整合

    安装Apache2 命令:apt-get install apache2 cd  /etc/apache2 打开apache.conf 加入 ServerName localhostDirectory ...

随机推荐

  1. RabbitMQ (十) 远程过程调用(RPC)

    在远程计算机上运行一个函数并等待结果,我们通常叫这种模式为远程过程调用或者RPC. 通过 RabbitMQ 进行 RPC 很容易,客户端发送请求消息,服务器回复响应消息.为了接收响应,我们需要发送带有 ...

  2. Visual Studio警告IDE0006的解决办法

     Visual Studio警告IDE0006的解决办法 Visual Studio警告IDE0006虽然给出明确的操作过程,但是在实施的过程中,还是有很多地方需要注意.下面以官方的信息,介绍一下注意 ...

  3. [Codeforces #196] Tutorial

    Link: Codeforces #196 传送门 A: 枚举 #include <bits/stdc++.h> using namespace std; #define X first ...

  4. 【KMP模板】POJ3461-Oulipo

    [题意] 找出第一个字符串在第二个字符串中出现次数. [注意点] 一定要先将strlen存下来,而不能每次用每次求,否则会TLE! #include<iostream> #include& ...

  5. 1.创建spring cloud父工程和子模块

    创建父工程 idea创建父工程 idea创建一个工程.父工程管理公共资源 添加子模块 选择添加到父工程里面spring_cloud_parent 相应的子模块添加到父工程的pom.xml文件里

  6. SpringBoot整合Mybatis多数据源 (AOP+注解)

    SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding=& ...

  7. Semaphore(信号量)源码分析

    1. Semaphore Semaphore和ReentrantReadWriteLock.ReadLock(读锁)都采用AbstractOwnableSynchronizer共享排队的方式实现. 关 ...

  8. [转]提示错误 package javax.servlet.jsp does not exist package javax.servletr.jsp.tagext does not exist

    你在JAVA servlet容器运行的时候没配置servlet-api.jar,tools.jar,rt.jar,jsp-api.jar的classpath 我的classpath= .;%JAVA_ ...

  9. iOS GCD NSOperation NSThread等多线程各种举例详解

    废话就不多说,直接上干货.如下图列举了很多多线程的知识点,每个按钮都写有对应的详细例子,并对运行结果进行分析,绝对拿实践结果来说话.如果各位道友发现错误之处还请指正.附上demo下载地址

  10. 【shiro】报错: If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying.

    spring整合shiro,项目报如下错误: ==============异常开始============= java.lang.IllegalStateException: The mapped c ...