环境:CentOS 5.4

第一步:安装Apache的apxs

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

apxs可参考官方文档

http://lamp.linux.gov.cn/Apache/ApacheMenu/programs/apxs.html

输入命令查看是否有httpd-devel这个包,如果没有需要安装

#rpm -qa|grep httpd

# yum -y install httpd-devel

利用指令确认其已经安装

# which apxs
/use/sbin/apxs

也可以这样查找全部

#find / | grep apxs

第二步:apxs -g -n helloworld

上面的命令可以帮助我们产生一个模块名字为helloworld的模板。
上面的命令会产生以下代码

C代码

  1. #include "httpd.h"
  2. #include "http_config.h"
  3. #include "http_protocol.h"
  4. #include "ap_config.h"
  5. /* The sample content handler */
  6. static int helloworld_handler(request_rec *r)
  7. {
  8. if (strcmp(r->handler, "helloworld")) {
  9. return DECLINED;
  10. }
  11. r->content_type = "text/html";
  12. if (!r->header_only)
  13. ap_rputs("The sample page from mod_helloworld.c\n", r);
  14. return OK;
  15. }
  16. static void helloworld_register_hooks(apr_pool_t *p)
  17. {
  18. ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
  19. }
  20. /* Dispatch list for API hooks */
  21. module AP_MODULE_DECLARE_DATA helloworld_module = {
  22. STANDARD20_MODULE_STUFF, //用于编译后的模块产生版本信息
  23. NULL,                  /* 创建目录配置结构*/
  24. NULL,                  /* 合并目录配置结构 */
  25. NULL,                  /* 创建主机配置结构 */
  26. NULL,                  /* 合并主机配置结构 */
  27. NULL,                  /* 为模块配置相关指令       */
  28. helloworld_register_hooks  /* 注册模块的钩子函数                      */
  29. };

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

C代码

  1. ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);

ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);

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

C代码

  1. if (strcmp(r->handler, "helloworld")) {//判断是否是这个helloworld  handler
  2. return DECLINED;//
  3. }
  4. r->content_type = "text/html";
  5. if (!r->header_only)
  6. ap_rputs("The sample page from mod_helloworld.c\n", r);//内容生成
  7. return OK;

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
/usr/lib/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -fno-strict-aliasing  -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -pthread -I/usr/include/httpd  -I/usr/include/apr-1   -I/usr/include/apr-1   -c -o mod_helloworld.lo mod_helloworld.c && touch mod_helloworld.slo
/usr/lib/apr-1/build/libtool --silent --mode=link gcc -o mod_helloworld.la  -rpath /usr/lib/httpd/modules -module -avoid-version    mod_helloworld.lo

ls后发现目录下的确多了几个文件,其中就有一个mod_helloworld.la的,于是再调用

# apxs -i mod_helloworld.la

apache的Module目录下就多了一个mod_helloworld.so

再在httpd.conf中加入这一Module:

LoadModule helloworld_module /usr/lib/httpd/modules/mod_helloworld.so

<Location /helloworld>
        SetHandler helloworld
</Location>

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

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

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

apache 可以开发出一些功能非常强大的模块来,可以为我们定制更好的apache,比如容器中应用的流量统计,cpu统计等。

网上还有几篇写的可参考的:

http://www.cnblogs.com/ithurricane/archive/2009/01/01/1366312.html

http://www.cnblogs.com/DeadKnight/archive/2010/04/08/1707444.html

http://hi.baidu.com/kylelee/blog/item/f13fef2c7366bceb8b1399c9.html

Apache模块开发helloworld无错版相关推荐

  1. Apache模块开发

    一.简介 Apache HTTP服务器是一个模块化的软件,使管理者可以选择核心中包含的模块以裁剪功能.可以在编译时选择被静态包含进httpd二进制映象的模块,也可以编译成独立于主httpd二进制映象的 ...

  2. 服务器架设笔记——Apache模块开发基础知识

    通过上节的例子,我们发现Apache插件开发的一个门槛便是学习它自成体系的一套API.虽然Apache的官网上有对这些API的详细介绍,但是空拿着一些零散的说明书,是很难快速建立起一套可以运行的系统. ...

  3. apache c语言源码,Apache模块开发/用C语言扩展apache(2:APR编程介绍)

    Apache模块开发/用C语言扩展apache(2:APR编程介绍) by linux_prog 可以看到apache代码中使用了大量的以apr_开头的结构或者函数,这些其实是APR. 什么是apr  ...

  4. 用c++来开发php的底层模块|用c++来开发apache模块,Apache模块开发实例(2)

    三.常见问题 1.常见错误 apxs:Error: Sorry, cannot determine bootstrap symbol name. apxs:Error: Please specify ...

  5. Apache HTTP 过滤器filter、开源WAF(ModSecurity)、Apache 模块开发

    文章目录 一.安全基础 1. 什么是XSS攻击 2. SQL Injection 3. OWASP的核心规则集 二.ModSecurity 1. ModSecurity简介 1.1 ModSecuri ...

  6. C语言-apache mod(模块开发)-采用apxs开发实战(centos7.2 linux篇)

    C语言-apache mod(模块开发)-采用apxs开发实战(centos7.2 linux篇) 名词解释:apxs apxs is a tool for building and installi ...

  7. php magento 开发,magento 2模块开发实例helloworld模块 hello world 程序 c语言hello world代码 c语言hello worl...

    data-id="1190000005008433" data-license="cc"> 1.在app/etc/config.php中添加自定义的模块( ...

  8. 火鸟地方门户3.9全能完整无错版源码 新增二手车模块

    火鸟地方门户3.9全能完整无错版源码,新增分站自定义配置SEO.LOGO.模板功能,新增二手车模块,房产发布功能全新改版等近30项改进,压缩包内包含详情的测试配置安装测试教程. 火鸟门户采用新的模板引 ...

  9. 02-Maven高级-分模块开发、依赖传递、聚合、继承(SpringBoot的部分底层原理)、多模块开发(环境切换)、Nexus私服搭建与使用

    文章目录 学习目标 一.分模块开发与设计 1. 分模块开发的意义 问题导入 模块拆分原则 2. 分模块开发(模块拆分) 问题导入 2.1 创建Maven模块 2.2 书写模块代码 2.3 通过mave ...

最新文章

  1. 实战解析:真实AI场景下,极小目标检测与精度提升 | 百度AI公开课
  2. Python基础知识(第十一天)
  3. centos安装mysql 简书_在centos上安装mysql
  4. 普罗米修斯监控java项目_java学到什么程度可以出去实习?
  5. “人工智障”,我们还能忍你多久?
  6. tornado和subprocess实现程序的非堵塞异步处理
  7. js学习笔记——函数定义
  8. Node.js编写CLI的实践
  9. python lnum_python 基础笔记 — 数据类型之数字
  10. win10笔记本电脑双系统 安装黑苹果系统macOS 小白黑苹果乐园下载资源简便安装黑苹果方式,非常详细,还有资源!
  11. ROS使用节点句柄nh(“~“)和nh的区别:发布的话题的命名空间不同
  12. C# 温故而知新:Stream篇(五)
  13. 清洁代码之道:一份实用关于如何编写和维护干净整洁的好代码的的方法 The Art Of Clean Code...
  14. 微信运营必备!专业的微信粉丝和社群管理工具!功能强大好用,而且免费!
  15. 阿里云服务器qq邮箱无法推送问题
  16. Android实现九宫格图案解锁
  17. SSH连接慢问题解决
  18. 网络编程及TCP/UDP协议
  19. 姚明全部比赛录像合集【百度网盘高清分享】
  20. 从零开始的全栈工程师——html篇1.3

热门文章

  1. 翻转矩阵(数组右移问题)
  2. 崇阳计算机技校,湖北省崇阳县龙翔技工学校
  3. linux web高级编程,寒假学习 第16.17天 (linux 高级编程)
  4. centos7.4.3 部署python-pcl亲测可用(采坑记)
  5. linux rpm找不到命令_Linux安装软件
  6. Open3d 学习计划—12(Jupyter 可视化)
  7. OpenResty中遇到Can't locate Time/HiRes.pm in @INC问题的解决方法
  8. 如何从ATS获取客户端平均响应时间(单位,毫秒)?
  9. LSM 优化系列(六)-- 【ATC‘20】MatrixKV : NVM 的PMEM 在 LSM-tree的write stall和写放大上的优化
  10. leetcode-402 移掉K位数组