⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch17_03.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<?label 17.3. mod_perl?><html><head><title>mod_perl (CGI Programming with Perl)</title><link href="../style/style1.css" type="text/css" rel="stylesheet" /><meta name="DC.Creator" content="Scott Guelich, Gunther Birznieks and Shishir Gundavaram" /><meta scheme="MIME" content="text/xml" name="DC.Format" /><meta content="en-US" name="DC.Language" /><meta content="O'Reilly & Associates, Inc." name="DC.Publisher" /><meta scheme="ISBN" name="DC.Source" content="1565924193L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="CGI Programming with Perl" /><meta content="Text.Monograph" name="DC.Type" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" alt="Book Home" usemap="#banner-map" border="0" /><map name="banner-map"><area alt="CGI Programming with Perl" href="index.htm" coords="0,0,466,65" shape="rect" /><area alt="Search this book" href="jobjects/fsearch.htm" coords="467,0,514,18" shape="rect" /></map><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch17_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm">CGI Programming with Perl</a></td><td width="172" valign="top" align="right"><a href="appa_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h2 class="sect1">17.3. mod_perl</h2><p><em class="emphasis">mod_perl</em> is an Apache <a name="INDEX-3276" /> <a name="INDEX-3277" /><a name="INDEX-3278" />server extension that <a name="INDEX-3279" /><a name="INDEX-3280" /><a name="INDEX-3281" />embeds Perl within Apache, providing aPerl interface to the Apache API. This allows us to developfull-blown Apache modules in Perl to handle particular stages of aclient request. It was written by Doug MacEachern, and since it wasintroduced, its popularity has grown quickly.</p><p>The most popular <a name="INDEX-3282" /><a name="INDEX-3283" />Apache/Perlmodule is Apache::Registry, which emulates the CGI environment,allowing us to write CGI applications that run under<em class="emphasis">mod_perl</em>. Since Perl is embedded within theserver, we avoid the overhead of starting up an external interpreter.In addition, we can load and compile all the external Perl modules wewant to use at server startup, and not during the execution of ourapplication. Apache::Registry also caches compiled versions of ourCGI applications, thereby providing a further boost. Users havereported performance gains of up to 2000 percent in their CGIapplications using a combination of <em class="emphasis">mod_perl</em> andApache::Registry.</p><p>Apache::Registry is a <a name="INDEX-3284" /><a name="INDEX-3285" />response handler, which means that itis responsible for generating the response that will be sent back tothe client. It forms a layer over our CGI applications; it executesour applications and sends the resulting output back to the client.If you don't want to use Apache::Registry, you can implementyour own response handler to take care of the request. However, thesehandlers are quite different from standard CGI scripts, so wewon't discuss how to create handlers with<em class="emphasis">mod_perl</em>. To learn about handlers along withanything else you might want to know about<em class="emphasis">mod_perl</em>, refer to <em class="citetitle">Writing ApacheModules with Perl and C</em> by Lincoln Stein and DougMacEachern (O'Reilly &amp; Associates, Inc.).</p><a name="ch17-12-fm2xml" /><div class="sect2"><h3 class="sect2">17.3.1. Installation and Configuration</h3><p>Before we go <a name="INDEX-3286" /> <a name="INDEX-3287" />any further, let's<a name="INDEX-3288" />install<em class="emphasis">mod_perl</em>. You can obtain it from CPAN at<a href="http://www.cpan.org/modules/by-module/Apache/">http://www.cpan.org/modules/by-module/Apache/</a>.The Apache namespace is used by modules that are specific to<em class="emphasis">mod_perl</em>. The installation is relatively simpleand should proceed well:</p><blockquote><pre class="code">$ cd mod_perl-1.22$ perl Makefile.PL \&gt; APACHE_PREFIX=/usr/local/apache  \&gt; APACHE_SRC=../apache-1.3.12/src  \&gt; DO_HTTPD=1                       \&gt; USE_APACI=1                      \&gt; EVERYTHING=1 $ make$ make test$ su# make install</pre></blockquote><p>Refer to the installation directions that came with Apache and<em class="emphasis">mod_perl</em> if you want to perform a custominstallation. If you're not interested in possibly developingand implementing the various Apache/Perl<a name="INDEX-3289" />handlers, then you do not needthe <em class="emphasis">EVERYTHING=1</em> directive, in which case, youcan implement only a <em class="emphasis">PerlHandler</em>.</p><p>Once that's complete, we need to<a name="INDEX-3290" />configure Apache. Here's asimple setup:</p><blockquote><pre class="code">PerlRequire      /usr/local/apache/conf/startup.plPerlTaintCheck   OnPerlWarn         OnAlias /perl/ /usr/local/apache/perl/&lt;Location /perl&gt;SetHandler       perl-scriptPerlSendHeader   OnPerlHandler      Apache::RegistryOptions          ExecCGI&lt;/Location&gt;</pre></blockquote><p>As you can see, this is very similar to the manner in which weconfigured FastCGI. We use the <em class="emphasis">PerlRequire</em>directive to execute a startup script. Generally, this is where youwould pre-load all the modules that you intend to use (see <a href="ch17_03.htm#ch17-80670">Example 17-3</a>).</p><p>However, if you are interested in loading only a small set of modules(a limit of ten), you can use the <em class="emphasis">PerlModule</em>directive instead:</p><blockquote><pre class="code">PerlModule  CGI  DB_File  MLDBM  Storable</pre></blockquote><p>For <a name="INDEX-3291" /> <a name="INDEX-3292" /><a name="INDEX-3293" />Apache::Registryto honor taint mode and warnings, we must add directive the<tt class="function">PerlTaintMode</tt> and <tt class="function">PerlWarn</tt>directives. Otherwise, they won't be enabled. We do thisglobally. Then we configure the directory we are setting up to runour scripts.</p><p>All requests for resources in the <em class="emphasis">/perl</em>directory go through the <em class="emphasis">perl-script</em>(<em class="emphasis">mod_perl</em>) handler, which then passes therequest off to the Apache::Registry module. We also need to enablethe <em class="emphasis">ExecCGI</em> option. Otherwise,<a name="INDEX-3294" />Apache::Registrywill not execute our CGI applications.</p><p>Now, here's a sample configuration file in <a href="ch17_03.htm#ch17-80670">Example 17-3</a>.</p><a name="ch17-80670" /><div class="example"><h4 class="objtitle">Example 17-3. startup.pl </h4><blockquote><pre class="code">#!/usr/bin/perl -wTuse Apache::Registry;use CGI;## any other modules that you may need for your## other mod_perl applications running ...print "Finished loading modules. Apache is ready to go!\n";1;</pre></blockquote></div><p>It is really a very simple program, which does nothing but load themodules. We also want Apache::Registry to be pre-loaded sinceit'll be handling all of our requests. A thing to note here isthat each of Apache's child processes will have access to thesemodules.</p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -