tmp
Thursday, June 5, 2014
c++ で apache module 開発
##必要なパッケージのインストール ``` sudo apt-get install apache2-threaded-dev sudo apt-get install g++ ``` ##サンプル用のモジュールを作成 ###mod_cpphello.cpp ``` #include "ap_config.h" #include "apr.h" #include "apr_lib.h" #include "apr_strings.h" #include "apr_network_io.h" #include "apr_want.h" #include "httpd.h" #include "http_config.h" #include "http_core.h" #include "http_request.h" #include "http_protocol.h" #include <string> extern "C" module AP_MODULE_DECLARE_DATA cpphello_module; typedef struct { char *hellomessage; } cpphello_dir_config; static void *cpphello_create_dir_config(apr_pool_t *p, char *path) { cpphello_dir_config *cfg = (cpphello_dir_config *)apr_pcalloc(p, sizeof(cpphello_dir_config)); cfg->hellomessage = (char *)"Hello World!"; return cfg; } static int cpphello_handler(request_rec *r) { cpphello_dir_config *cfg = (cpphello_dir_config *) ap_get_module_config(r->per_dir_config, &cpphello_module); std::string messagetosend = std::string("<html><p>") + std::string(cfg->hellomessage) + std::string("</p></html>\n"); r->content_type = "text/html"; if (!r->header_only) { ap_rputs(messagetosend.c_str(), r); } return OK; } static void register_hooks(apr_pool_t *p) { ap_hook_handler(cpphello_handler,NULL,NULL,APR_HOOK_MIDDLE); } extern "C" { module AP_MODULE_DECLARE_DATA cpphello_module = { STANDARD20_MODULE_STUFF, cpphello_create_dir_config, NULL, NULL, NULL, NULL, register_hooks }; }; ``` ###Makefile ``` ## ## Makefile -- Build procedure for fast3lpoad Apache module ## ## This is a C++ module so things have to be handled a little differently. # the used tools APXS=apxs2 APACHECTL=apachectl # Get all of apxs's internal values. APXS_CC=`$(APXS) -q CC` APXS_TARGET=`$(APXS) -q TARGET` APXS_CFLAGS=`$(APXS) -q CFLAGS` APXS_SBINDIR=`$(APXS) -q SBINDIR` APXS_CFLAGS_SHLIB=`$(APXS) -q CFLAGS_SHLIB` APXS_INCLUDEDIR=`$(APXS) -q INCLUDEDIR` APXS_LD_SHLIB=`$(APXS) -q LD_SHLIB` APXS_LIBEXECDIR=`$(APXS) -q LIBEXECDIR` APXS_LDFLAGS_SHLIB=`$(APXS) -q LDFLAGS_SHLIB` APXS_SYSCONFDIR=`$(APXS) -q SYSCONFDIR` APXS_LIBS_SHLIB=`$(APXS) -q LIBS_SHLIB` # the default target all: mod_cpphello.so # compile the shared object file. use g++ instead of letting apxs call # ld so we end up with the right c++ stuff. We do this in two steps, # compile and link. # compile mod_cpphello.o: mod_cpphello.cpp g++ -c -fPIC -I$(APXS_INCLUDEDIR) -I/usr/include/apr-1/ $(APXS_CFLAGS) $(APXS_CFLAGS_SHLIB) -Wall -o $@ $< # link mod_cpphello.so: mod_cpphello.o g++ -fPIC -shared -o $@ $< $(APXS_LIBS_SHLIB) # install the shared object file into Apache install: all $(APXS) -i -a -n 'cpphello' mod_cpphello.so # display the apxs variables check_apxs_vars: @echo APXS_CC $(APXS_CC);\ echo APXS_TARGET $(APXS_TARGET);\ echo APXS_CFLAGS $(APXS_CFLAGS);\ echo APXS_SBINDIR $(APXS_SBINDIR);\ echo APXS_CFLAGS_SHLIB $(APXS_CFLAGS_SHLIB);\ echo APXS_INCLUDEDIR $(APXS_INCLUDEDIR);\ echo APXS_LD_SHLIB $(APXS_LD_SHLIB);\ echo APXS_LIBEXECDIR $(APXS_LIBEXECDIR);\ echo APXS_LDFLAGS_SHLIB $(APXS_LDFLAGS_SHLIB);\ echo APXS_SYSCONFDIR $(APXS_SYSCONFDIR);\ echo APXS_LIBS_SHLIB $(APXS_LIBS_SHLIB) # cleanup clean: rm -f *.so *.o *~ # install and activate shared object by reloading Apache to # force a reload of the shared object file reload: install restart # the general Apache start/restart/stop # procedures start: $(APACHECTL) start restart: $(APACHECTL) restart stop: $(APACHECTL) stop ``` ##コンパイルしてインストール ``` sudo make install sudo make stop sudo make start ``` サイトを表示してみて ``` Hello World! ``` と表示されていたら成功 ##参考 * <http://d.hatena.ne.jp/yutakikuchi/20131101/1383260489> * <http://blog.enjoitech.jp/article/131>
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment