Using Apache as an Application Server


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

  • My book is about building IMAs

  • Using Apache as an Application Server

    1. 1. Using Apache as an Application Server Phillip J. Windley, Ph.D. Founder and CTO Kynetx
    2. 2. Assumptions You’ve heard of Apache You’ve heard of Perl You’d like to build Web applications with less effort
    3. 3. Anti-Assumptions You’re familiar with mod_perl You’re an Apache expert You know what an application ser ver is and why you’d want one
    4. 4. Application Server Program that supplies support for running applications: Process and thread management Security Logging Interprocess communication Request dispatching
    5. 5. Popular Application Ser vers You might have heard of Tomcat JBoss WebLogic WebSphere
    6. 6. Application Server Benefits Flexibility through indirection (intermediary) Resource management Focused developer roles Maintainability Security Scalability Feature augmentation
    7. 7. Kynetx
    8. 8. Context automation services to link tasks across multiple Web-sites
    9. 9. Dynamically rearrange the DOM (post-delivery) based on client supplied rules, Web services, and browser context
    10. 10. Rule Language rule frequent_archive_visitor is active { select using "/archives/d+/d+/" setting () pre { c = counter.archive_pages; } if counter.archive_pages > 2 within 3 days then { replace_html("prize_div", "You win the prize! You've seen " + c + " pages from the archives!") } fired { clear counter.archive_pages; } else { counter.archive_pages += 1 from 1; } }
    11. 11. Kynetx Information Flow
    12. 12. We need a rule interpreter that dynamically serves Javascript
    13. 13. Rule execution must be as fast as possible
    14. 14. Amazon AWS deployment and scaling a necessity
    15. 15. Possible Architectures Standard Web application Custom HTTP server Apache module
    16. 16. Architecture Decisions Implement rule interpreter as an Apache module No database for scalability No session pinning Store rules in versioned repository
    17. 17. Detailed Architecture
    18. 18. Apache as an Application Server
    19. 19. Apache Fast and efficient Portable Well supported Won’t go away (open source) Stable and reliable Plenty of features Extensible Easy to administer
    20. 20. Apache Modules
    21. 21. Modules are NOT CGI Programs Modules run inside the Apache process architecture Modules have access to and can replace all Apache ser vices Uses the ser ver API - not an embedded interpreter Access to every part of the HTTP request lifecycle
    22. 22. Apache Server Lifecycle
    23. 23. HTTP Request Lifecycle
    24. 24. We’ve got both kinds: C & Perl!
    25. 25. Apache Application Ser vices Configuration Process and thread management Security Logging Interprocess communication Request dispatching
    26. 26. Configuration Support Via Apache configuration file Setting variables that can be seen in Perl $r->dir_config('memcached_hosts')
    27. 27. Configuration <LocationMatch /kobj/> # env var to turn on rule logging PerlSetEnv KOBJ 1 # host names PerlSetVar init_host init PerlSetVar log_host logger PerlSetVar action_host cs PerlSetVar frag_host frag # mode => development, production, test PerlSetVar run_mode development PerlSetVar memcached_hosts PerlAddVar memcached_hosts SetHandler perl-script PerlHandler Kynetx # Perl module name </LocationMatch>
    28. 28. Process & Thread Management Apache 2.X uses multi-processing modules (MPMs) to provide a flexible way to change the execution model prefork - Apache 1.3 worker - Hybrid, based on pthreads OS specific - Windows, BeOS, etc. perchild - virtual hosts with custom UID’s
    29. 29. Process & Thread Management <IfModule prefork.c> StartServers 5 MinSpareServers 8 MaxSpareServers 15 MaxClients 150 MaxRequestsPerChild 0 </IfModule>
    30. 30. Apache manages the processes
    31. 31. Start with one process model and switch to another later
    32. 32. Security Apache HTTP access, authentication, and authorization (AAA) isn’t very good for people HTTP AAA works fine for machines (usually) Standard mod_auth mod_access You can also override the AAA phases of the HTTP request cycle
    33. 33. Avoid AAA code in your application
    34. 34. Interprocess Communication Lots of Perl Modules IPC::SysV IPC::Shareable IPC::Semaphore IPC::ShareLite Cache::Cache Apache::Session
    35. 35. Apache::Session
    36. 36. Apache::Session Example my $mem_servers = Kynetx::Memcached::get_memcachedb_servers(); tie %{$session}, 'Apache::Session::Memcached', $cookie, { Servers => $mem_servers, NoRehash => 1, Readonly => 0, Debug => 1, CompressThreshold => 10_000 }; return $session;
    37. 37. Logging Apache has a great logging facility Use the standard Apache logging commands Apache2::Log
    38. 38. Logging LogFormat "%h %t %{TOTAL_SECS}e %{SITE}e %{TXN_ID}e "%{CALLER}e" % {SID}e "%{REFERER}e" "%{TITLE}e" [%{RULE_NAMES}e] [%{RESULTS}e] [% {ACTIONS}e] [%{TAGS}e] [%{LABELS}e] " KOBJ CustomLog "|/web/sbin/cronolog --period=5 --symlink=/web/logs/ kynetx_log /web/logs/kynetx/KOBJ-%Y%m%d-%H%M.log" KOBJ env=KOBJ
    39. 39. Logging $r->subprocess_env(SITE => $request_info->{'site'}); $r->subprocess_env(RULE_NAMES => join(',', @{ $rule_env->{'names'}})); $r->subprocess_env(RESULTS => join(',', @{ $rule_env->{'results'}}));
    40. 40. Request Dispatching Request dispatching says what to do for each URL or URL pattern Turns out that Apache is really good at this Request dispatching is known as “routes” in Rails
    41. 41. Dispatch Example Translate to date=20021031;id=09;page=index.html before news sees it.
    42. 42. Three Methods Use configuration file Use mod_rewrite Use custom Translation stage handler You can do anything you want here
    43. 43. Request Dispatching Example <LocationMatch /kobj/> SetHandler perl-script PerlHandler Kynetx # Perl module name </LocationMatch> <LocationMatch /js/> SetHandler perl-script PerlHandler Kynetx::KOBJ # Perl module name </LocationMatch> <LocationMatch /cb/> SetHandler perl-script PerlHandler Kynetx::Callbacks # Perl module name </LocationMatch>
    44. 44. What’s Missing? Event monitoring Timers Long running processes
    45. 45. Other Tools You Might Use mod_header mod_deflate Authentication modules Authorization modules
    46. 46. The End 46
    47. 47. Contact Information Contact me Questions?