Friday, April 1, 2011

Apache Hello world module in ubuntu 10.04

The C code of the mod_helloworld.c source file ----
/* The simplest HelloWorld module */
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>
static int helloworld_handler(request_rec *r)
if (!r->handler || strcmp(r->handler, "helloworld")) {
return DECLINED;
if (r->method_number != M_GET) {
ap_set_content_type(r, "text/html;charset=ascii");
ap_rputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n",
ap_rputs("<html><head><title>Apache HelloWorld "
"Module</title></head>", r);
ap_rputs("<body><h1>Hello World!</h1>", r);
ap_rputs("<p>This is the Apache HelloWorld module!</p>", r);
ap_rputs("</body></html>", r);
return OK;
static void helloworld_hooks(apr_pool_t *pool)
ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
module AP_MODULE_DECLARE_DATA helloworld_module = {
} ;

compile the code--

sudo apxs2 -c mod_helloworld.c

to install this

sudo apxs2 -i

your output will be like that---

/usr/share/apache2/build/ SH_LIBTOOL='/usr/share/apr-1.0/build/libtool' /usr/lib/apache2/modules
/usr/share/apr-1.0/build/libtool --mode=install cp /usr/lib/apache2/modules/
libtool: install: cp .libs/ /usr/lib/apache2/modules/
libtool: install: cp .libs/mod_helloworld.lai /usr/lib/apache2/modules/
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/sbin" ldconfig -n /usr/lib/apache2/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
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/'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and manual pages.
chmod 644 /usr/lib/apache2/modules/

Then we need to add two file in the /etc/apache2/mods-available

Add first file helloworld.load by vi editor. You can use gedit or any other editor for this.
vi helloworld.load

the code will be
LoadModule helloworld_module /usr/lib/apache2/modules/

And then add another file

vi helloworld.conf

the code will be

<Location /helloworld>
SetHandler helloworld

After that you need to enable the module

a2enmod helloworld

At last we need to restart the apache server
sudo /etc/init.d/apache2 restart

Finally you need to go the url


The output will be like that--


test said...

thanks a lot, have to break my head untill i have found this simple and nice tutorial atleast to start with apache modules

good job :)

Shekar said...

its very simply..