
08 Implementing Configuration Directives
The following function will set up the configuration directives for our module.
[bash]static const command_rec mod_lud_cmds[] =
{
// AP_INIT_TAKE1 macro declares that this configuration directive takes only one argument
//You can also use AP_INIT_NO_ARGS, AP_INIT_RAW_ARGS, AP_INIT_TAKE2, etc.
AP_INIT_TAKE1(
//name of configuration directive set for httpd.conf file
“ModuleTutorialString”,
//this function will be called by the httpd core after correctly parsing the Configuration
//directive
set_modlud_string,
NULL,
//A directive with the RSRC_CONF bit set may appear in the server-wide configuration
//files (e.g., httpd.conf) outside <Directory> or <Location> containers.
RSRC_CONF,
// This act as a Usage Message and will be prompted to the user incase of incorrect
//syntax
“ModludString <string> — the string to print to the error log for each HTTP request.”
),
{NULL}
};
[/bash]
09 Setting Module Configuration
The following function will be called after parsing the configuration directive.
[bash]static const char *set_modlud_string(cmd_parms *parms, void *mconfig, const char *arg)
{
// get the module configuration (this is the structure created by create_modlud_config())
modlud_config *s_cfg = ap_get_module_config(parms->server->module_config, &lud_module);
// make a duplicate of the argument’s value using the command parameters pool.
s_cfg->string = (char *) arg;
// success
return NULL;
}
[/bash]
Deploying Apache module mod_lud.c
Deploying an Apache module consists of three steps:
1. Compiling / Installing
2. Configuring
3. Running
10 Compiling / installing mod_lud.c
Module compilation and installation is done using a tool called apxs (APache eXtenSion tool). apxs makes the compilation and installation process very easy. It eliminates the need to create makefiles by hand and copying the modules manually to the module directory. It also takes care of the basic configuration of module inside httpd.conf for module loading. Perform the following command to compile and install the module.
[bash]$ apxs -c -i -a mod_lud.c
[/bash]
Please note:
It is important that you use the correct apxs tool while building the module. It will affect the configuration and installation location of the module.
Important apxs command-line switches:
-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.
-i: This directs the apxs tool to install the modules into the server’s modules directory.
-a: This activates the module by automatically adding a corresponding LoadModule line to Apache’s httpd.conf configuration file, or by enabling it if it already exists.
After running the apxs tool <HTTPD Directory>/modules should contain mod_lud.so file and <HTTPD Directory>/conf/httpd.conf should contain the following line:
[bash]LoadModule lud_module modules/mod_lud.so
[/bash]
11 Configuring mod_lud.c
Our module takes a global configuration directive called ModuleTutorialString that needs to be present at the end of httpd.conf. This directive takes a string and sets up the custom logging message.
In our case let’s set it to the following:
[bash]ModuleTutorialString “mod_lud: Custom HTTP Request Alert”
[/bash]
12 Running mod_lud.c
After all the hard work, let’s see mod_lud. in action. First, check if httpd.conf is error free.
[bash]$ apachectl configtest
Syntax OK
[/bash]
If there is syntax error, you will need to correct it before proceeding.
Start the web server
[bash]$ apachectl restart
[/bash]
Now open a web browser. Point it to http://localhost:3322 (or whatever port you have set up in httpd.conf’s Listen directive). Refresh the page a few times.
[bash]error_log should have the following data:
$ tail logs/error_log
[Mon Aug 26 17:57:26 2010] [notice] SIGHUP received. Attempting to restart
[Mon Aug 26 17:57:26 2010] [notice] Apache/2.2.16 (Unix) configured — resuming normal operations
mod_lud: Custom HTTP Request Alert
mod_lud: Custom HTTP Request Alert
mod_lud: Custom HTTP Request Alert
mod_lud: Custom HTTP Request Alert
[/bash]
Now let’s comment the following line in httpd.conf and restart the server:
[bash]# ModuleTutorialString “This is a custom log message.”
[/bash]
This should activate the default message. Restart the server again and point to http://localhost:3322.
This time error_log should have the following data:
[bash]$ tail logs/error_log
[Mon Aug 26 18:10:04 2010] [notice] Apache/2.2.16 (Unix) configured — resuming normal operations
mod_lud: Default HTTP Request Alert.
mod_lud: Default HTTP Request Alert.
mod_lud: Default HTTP Request Alert.
mod_lud: Default HTTP Request Alert.
[/bash]
Modules provide an excellent way to extend Apache HTTP Server very easily. Today, Apache modules are powering Web 2.0 in the form of mod_php, mod_python, mod_perl and others, bringing modern web technologies directly to Apache HTTP Server. This has turned it into a full-blown OS in which modules work like installable software catering to a wide variety of tasks.
Find more tutorials from Linux User
Return to the Linux User homepage
[twitter username=”linuxusermag”]