apacheモジュール開発

4428 ワード

会社のjbossクラスタの問題を解決して、apacheモジュールの開発を学び始めて、開発したプラットフォームはwindowsプラットフォームを採用して、みんながもっと簡単に開発段階に入ることができることを望んで、会社の大部分はvs 2008があります.
1.apacheインストールパッケージhttpd-2.2をインストールする.21-win32-x86-no_ssl.msiは必ずcustomをすべてインストールしなければなりません.そうしないとincludeとlibディレクトリはありません.
2.構成apxs 1)インストールapxsインストールパッケージapxs_win 32 2)Strawberry Perlインストールパッケージstrawberry-perl-5.16をインストールする.3.1-32bit.msi 3)dosコマンドプロンプトに入り、apxsインストールディレクトリの下に移動し、perl Configureを入力.pl、要求通りにapacheのインストールディレクトリに記入...\apache2.2とコマンド名「httpd.exe」4)は、前のステップでapache 2に進みます.2binでapxsコマンドを生成し、apache 2.2ディレクトリの下でbuildディレクトリが生成された5)apache 2に変更する.2次buildディレクトリのconfig_vars.mkファイルはCC=gccのgccをcl.exe、LD=g++のg++をlinkに変更する.exe,CPP=gcc-Eのgcc-E削除6)apacheでのbinのコマンドapxsの経路を環境変数として設定し,具体的なインストールディレクトリに入らずにapxsを実行する.
3.コンパイルapacheモジュール1)Visual Studio 2008コマンドプロンプトを実行する(開始したMicrosoft Visual Studio 2008で見つかる)2)apxs-g-n helloworld(helloworldはモジュール名)を実行すると、helloworldというディレクトリとテンプレートコードが生成されます3)helloworldディレクトリに入りmod_helloworld.c 4)apxs-c-i-a mod_を実行helloworld.c libapr-1.lib libaprutil-1.lib libapriconv-1.lib libhttpd.lib,mod_を生成するhelloworld.so 5)mod_helloworld.so Apache 2にコピーする.2modules下6)Apache 2を修正する.2\conf\httpd.conf、末尾にLoadModule helloworld_を付けるmodule\modules\mod_helloworld.so setHandler helloworld 7)apacheを起動し、IEに入力http://loacalhost/helloworld
 
/* 
**  mod_helloworld.c -- Apache sample helloworld module
**  [Autogenerated via ``apxs -n helloworld -g'']
**
**  To play with this sample module first compile it into a
**  DSO file and install it into Apache's modules directory 
**  by running:
**
**    $ apxs -c -i mod_helloworld.c
**
**  Then activate it in Apache's httpd.conf file for instance
**  for the URL /helloworld in as follows:
**
**    #   httpd.conf
**    LoadModule helloworld_module modules/mod_helloworld.so
**    
**    SetHandler helloworld
**    
**
**  Then after restarting Apache via
**
**    $ apachectl restart
**
**  you immediately can request the URL /helloworld and watch for the
**  output of this module. This can be achieved for instance via:
**
**    $ lynx -mime_header http://localhost/helloworld 
**
**  The output should be similar to the following one:
**
**    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
**  
**    The sample page from mod_helloworld.c
*/ 

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"


static int printitem(void *rec,const char *key,const char *value)
{
	request_rec *r = rec;
	ap_rprintf(r,"%s%s
",ap_escape_html(r->pool,key),ap_escape_html(r->pool,value)); return 1; } static void printtable(request_rec *r,apr_table_t *t,const char *caption,const char *keyhead,const char *valhead) { ap_rprintf(r,"
%s%s%s",caption,keyhead,valhead);
apr_table_do(printitem,r,t,NULL);
ap_rputs("",r);
}
/* The sample content handler */
static int helloworld_handler(request_rec *r)
{
ap_set_content_type(r,"text/html;charset=ascii");
ap_rputs(""
"Apache module devlopapache is back",r);
printtable(r,r->headers_in,"request headers","header","value");
printtable(r,r->headers_out,"response headers","header","value");
ap_rputs("apache module develop from ibyoung.c", r);
if (strcmp(r->handler, "helloworld")) {
return DECLINED;
}
r->content_type = "text/html";
if (!r->header_only)
ap_rputs("run success.c", r);
ap_rputs("",r);
return OK;
}
static void helloworld_register_hooks(apr_pool_t *p)
{
ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA helloworld_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 */
NULL, /* table of config file commands */
helloworld_register_hooks /* register hooks */
};