编写apache module程序
上一篇 / 下一篇 2007-03-16 21:18:59 / 个人分类:apache性能监控与调整
参考http://threebit.net/tutorials/
编译apache要求指定?
./configure --prefix=$INSTALLPATH/apache --enable-so
(1) aclocal, autoconf, automake.生成所用的path,lib路径
[liangjz@serch-dev-2 modules]$ cd /home/liangjz/apache/modules
# import automake m4 macros.
[liangjz@serch-dev-2 modules]$aclocal
# create configure based on configure.in
[liangjz@serch-dev-2 modules]$ autoconf
# create Makefile.in based on Makefile.am and configure.in
[liangjz@serch-dev-2 modules]$ automake -a
(2) configure
Now we can run configure to prepare the module's Makefile.
# The ubiquitous configure scrīpt
[liangjz@serch-dev-2 modules]$./configure --with-apache=/home/liangjz/apache
(3) make
And now we can run make to compile the module. Note: don't run make install. We'll handle the module installation later.
[liangjz@serch-dev-2 modules]$ make
source='mod_tut1.c' ōbject='mod_tut1.lo' libtool=yes \
depfile='.deps/mod_tut1.Plo' tmpdepfile='.deps/mod_tut1.TPlo' \
depmode=gcc3 /bin/sh ./depcomp \
/bin/sh ./libtool --mode=compile gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"mod_tut1\" -DVERSION=\"1.0\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1 -I. -I. -I/home/liangjz/apache//include -g -O2 -c -o mod_tut1.lo `test -f 'mod_tut1.c' || echo './'`mod_tut1.c
./libtool: ./libtool: 没有那个文件或目录
make: *** [mod_tut1.lo] Error 127
确认是由于libtool路径不对!修正Makefile
#LIBTOOL = $(SHELL) $(top_builddir)/libtool
LIBTOOL =/home/liangjz/apache/build/libtool
(4) apxs
** DO NOT RUN make install ** Ordinarially you would, but the install step for an Apache module is different. Instead, apxs is used to register the module in httpd.conf and move the shared object into the apache lib directory.
[liangjz@serch-dev-2 modules]$/home/liangjz/apache/bin/apxs -i -a -n tut1 libmodtut1.la
apxs also addes the following line to httpd.conf:
LoadModule tut1_module modules/libmodtut1.so
或者手工编辑
LoadModule tut1_module modules/libmodtut1.so
<Location /bin/moduletest>
SetHandler mod_tut1_method_handler
</Location>
?????????????????没有起到过滤作用!!!!
<Location /bin/moduletest>
SetHandler mod_tut2_method_handler
</Location>
要求在程序内加入
static int mod_tut2_method_handler (request_rec *r)
{
// Get the module configuration
char buf[1024]={0};
conn_rec * p_conn_rec =NULL;
modtut2_config *s_cfg = ap_get_module_config(r->server->module_config, &tut2_module);
if (strcmp(r->handler, "mod_tut2_method_handler")) //即不匹配/bin/moduletest拒绝服务
return DECLINED;
// Send a message to the log file.
fprintf(stderr,s_cfg->string);
fprintf(stderr,"\n");
// We need to flush the stream so that the message appears right away.
// Performing an fflush() in a production system is not good for
// performance - don't do this for real.
fflush(stderr);
// Return DECLINED so that the Apache core will keep looking for
// other modules to handle this request. This effectively makes
// this module completely transparent.
p_conn_rec=r->connection;
sprintf(buf,"server=%s,client_ip=%s,method=%s,filename=%s,hander=%s\n",r->hostname,r->connection->remote_ip,r->method,r->filename,r->handler);
//解析客户端并响应给浏览器
ap_rputs(buf,r);
return 0;
}
url: http:host:port/bin/moduletest/ 。 内部转发请求给
(5)Run Apache
Now we are ready to run Apache and test the module.
# Change to the apache directory
# Start Apache
[liangjz@serch-dev-2 apache]$bin/apachectl start
(6)ie浏览器发起http请求
# hit the web server.
(7)检查输出
# Look for the module's message in the error log
[liangjz@serch-dev-2 apache$cat logs/error_log | grep tut1
apache2_mod_tut1: A request was made.
A下一步,弄清楚 path,lib,so之间关系!!!
B 修正location的过滤作用!
C 调试
/usr/local/apache2/bin/apxs -i -a -c mod_tut2.c
[liangjz@serch-dev-2 modules]$ /home/liangjz/apache/bin/apxs -i -a -c mod_tut2.c
/home/liangjz/apache/build/libtool --silent --mode=compile gcc -prefer-pic -DAP_HAVE_DESIGNATED_INITIALIZER -DLINUX=2 -D_REENTRANT -D_XOPEN_SOURCE=500 -D_BSD_SOURCE -D_SVID_SOURCE -D_GNU_SOURCE -g -O2 -pthread -I/home/liangjz/apache/include -I/home/liangjz/apache/include -I/home/liangjz/apache/include -c -o mod_tut2.lo mod_tut2.c && touch mod_tut2.slo
/home/liangjz/apache/build/libtool --silent --mode=link gcc -o mod_tut2.la -rpath /home/liangjz/apache/modules -module -avoid-version mod_tut2.lo
/home/liangjz/apache/build/instdso.sh SH_LIBTOOL='/home/liangjz/apache/build/libtool' mod_tut2.la /home/liangjz/apache/modules
/home/liangjz/apache/build/libtool --mode=install cp mod_tut2.la /home/liangjz/apache/modules/
cp .libs/mod_tut2.so /home/liangjz/apache/modules/mod_tut2.so
cp .libs/mod_tut2.lai /home/liangjz/apache/modules/mod_tut2.la
cp .libs/mod_tut2.a /home/liangjz/apache/modules/mod_tut2.a
ranlib /home/liangjz/apache/modules/mod_tut2.a
chmod 644 /home/liangjz/apache/modules/mod_tut2.a
PATH="$PATH:/sbin" ldconfig -n /home/liangjz/apache/modules
----------------------------------------------------------------------
Libraries have been installed in:
/home/liangjz/apache/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /home/liangjz/apache/modules/mod_tut2.so
[activating module `tut2' in /home/liangjz/apache/conf/httpd.conf]
[liangjz@serch-dev-2 modules]$ ls mod_tut2.so
mod_tut2.so
可以成功运行!
或者
[liangjz@serch-dev-2 modules]$ gcc -c -O3 -I/home/liangjz/apache/include -fPIC -DLINUX=2 -DUSE_HSREGEX -DUSE_EXPAT -fPIC -DSHARED_MODULE -o mod_tut2.mod mod_tut2.c
[liangjz@serch-dev-2 modules]$ gcc -shared -L. -L /home/liangjz/apache/lib/ -o libmodtut2.so mod_tut2.mod
[liangjz@serch-dev-2 modules]$ ls
libmodtut2.so mod_tut2.c mod_tut2.mod
或者
gcc -fpic -DSHARED_MODULE -I/home/liangjz/apache/include -g -c mod_tut2.c
ld -Bshareable -g -o libmodtut2.so mod_tut2.o
http://www.evance.name/tools/Apache-Chs/programs/apxs.html
用gdb调试apache
当前工作目录是 /home/jeson/apache2。 先apachectl stop关闭apache服务。
1. gdb bin/httpd
2. b ap_process_request (设置断点)
3. r -X -d /home/jeson/apache2/(指httpd.conf配置文件的上级目录conf的路径)
启动apache.
4. 启动客户端,发送请求
从ie发起http请求。
5. 设置断点在指定的文件中 b file:class:function
实践:
liangjz@serch-dev-2modules]$ gcc -fpic -DSHARED_MODULE -I/home/liangjz/apache/include -g -c mod_tut2.c
liangjz@serch-dev-2modules]$ld -Bshareable -g -o libmodtut2.so mod_tut2.o
[liangjz@serch-dev-2 apache]$ pwd
/home/liangjz/apache
[liangjz@serch-dev-2 apache]$ gdb bin/httpd
GNU gdb Red Hat Linux (6.3.0.0-1.62rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) b
No default breakpoint address now.
(gdb) b ap_process_request
Breakpoint 1 at 0x806eea7: file http_request.c, line 243.
(gdb) r -X -d /home/liangjz/apache
Starting program: /home/liangjz/apache/bin/httpd -X -d /home/liangjz/apache
[Thread debugging using libthread_db enabled]
[New Thread -1218543488 (LWP 16009)]
[Switching to Thread -1218543488 (LWP 16009)]
Breakpoint 1, ap_process_request (r=0x97efe20) at http_request.c:243
243 if (ap_extended_status)
(gdb) b /home/liangjz/apache/modules/mod_tut2.c:mod_tut2_method_handler
Breakpoint 2 at 0xac34f2: file mod_tut2.c, line 55.
(gdb) c
Continuing.
Breakpoint 2, mod_tut2_method_handler (r=0x97efe20) at mod_tut2.c:55
55 modtut2_config *s_cfg = ap_get_module_config(r->server->module_config, &tut2_module);
(gdb) n
58 fprintf(stderr,s_cfg->string);
(gdb) print tut2_module
$2 = {version = 20020903, minor_version = 9, module_index = 21, name = 0xac36e6 "mod_tut2.c",
dynamic_load_handle = 0x9787b98, next = 0x80b55c0, magic = 1095774768, rewrite_args = 0,
create_dir_config = 0, merge_dir_config = 0, create_server_config = 0xac35e1 <create_modtut2_config>,
merge_server_config = 0, cmds = 0xac4700, register_hooks = 0xac355f <mod_tut2_register_hooks>}
(gdb) detach
Detaching from program: /home/liangjz/apache/bin/httpd, process 16009
可以让httpd继续保持!!
TAG: apache性能监控与调整
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 391732
- 日志数: 186
- 图片数: 1
- 建立时间: 2007-03-16
- 更新时间: 2017-12-06