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) {
return HTTP_METHOD_NOT_ALLOWED;
}
ap_set_content_type(r, "text/html;charset=ascii");
ap_rputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n",
r);
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 = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
helloworld_hooks
} ;


compile the code--

sudo apxs2 -c mod_helloworld.c

to install this

sudo apxs2 -i mod_helloworld.la


your output will be like that---

/usr/share/apache2/build/instdso.sh SH_LIBTOOL='/usr/share/apr-1.0/build/libtool' mod_helloworld.la /usr/lib/apache2/modules
/usr/share/apr-1.0/build/libtool --mode=install cp mod_helloworld.la /usr/lib/apache2/modules/
libtool: install: cp .libs/mod_helloworld.so /usr/lib/apache2/modules/mod_helloworld.so
libtool: install: cp .libs/mod_helloworld.lai /usr/lib/apache2/modules/mod_helloworld.la
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:
/usr/lib/apache2/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 644 /usr/lib/apache2/modules/mod_helloworld.so



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/mod_helloworld.so



And then add another file

vi helloworld.conf


the code will be


<Location /helloworld>
SetHandler helloworld
</Location>

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

http://localhost/helloworld




The output will be like that--

2 comments:

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..
satisfied