Ubuntu 下apache2 增加新的module
http://andrew913.iteye.com/blog/398648
首先来介绍下apache的一个工具apxs。apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象,使之可以用由mod_so提供的LoadModule指令在运行时加载到Apache服务器中。
注意在Ubuntu apache2下 apxs为apxs2,另外非源码包安装的apache2 是不带有apxs2 的。
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的更多相关文章
- ubuntu下Apache2配置
Ubuntu下Apache2的CGI简单配置:http://blog.csdn.net/a623891391/article/details/47170355 Ubuntu Apache的安装和配置以 ...
- Ubuntu下apache2安装配置(内含数字证书配置)
Ubuntu下apache2安装配置(内含数字证书配置)安装命令:sudo apt-get updatesudo apt-get install apache2 配置1.查看apache2安装目录命令 ...
- Ubuntu 下 Apache2 和 PHP 服务器环境配置
Ubuntu 下 Apache2 和 PHP 服务器环境配置 1.简介 本文主要是 Ubuntu 下 Apache2 和 PHP 服务器环境配置方法,同样适用于 Debian 系统:Ubuntu 20 ...
- ubuntu下apache2 安装 配置 卸载 CGI设置 SSL设置
一.安装.卸载apache2 apache2可直接用命令安装 sudo apt-get install apache2 卸载比较麻烦,必须卸干净,否则会影响ap ...
- Ubuntu下apache2启动、停止、重启、配置
Linux系统为Ubuntu 一.Start Apache 2 Server /启动apache服务# /etc/init.d/apache2 startor$ sudo /etc/init.d/ap ...
- 在Ubuntu下创建一个新的用户
Step1:添加新用户useradd -r -m -s /bin/bash 用户名 Step2:配置新用户密码passwd 用户名 Step3:给新添加的用户增加ROOT权限vim /etc/sudo ...
- Ubuntu 下apache2开启rewrite隐藏index.php
为了实现 http://www.example.com/route/route 而不是 http://www.example.com/index.php/route/route 需要开启apache2 ...
- ubuntu下apache2使用的简单总结
一. 修改apache2原80端口为90端口 1. 修改/etc/apache2/ports.conf, 将端口80改为90,443,改为444 2. 修改/etc/apache2/sites ...
- Ubuntu下Apache2+Tomact7安装、配置及整合
安装Apache2 命令:apt-get install apache2 cd /etc/apache2 打开apache.conf 加入 ServerName localhostDirectory ...
随机推荐
- Linux命令之rlogin
rlogin [-8EKLdx] [-e char] [-l username] host rlogin在远程主机host上开始一个终端会话. (1).选项 -8 选项允许进行8位的输入数据传送:否则 ...
- JSON APIs and Ajax
1. 通过jQuery来绑定点击事件. 函数 $(document).ready()这个函数中的代码只会在我们的页面加载时候运行一次,确保执行js之前页面所有的dom已经准备就绪. 在$(docume ...
- bytes2HexString
public static String bytes2HexString(byte[] b) { String r = ""; for (int i = 0; i < b.l ...
- run()和start()的区别
run没有启新的线程,start方法才会调用Thread的native的start0方法,start0会调用run方法,开启新的线程,博主这么做,不是多线程顺序执行,而是把业务阻塞在主线程里.请打印: ...
- vijos1889:天真的因数分解
题目链接 vijos1889:天真的因数分解 题解 同bzoj2440: [中山市选2011]完全平方数 就是改成了求有平方因子数,依旧考虑二分,只是把容斥系数取一下相反数,也就是把莫比乌斯函数求一个 ...
- [USACO 2017 Feb Gold] Tutorial
Link: 传送门 A: 分层图最短路(其实就是最短路转移时多记录一维的数据 #include <bits/stdc++.h> using namespace std; #define X ...
- poj 1348 Period(KMP)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 【动态规划+二分查找】POJ2533&POJ1631最长上升子序列(LIS)
POJ2533裸的LIS,时间复杂度为O(n^2) #include<iostream> #include<cstdio> using namespace std; +; in ...
- Python学习札记
Python是很多公司都在使用的一种脚本语言,其语法与Perl.C++.JAVA等都大同小异.本文仅对一些比较常用的语法结构进行总结,比如字典.列表.正则匹配.读写文件等.供广大喜爱Python的同学 ...
- 8VC Venture Cup 2016 - Elimination Round G. Raffles 线段树
G. Raffles 题目连接: http://www.codeforces.com/contest/626/problem/G Description Johnny is at a carnival ...