• Linux下增加Apache的rewrite Module
    时间:2008-09-08   作者:佚名   出处:互联网

    最近,项目需要在已经编译好的Apache上以动态方式加载rewrite模块。于是,我们在网上找了许多资料,但都讲解得不是很详细且格式错位问题严重。所以我们有必要把这一过程再重述一遍。实际操作步骤如下:

    工作现场描述:
        Linux 2.4.21
        apache_1.3.34.tar.gz    解压后的目录为 /root/apache_1.3.34 文中简称为“源码目录”
        apache安装目录 /usr/local/apache 文中简称为“目标目录”

    步骤:
    1、Apache安装rewrite模块的时候需要DBM支持,否则无法编译,所以首先要安装一个GDBM
        下载地址:ftp://ftp.gnu.org/gnu/gdbm/
        安装步骤:
            进入安装目录,
            ./configure
            make
            make install
            make install-compat    (最后行也要执行。否则无法编译出ndbm.h头文件)
        如果您不能确定服务器上是否已经装有DBM,那么可以安装一下。否则这步可以跳过。

    2、现在到apache源码目录的标准模块目录中(/root/apache_src/src/modules/standard/)中,使用如下指令编译出so文件:
        /usr/local/apache/bin/apxs -c mod_rewrite.c -lgdbm
    即可得到mod_rewrite.so文件。
        备注:"-lgdbm"是用为说明在编译mod_rewrite.c时要把gdbm链接进来。这样在第6步启动apache时就不会报出"dbm fetch"的错误了。

    3、现在让apache的apxs来自动向http.conf配置文件中加入LoadModule语句并将mod_rewrite.so文件拷贝到apache/libexec目录
        /usr/local/apache/bin/apxs -i -A -n rewrite /root/apache_1.3.34/src/modules/standard/mod_rewrite.so
        备注:命令中的rewrite参数是告诉apxs命令mod_rewrite.so文件中的模块名。在命令执行后,apxs会在LoadModule中为 rewrite加上"_module"以标名模块名称。如果你在启动apache时发现总是给出“不能定位API”之类的错误,那就是说明 LoadModule后面的模块名的语法要根据您的apache版本加以改变。

    4、停止apache
        apache/bin/apachectl stop

    5、运行配置文件检查命令
        apache/bin/apachectl configtest
        如显示Syntax OK,则表示整个操作成功。那么可以到第6步。否则根据提示信息进行调试。但只要按照此文所说进行操作是不会出错的。

    6、启动apache
        apache/bin/apachectl start

    全文结束。


    参考原文
    ============================

    公司一台Linux服务器,Apache默认安装时候没有加载任何Modules,最近要用到Apache的rewrite模块,经过一夜一天的努力,终于成功了,兴奋....
    现在列下几个要点:
    1. Apache安装rewrite模块的时候需要DBM支持,否则无法编译,所以首先要安装一个GDBM  下载地址:ftp://ftp.gnu.org/gnu/gdbm/
        安装步骤: 进入安装目录,./configure; make; make install; make install-compat; 否则无法编译出ndbm.h头文件.
    2. 然后用Apache bin目录下的apxs命令安装
        /var/apache/bin/apxs -c mod_rewrite.c {
        gcc -DLINUX=22 -DUSE_HSREGEX -DUSE_EXPAT -I../lib/expat-lite -fpic -DSHARED_MODULE -I/var/apache/include  -c mod_rewrite.c
        gcc -shared -o mod_rewrite.so mod_rewrite.o -lgdbm
    }
        /var/apache/bin/apxs -i -a -n mod_rewrite mod_rewrite.so

    然后在http.conf配置文件里加上:LoadModule rewrite_module libexec/mod_rewrite.so
    接下来用/usr/local/apache/bin/apachectl
    stop停止apache,然后用再start,千万注意,在这里不能用restart或者graceful参数来重新启动apache,必须先停止,然后再开始,或者是reboot机器,否则rewrite将不起作用。

    -------------------------------------------------------------------------------------------------------------
    I tried to include in my Apache Web server's configuration the mod_rewrite module, but when I restarted the server, I received an error:

    Cannot load /usr/local/apache/libexec/mod_rewrite.so into server:
    /usr/local/apache/libexec/mod_rewrite.so: undefined symbol: dbm_fetch
    The problem, as it turns out, is that mod_rewrite.so is compiled incorrectly. It should be linked with a dbm library but it isn't.

    If you have an up-to-date set of Apache source files, you can easily solve this problem by manually rerunning the last compilation step of this module, using the correct options. When you execute make mod_rewrite.so in the appropriate directory, it performs this final step:

    gcc -shared -o mod_rewrite.so mod_rewrite.lo
    Rerun gcc, this time adding a reference to the GNU gdbm library:

    gcc -shared -o mod_rewrite.so mod_rewrite.lo -lgdbm
    Next, copy the newly created mod_rewrite.so over to /usr/local/apache/libexec  or wherever your Apache module files are located.

    In my case, this was all that was needed to solve the problem. Your mileage may vary.

    网友留言/评论

    我要留言/评论