Matthew Note

Apache Module 开发入门

Actually to build a apache module is not very hard if you can find a nice way to do it. Here I want to show you my way.

The Environment

To build a apache module you have to install two things:

  • Apache http server
  • APR(apache portable runtime)

Nothing much to describe How to install these, Just apt-get install!!(Debian)

Build a Module sample

if you have successfuly installed these two kinds of thing above, you can use the following command to automatically build a module sample

1
sudo apxs2 -n mod_foo -g

Now you can see a mod_foo.c and a Makefile in your working dirtectroy. Next!

How to use it?

To play with this sample module first compile it into a DSO file and install it into Apache’s modules directory by running:

1
apxs -c -i mod_foo.c

Then activate it in Apache’s apache2.conf file for instance for the URL /foo in as follows:

1
2
3
4
5
#apache2.conf
LoadModule foo_module modules/mod_foo.so
<Location /foo>
SetHandler foo
</Location>

Then after restarting Apache via

1
apachectl restart

you immediately can request the URL /foo and watch for the output of this module. This can be achieved for instance via:

1
lynx -mime_header http://localhost/foo

The output should be similar to the following one:

1
2
3
4
5
HTTP/1.1 200 OK
Date: Tue, 31 Mar 1998 14:42:22 GMT
Server: Apache/1.3.4 (Unix)
Connection: close
Content-Type: text/html

More infomation: Looking at 《The Apache Module Book》you can find a pdf version on the internet.