灵感:
应业务的要求,我需要在Apache上扩展出自定义的功能模块,以便处理特殊请求,study了半天,终于摸透了编写Apache扩展模块编写流程。为了方便以后记忆及查询,特记录此篇
资料:
Apache API查询:https://ci.apache.org/projects/httpd/trunk/doxygen/index.html
Apache 模块说明:https://httpd.apache.org/docs/2.4/developer/modguide.html
例子1:https://www.cnblogs.com/274914765qq/p/4453315.html
例子2:http://blog.youkuaiyun.com/hqin6/article/details/6166750
例子3:https://www.cnblogs.com/baochuan/archive/2012/03/27/2418789.html
环境:
操作系统:Ubuntu 17.10 server 64bit
Apache版:Apache2
Apache API:Apache2.0 Handler
PHP版本:7.1
Apache modules目录:/usr/lib/apache2/modules
Apache 运行目录:/etc/apache2
Apache 网页目录:/var/www/html
Apache 配置文件位置:/etc/apache2/apache2.conf
Apache启停操作
Apachectl stop
apachectl start
apachectl restart
/etc/init.d/apache2 stop
/etc/init.d/apache2 start
/etc/init.d/apache2 restart
工具
apxs
没有则安装:sudo apt install apache2-dev
默认示例
在任意目录(你想放源文件的目录,通常是home/usr)使用命令:apxs -g -n hello 将会生成一个hello文件夹,并把所有文件放到里面。
hello文件夹初始内容:
Makefile
modules.mk
mod_hello.c
.deps
其中
Makefile文件:
##
## Makefile -- Build procedure for sample hello Apache module
## Autogenerated via ``apxs -n hello -g''.
##
builddir=.
top_srcdir=/usr/share/apache2
top_builddir=/usr/share/apache2
include /usr/share/apache2/build/special.mk
# the used tools
APACHECTL=apachectl
# additional defines, includes and libraries
#DEFS=-Dmy_define=my_value
#INCLUDES=-Imy/include/dir
#LIBS=-Lmy/lib/dir -lmylib
# the default target
all: local-shared-build
# install the shared object file into Apache
install: install-modules-yes
# cleanup
clean:
-rm -f mod_hello.o mod_hello.lo mod_hello.slo mod_hello.la
# simple test
test: reload
lynx -mime_header http://localhost/hello
# install and activate shared object by reloading Apache to
# force a reload of the shared object file
reload: install restart
# the general Apache start/restart/stop
# procedures
start:
$(APACHECTL) start
restart:
$(APACHECTL) restart
stop:
$(APACHECTL) stop
mod_hello.c文件:
/*
** mod_hello.c -- Apache sample hello module
** [Autogenerated via ``apxs -n hello -g'']
**
** To play with this sample module first compile it into a
** DSO file and install it into Apache's modules directory
** by running:
**
** $ apxs -c -i mod_hello.c
**
** Then activate it in Apache's apache2.conf file for instance
** for the URL /hello in as follows:
**
** # apache2.conf
** LoadModule hello_module modules/mod_hello.so
** <Location /hello>
** SetHandler hello
** </Location>
**
** Then after restarting Apache via
**
** $ apachectl restart
**
** you immediately can request the URL /hello and watch for the
** output of this module. This can be achieved for instance via:
**
** $ lynx -mime_header http://localhost/hello
**
** The output should be similar to the following one:
**
** HTTP/1.1 200 OK
** Date: Tue, 31 Mar 1998 14:42:22 GMT
** Server: Apache/1.3.4 (Unix)
** Connection: close
** Content-Type: text/html
**
** The sample page from mod_hello.c
*/
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
/* The sample content handler */
static int hello_handler(request_rec *r)
{
if (strcmp(r->handler, "hello")) {
return DECLINED;
}
r->content_type = "text/html";
if (!r->header_only)
ap_rputs("The sample page from mod_hello.c\n", r);
return OK;
}
static void hello_register_hooks(apr_pool_t *p)
{
ap_hook_handler(hello_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA hello_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
hello_register_hooks /* register hooks */
};
modules.mk文件:
mod_hello.la: mod_hello.slo
$(SH_LINK) -rpath $(libexecdir) -module -avoid-version mod_hello.lo
DISTCLEAN_TARGETS = modules.mk
shared = mod_hello.la
编译及引用流程
到hello目录里编译安装:apxs -c -i mod_hello.c
编译后会自动生成mod_hello.so文件,当前目录下会生成一个.libs目录,请查阅
同时会复制到Apache module目录下,自动更改权限为644
在/etc/apache2/mods-enabled目录下新建文件:(被“apache2.conf”自动引用)
hello.conf文件:
<IfModule mod_hello.c>
<Location /hello>
SetHandler hello
</Location>
<IfModule mod_hello.c>
</IfModule>
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
hello.load文件:
LoadModule hello_module /usr/lib/apache2/modules/mod_hello.so
重启Apache服务器即可。
实验现象
在浏览器中输入:http://网址/hello
说明
我的Apache服务器似乎没有httpd。
最后一张图内容不必较真