2014-03-10 09:34:01
来 源
kejihao
Apache
本文介绍Apache服务器中rewrite模块的安装和使用方法,希望对于初学Apache服务器相关的朋友有帮助,更多Apache安装、配置、报错处理等资源请本站内搜索。
最近工作需要用到apache rewrite功能,之前本机已经安装apache,但没有安装rewrite模块。下面总结一下rewrite模块的安装过程和相关知识。本机已安装的apache版本为2.2.16,操作系统为ubuntu10.10。

一、 安装过程

1. 在apache源码目录下查找mod_rewrite.c文件。

find . -name mod_rewrite.c

2. 进入mod_rewrite.c文件所在目录。

cd modules/mappers

3. 将C程序编译成共享模块,以便在运行时加载到apache服务器中。

/usr/alibaba/install/httpd-2.2.16/bin/apxs -c mod_rewrite.c

其中/usr/alibaba/install/httpd-2.2.16为本机的apache安装目录。

console输出如下:

/usr/alibaba/install/httpd-2.2.16/build/libtool --silent --mode=compile gcc -prefer-pic -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -g -O2 -pthread -I/usr/alibaba/install/httpd-2.2.16/include -I/usr/alibaba/install/httpd-2.2.16/include -I/usr/alibaba/install/httpd-2.2.16/include -c -o mod_rewrite.lo mod_rewrite.c && touch mod_rewrite.slo

/usr/alibaba/install/httpd-2.2.16/build/libtool --silent --mode=link gcc -o mod_rewrite.la -rpath /usr/alibaba/install/httpd-2.2.16/modules -module -avoid-version mod_rewrite.lo

4. 安装动态共享对象到服务器的module目录中,并修改httpd.conf文件配置。

/usr/alibaba/install/httpd-2.2.16/bin/apxs -i -a -n rewrite mod_rewrite.la

console输出如下:

/usr/alibaba/install/httpd-2.2.16/build/instdso.sh SH_LIBTOOL='/usr/alibaba/install/httpd-2.2.16/build/libtool' mod_rewrite.la /usr/alibaba/install/httpd-2.2.16/modules

/usr/alibaba/install/httpd-2.2.16/build/libtool --mode=install cp mod_rewrite.la /usr/alibaba/install/httpd-2.2.16/modules/

cp .libs/mod_rewrite.so /usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.so

cp .libs/mod_rewrite.lai /usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.la

cp .libs/mod_rewrite.a /usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.a

chmod 644 /usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.a

ranlib /usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.a

PATH="$PATH:/sbin" ldconfig -n /usr/alibaba/install/httpd-2.2.16/modules

----------------------------------------------------------------------

Libraries have been installed in:

/usr/alibaba/install/httpd-2.2.16/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 /usr/alibaba/install/httpd-2.2.16/modules/mod_rewrite.so

[activating module `rewrite' in /usr/alibaba/install/httpd-2.2.16/conf/httpd.conf]

参数含义:

-i 表示需要执行安装操作。

-a 自动增加一个LoadModule行到httpd.conf文件中,以激活此模块,或者,如果此行已经存在,则启用之。

-n 增加或启用的模块名称。

5. 打开apache安装目录下的conf/httpd.conf文件,检查是否有如下行:

LoadModule rewrite_module modules/mod_rewrite.so

如果包含此行,则安装成功。

6. 重新启动apache。

/usr/alibaba/install/httpd-2.2.16/bin/apachectl stop

/usr/alibaba/install/httpd-2.2.16/bin/apachectl start

二、 安装错误解决方法

1. 现象: Can`t loacte API module staructure `mod_rewrite_module` in file

/usr/local/apache2/modules/mod_rewrite.so:/usr/local/apache2/lib/libapr-

0.so.0:undefined symbol:mod_rewrite_module

解决方法: httpd.conf里面写的mod_rewrite_module改成rewrite_module。

2. 现象: module rewrite_module is built-in and can't be loaded.

解决方法: 表示模块是内建的,不用再手动调入,注释掉httpd.conf中如下行:

#LoadModule rewrite_module modules/mod_rewrite.so

检查所有内建模块的命令: apachectl -l

三、 rewrite功能简介

需求: 如果本地包含样式文件,则通过本地apache访问资源;否则从远端服务器获取资源。

实现:

<VirtualHost *:80>

DocumentRoot "/home/zhouyingbo/sourcecode/style_other/20111031_88209_punish_1"

ServerName style.china.alibaba.com

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f #不是本地文件夹

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d #不是本地文件

RewriteRule ^(.*)$ http://10.20.136.137$1 [L] #重写到远端服务器

</IfModule>

</VirtualHost>

说明:

RewriteEngine On 表示开启重写引擎功能;

RewriteCond语法格式为: RewriteCond TestString CondPattern,当TestString满足CondPattern时,执行RewriteRule定义的规则。

RewriteRule语法格式为: RewriteRule Pattern Substitution,Pattern为posix正则式,Substitution用来替换相应的匹配。

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。