apxs -q query ...
apxs -c [ -o dsofile ] [ -I incdir ] [ -D name=value ] [ -L libdir ] [ -l libname ] [ -Wc,compiler-flags ] [ -Wl,linker-flags ] files ...
apxs -i [ -n modname ] [ -a ] [ -A ] dsofile ...
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/libexec/mod_foo.so chmod 755 /path/to/apache/libexec/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 automtaically 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 independend 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 compile the object files.
For more details about DSO support in Apache read the documentation of mod_so or perhaps even read the src/modules/standard/mod_so.c source file.
Query options:
CC TARGET CFLAGS SBINDIR CFLAGS_SHLIB INCLUDEDIR LD_SHLIB LIBEXECDIR LDFLAGS_SHLIB SYSCONFDIR LIBS_SHLIBUse this for manually determining settings. For instance use
INC=-I`apxs -q INCLUDEDIR`inside your own Makefiles if you need manual access to Apache's C header files.
Template Generation options:
DSO compilation options:
DSO installation options:
$ apxs -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 $ _
Then you have to update the Apache configuration by making sure a LoadModule directive is present to load this shared object. To simplify this step apxs provides an automatic way to install the shared object in its "libexec" directory and updating the httpd.conf file accordingly. This can be achieved by running:
$ apxs -i -a mod_foo.c cp mod_foo.so /path/to/apache/libexec/mod_foo.so chmod 755 /path/to/apache/libexec/mod_foo.so [activating module `foo' in /path/to/apache/etc/httpd.conf] $ _
This way a line named
LoadModule foo_module libexec/mod_foo.so
is added to the configuration file if still not present. If you want to have this this disabled per default use the -A option, i.e.
$ apxs -i -A mod_foo.c
For a quick test of the APXS mechanism you can create a sample Apache module template plus a corresponding Makefile via:
$ apxs -g -n foo Creating [DIR] foo Creating [FILE] foo/Makefile Creating [FILE] foo/mod_foo.c $ _
Then you can immediately compile this sample module into a shared object and load it into the Apache server:
$ cd foo $ make all reload apxs -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 apxs -i -a -n "foo" mod_foo.so cp mod_foo.so /path/to/apache/libexec/mod_foo.so chmod 755 /path/to/apache/libexec/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 $ _
You can even use apxs to compile complex modules outside the Apache source tree, like PHP3:
$ cd php3 $ ./configure --with-shared-apache=../apache-1.3 $ apxs -c -o libphp3.so mod_php3.c libmodphp3-so.a gcc -fpic -DSHARED_MODULE -I/tmp/apache/include -c mod_php3.c ld -Bshareable -o libphp3.so mod_php3.o libmodphp3-so.a $ _
because apxs automatically recognized C source files and object files. Only C source files are compiled while remaining object files are used for the linking phase.