apache是一个元老级web服务器,在nginx出现之前这是大众的选择。apache的模块化设计是不错的,下面咱们就来讲说他的模块设计。apache的模块能够随核心一块儿编译进去,也能够编译成单独的模块,便于动态加载和卸载,使用起来比较方便。可是动态加载的模块在性能上确定不如编译进核心的性能高。这个要本身去权衡了,通常常常使用的模块最好编译进核心,而不经常使用的就能够独立成模块,须要时加载进去。本文要说的是如何编译成独立模块(动态共享对象DSO)。
一、如何编译模块成动态模块
当咱们使用./configure --prefix=/path/to/install编译安装apache时,默认是将模块编译进核心的,以下:
[root@vm3 httpd-2.2.24]# ./configure --prefix=/usr/local/httpd
[root@vm3 httpd-2.2.24]# make -j 4
[root@vm3 httpd-2.2.24]# make install
[root@vm3 httpd-2.2.24]# /usr/local/httpd/bin/httpd -M
Loaded Modules:
core_module (static)
authn_file_module (static)
authn_default_module (static)
authz_host_module (static)
authz_groupfile_module (static)
authz_user_module (static)
authz_default_module (static)
auth_basic_module (static)
include_module (static)
filter_module (static)
log_config_module (static)
env_module (static)
setenvif_module (static)
version_module (static)
mpm_prefork_module (static)
http_module (static)
mime_module (static)
status_module (static)
autoindex_module (static)
asis_module (static)
cgi_module (static)
negotiation_module (static)
dir_module (static)
actions_module (static)
userdir_module (static)
alias_module (static)
so_module (static)
Syntax OK
能够看到这里的模块都是静态编译的,那如何把模块编译成独立的共享模块呢?看下面:
[root@vm3 httpd-2.2.24]# ./configure --prefix=/usr/local/httpd --enable-mods-shared=rewrite
[root@vm3 httpd-2.2.24]# make -j 4
[root@vm3 httpd-2.2.24]# make install
[root@vm3 httpd-2.2.24]# /usr/local/httpd/bin/httpd -M
Loaded Modules:
core_module (static)
authn_file_module (static)
authn_default_module (static)
authz_host_module (static)
authz_groupfile_module (static)
authz_user_module (static)
authz_default_module (static)
auth_basic_module (static)
include_module (static)
filter_module (static)
log_config_module (static)
env_module (static)
setenvif_module (static)
version_module (static)
mpm_prefork_module (static)
http_module (static)
mime_module (static)
status_module (static)
autoindex_module (static)
asis_module (static)
cgi_module (static)
negotiation_module (static)
dir_module (static)
actions_module (static)
userdir_module (static)
alias_module (static)
so_module (static)
rewrite_module (shared)
Syntax OK
这里咱们将rewrite模块编译成动态模块,最后一个模块就是了,它后面显示的是shared,其它的都是static的
二、如何为apache动态添加模块(随apache源码附带的模块)
要为apache动态添加模块必须在编译apache时启用了so模块,并且这个模块和core模块只能静态编译进核心,不能做为独立模块存在,不然会报错(configure: error: mod_so can not be built as a shared DSO)而终止。so模块提供LoadModule指令,使得咱们能够加载模块。本文的apache版本默认是启用so模块的。咱们要添加随apache源码自带的rewrite模块,有2种方法:
第一种:从新编译apache,开启rewrite模块
[root@vm3 httpd-2.2.24]# ./configure --prefix=/usr/local/httpd --enable-mods-shared=rewrite
[root@vm3 httpd-2.2.24]# /usr/local/httpd/bin/httpd -M|grep rewrite
rewrite_module (shared)
第二种:使用apache的apxs工具编译安装rewrite模块
[root@vm3 httpd-2.2.24]# /usr/local/httpd/bin/apxs -c -i -a /root/httpd-2.2.24/modules/mappers/mod_rewrite.c
这里参数的意思是:
-c compile 编译源码
-i install 将模块文件安装到apache模块目录
-a active 在httpd.conf文件里添加LoadModule行
三、如何为apache动态添加第三方模块
也分2种方法:
第一种:使用apache的apxs工具
当模块只包含一个mod_xxxx.c时,可使用这种方法
第二种:使用模块的configure
好比在安装php时使用下面命令:
[root@vm3 php-5.3.21]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/httpd/bin/apxs