Apache 2.2 installation of mod_rewrite module without recompiling whole apache
Usually I keep my sources available at all times because apache or php could require a new module to be installed (sooner or later). I will post here how to install apache mod_rewrite
module without recompiling whole apache from source.
Below is the source of the mod_rewrite
apache module on my system.
# cd /usr/src/httpd-2.2/modules/mappers
# ls *c | grep rewrite
I will be using the apache extension tool (apxs):
APXS(8) apxs APXS(8)
apxs - APache eXtenSion tool
apxs -g [ -S name=value ] -n modname
apxs -q [ -S name=value ] query ...
apxs -c [ -S name=value ] [ -o dsofile ] [ -I incdir ] [ -D name=value ] [ -L libdir ] [ -l libname ] [ -Wc,compiler-
flags ] [ -Wl,linker-flags ] files ...
apxs -i [ -S name=value ] [ -n modname ] [ -a ] [ -A ] dso-file ...
apxs -e [ -S name=value ] [ -n modname ] [ -a ] [ -A ] dso-file ...
apxs is a tool for building and installing extension modules for the Apache HyperText Transfer Protocol (HTTP) server.
This is achieved by building a dynamic shared object (DSO) from one or more source or object files which then can be
loaded into the Apache server under runtime via the LoadModule directive from mod_so.
So to use this extension mechanism your platform has to support the DSO feature and your Apache httpd binary has to be
built with the mod_so module. The apxs tool automatically complains if this is not the case. You can check this yourself
by manually running the command
$ httpd -l
The module mod_so should be part of the displayed list. If these requirements are fulfilled you can easily extend your
Apache server's functionality by installing your own modules with the DSO mechanism by the help of this apxs tool:
$ apxs -i -a -c mod_foo.c
gcc -fpic -DSHARED_MODULE -I/path/to/apache/include -c mod_foo.c
ld -Bshareable -o mod_foo.so mod_foo.o
cp mod_foo.so /path/to/apache/modules/mod_foo.so
chmod 755 /path/to/apache/modules/mod_foo.so
[activating module `foo' in /path/to/apache/etc/httpd.conf]
$ apachectl restart
/path/to/apache/sbin/apachectl restart: httpd not running, trying to start
[Tue Mar 31 11:27:55 1998] [debug] mod_so.c(303): loaded module foo_module
/path/to/apache/sbin/apachectl restart: httpd started
The arguments files can be any C source file (.c), a object file (.o) or even a library archive (.a). The apxs tool
automatically recognizes these extensions and automatically used the C source files for compilation while just using the
object and archive files for the linking phase. But when using such pre-compiled objects make sure they are compiled for
position independent code (PIC) to be able to use them for a dynamically loaded shared object. For instance with GCC you
always just have to use -fpic. For other C compilers consult its manual page or at watch for the flags apxs uses to comâ
pile the object files.
DSO Compilation Options
-c This indicates the compilation operation. It first compiles the C source files (.c) of files into corresponding
object files (.o) and then builds a dynamically shared object in dsofile by linking these object files plus the
remaining object files (.o and .a) of files. If no -o option is specified the output file is guessed from the
first filename in files and thus usually defaults to mod_name.so.
DSO Installation and Configuration Options
-i This indicates the installation operation and installs one or more dynamically shared objects into the server's
-a This activates the module by automatically adding a corresponding LoadModule line to Apache's httpd.conf configuâ
ration file, or by enabling it if it already exists.
In my example below I will only compile and install the mod_rewrite
apache module. I'm not interested about the -a
switch of apxs which will also add the LoadModule
line in apache configuration.
# /opt/apache/bin/apxs -c -i mod_rewrite.c
/opt/apache/build/libtool --silent --mode=compile gcc -prefer-pic -march=native -mfpmath=sse -msse2 -O2 -pipe -s -fomit-frame-pointer -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -pthread -I/opt/apache/include -I/opt/apache/include -I/opt/apache/include -c -o mod_rewrite.lo mod_rewrite.c && touch mod_rewrite.slo
/opt/apache/build/libtool --silent --mode=link gcc -o mod_rewrite.la -rpath /opt/apache/modules -module -avoid-version mod_rewrite.lo
/opt/apache/build/instdso.sh SH_LIBTOOL='/opt/apache/build/libtool' mod_rewrite.la /opt/apache/modules
/opt/apache/build/libtool --mode=install cp mod_rewrite.la /opt/apache/modules/
cp .libs/mod_rewrite.so /opt/apache/modules/mod_rewrite.so
cp .libs/mod_rewrite.lai /opt/apache/modules/mod_rewrite.la
cp .libs/mod_rewrite.a /opt/apache/modules/mod_rewrite.a
chmod 644 /opt/apache/modules/mod_rewrite.a
PATH="$PATH:/sbin" ldconfig -n /opt/apache/modules
Libraries have been installed in:
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
- add LIBDIR to the `LD_RUN_PATH' environment variable
- 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 /opt/apache/modules/mod_rewrite.so
Now, all that remains to be done is to manually add the "LoadModule rewrite_module modules/mod_rewrite.so
" line to the apache module section and check the configuration and restart apache:
# apachectl configtest
# apachectl graceful
Always make sure that the apache extension tool apxs
that you are using is the one from the already installed&working apache (this warning is retoric for most cases, but for those who have one apache instance installed from FreeBSD or Linux package system, which is inactive, and another apache customly compiled from source is essential) and also make sure that you are using the sources for the same version of apache, otherwise, you meight have error during compiling or, worse, a mod_rewrite module compiled from http-2.2.13 sources running a 2.2.11 apache, for example.