httpd-modules-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Wortham" <djwort...@gmail.com>
Subject Re: New to module development
Date Tue, 13 Mar 2007 07:59:49 GMT
Hello Souramita and welcome to the group.

You very likely want to use APXS if you are writing a module in C.

Start here (the Apache 2.2 APXS manual page):
http://httpd.apache.org/docs/2.2/programs/apxs.html

I believe this command:

apxs -g -n foo

(where 'foo' is the name of the module) will create a module source code
file with minimal code named mod_foo.c (and the directories that go along
with it).  That would be my reccomendation as a place to start.

This command:

apxs -c -i -a foo

will compile, install, and activate your module (doing everything except
restarting your httpd process for you).

IMHO, it is unnecessary to use GCC directly since APXS is a simple, clean,
intuitive interface that handles the compilation and installation for you.


For deeper understanding of how to write an Apache module in C, I would
reccomend bookmarking these links (and consider buying Nick Kew's new
book... which was just published last month IIRC<http://www.apachetutor.org>
).

http://www.apachetutor.org/dev/   (Nick's site ... he literally 'wrote the
book' on the subject)
http://modules.apache.org/doc/Intro_API_Prog.html
http://httpd.apache.org/docs/1.3/misc/API.html
http://httpd.apache.org/docs/2.0/upgrading.html              (Most of the
"*.apache.org" sites assume you program for 1.3.x and later upgrade your
code for 2.0)
http://dev.ariel-networks.com/apr/apr-tutorial/html/
http://threebit.net/tutorials/apache2_modules/tut1/tutorial1.html
http://threebit.net/tutorials/apache2_modules/tut2/tutorial2.html
http://www.f-m-c.org/projects/apache/html/3_3Extending_Apache.html

When asking for help on the mailing list, it is helpful to state
environment-dependent things like (1) your Apache version (1.3.x, 2.0.x, or
2.2.x), (2) your OS, and (3) your child-process-count and thread-count
directives (from your httpd.conf configuration file).

Also, you mentioned header files were not supplied with your distribution of
Fedora... I am willing to bet that most distros of Linux have the source
code files (and headers) included either on a separate disc image or simply
not installed by default.  Worst case scenario: you can grab the newest
version of Apache and it will come with the source tree.

Unfortunately, Apache contains a pretty powerful module API... which means
the learning curve is a little steep.  Some of the above links will help you
get the gist of the overall structure of writing modules and using the
module API.  When learning, it helps to know what version of Apache your
sample code is written for (look for STANDARD_MODULE_STUFF or
STANDARD20_MODULE_STUFF in the module definition).

Hope this helps,
David Wortham








On 3/12/07, Souramita Sen <souramita_sen@mindtree.com> wrote:
>
>
>
> Hi,
>
> Greetings to All..I am a new member of this list and would like to know
> the
> following :-
>
> 1. Suppose i make a demo module named "mod_demo" that would print "Hello
> World" once Apache web server is started. I have surfed through different
> web
> sites and jotted down the following steps:
>
>    a) Write the corresponding C file mod_demo.c that prints "Hello World"
>    b) Compile the module by invoking
>            gcc -fPIC -DSHARED_MODULE -I/usr/include/httpd
> -I/usr/include/apr-0  -c mod_demo.c
>
>    c) Make a shared object from the object file
>            ld -Bshareable -o mod_demo.so mod_demo.o
>
>    d) Copy the file to the apache modules directory:
>            cp mod_demo.so /usr/lib/httpd/modules/
>            chmod 0755 /usr/lib/httpd/modules/mod_demo.so
>
>    e) Add the line to httpd.conf file for Apache to load the module
>            LoadModule myvar_module /usr/lib/httpd/modules/mod_demo.so
>
>    f) Add the following in the httpd.conf file
>            <IfModule mod_demo.c>
>                DemoEnable On
>            </IfModule>
>
>     This will enable the new module.
>
> Now my doubts are:
>     a) To enable the module should not I stop the apache server(service
> httpd
> stop) and compile the apache directory again? If yes How do i compile the
> apache directory again?
>     b) I did not understand the format of how to write the mod_demo.c. It
> is
> not simple C file. Here is what i found in net as contents of some
> mod_myvar.c. In the Fedora core Linux distribution the following header
> files
> are not present by default.
>
>
>
>
> -----------------------------------------------------------------------------
> ---------------
>
> #include "httpd.h"
> #include "http_config.h"
> #include "http_protocol.h"
> #include "http_log.h"
> #include "ap_config.h"
>
> module AP_MODULE_DECLARE_DATA myvar_module;
>
> static apr_status_t myvar_cleanup(void *cfgdata) {
>    // cleanup code here, if needed
>    return APR_SUCCESS;
> }
>
> static void myvar_child_init(apr_pool_t *p, server_rec *s) {
>    apr_pool_cleanup_register(p, NULL, myvar_cleanup, myvar_cleanup);
> }
>
> static int myvar_post_read_request(request_rec *r) {
>    char myvar[16] = "CAPTAIN WAS HERE";
>    apr_table_set(r->notes, "myvar", myvar); // PHP: apache_notes
>    apr_table_set( r->subprocess_env, "myvar", myvar ); // PHP:
> HTTP_SERVER_VARS
>    return OK;
> }
>
> static const char *set_myvar_enable(cmd_parms *cmd, void *dummy, int arg)
> {
>    ap_get_module_config(cmd->server->module_config, &myvar_module);
>    return NULL;
> }
>
> static const command_rec myvar_cmds[] = {
>    AP_INIT_FLAG( "MyVarEnable", set_myvar_enable, NULL, OR_FILEINFO, "Turn
> on
> mod_myvar"),
>    {NULL}
> };
>
> static void myvar_register_hooks(apr_pool_t *p) {
>    ap_hook_post_read_request( myvar_post_read_request, NULL, NULL,
> APR_HOOK_MIDDLE );
>    ap_hook_child_init(        myvar_child_init,        NULL, NULL,
> APR_HOOK_MIDDLE );
> }
>
> // API hooks
> module AP_MODULE_DECLARE_DATA myvar_module = {
>    STANDARD20_MODULE_STUFF,
>    NULL,                        /* create per-dir    config structures */
>    NULL,                        /* merge  per-dir    config structures */
>    NULL,                        /* create per-server config structures */
>    NULL,                        /* merge  per-server config structures */
>    myvar_cmds,                  /* table of config file commands       */
>    myvar_register_hooks         /* register hooks                      */
> };
>
>
> -----------------------------------------------------------------------------
> ------------------------
>
> Thanks,
> Souramita
>
>
>
>
> DISCLAIMER:
> This message (including attachment if any) is confidential and may be
> privileged. Before opening attachments please check them for viruses and
> defects. MindTree Consulting Limited (MindTree) will not be responsible for
> any viruses or defects or any forwarded attachments emanating either from
> within MindTree or outside. If you have received this message by mistake
> please notify the sender by return  e-mail and delete this message from your
> system. Any unauthorized use or dissemination of this message in whole or in
> part is strictly prohibited.  Please note that e-mails are susceptible to
> change and MindTree shall not be liable for any improper, untimely or
> incomplete transmission.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message