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

📄 cgi.html

📁 这个是我在web培训时老师提供的手册
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="Apache, 中文, 手册, 中文版, 中文手册, 中文版手册, 参考手册, 中文参考手册, 金步国" />
<meta name="description" content="Apache 2.2 中文版参考手册" />
<meta name="author" content="金步国" />
<link href="../style/css/manual-zip.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="../style/css/manual-zip-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
<title>CGI动态页面 - Apache 2.2 中文版参考手册</title>
</head>
<body id="manual-page"><div id="page-header">
<p class="menu"><a href="../mod/index.html">模块索引</a> | <a href="../mod/directives.html">指令索引</a> | <a href="../faq/index.html">常见问题</a> | <a href="../glossary.html">词汇表</a> | <a href="../sitemap.html">站点导航</a></p><p class="apache">Apache HTTP Server 版本2.2</p><img alt="" src="../images/feather.gif" /></div>
<div class="up"><a href="./index.html"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
<div id="path"><a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">文档</a> &gt; <a href="../index.html">版本2.2</a> &gt; <a href="./index.html">如何.../指南</a></div>

<div id="translation-info">   <a href="../translator_announcement.html#thanks">致谢</a> | <a href="../translator_announcement.html#announcement">译者声明</a> | 本篇译者:<a href="mailto:&#099;sfr&#0097;nk&#0064;&#099;itiz&#046;n&#0101;t">金步国</a> | 本篇译稿最后更新:2006年1月8日 | <a href="../translator_announcement.html#last_new">获取最新版本</a></div>
<div id="page-content"><div id="preamble"><h1>CGI动态页面</h1>

</div>
	<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="intro" id="intro">简介</a></h2>
    

    <table border="1" cellpadding="0" cellspacing="0" bordercolor="#AAAAAA" class="related">
<tr><th>相关模块</th><th>相关指令</th></tr>
<tr><td><ul><li><code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code></li><li><code class="module"><a href="../mod/mod_cgi.html">mod_cgi</a></code></li></ul></td><td><ul><li><code class="directive"><a href="../mod/mod_mime.html#addhandler">AddHandler</a></code></li><li><code class="directive"><a href="../mod/core.html#options">Options</a></code></li><li><code class="directive"><a href="../mod/mod_alias.html#scriptalias">ScriptAlias</a></code></li></ul></td></tr>
</table>

    <p>CGI(公共网关接口)定义了web服务器与外部内容生成程序之间交互的方法,通常是指CGI程序或者CGI脚本,它是在网站上实现动态页面的最简单和常用的方法。本文将对如何在Apache web服务器上建立CGI以及如何编写CGI程序进行介绍。</p>
  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="configuring" id="configuring">配置Apache以允许CGI</a></h2>
    

    <p>要让CGI程序能正常运作,必须配置Apache以允许CGI的执行,其方法有多种。</p>

    <h3><a name="scriptalias" id="scriptalias">ScriptAlias</a></h3>
      

      <p><code class="directive"><a href="../mod/mod_alias.html#scriptalias">ScriptAlias</a></code>指令使Apache允许执行一个特定目录中的CGI程序。当客户端请求此特定目录中的资源时,Apache假定其中所有的文件都是CGI程序并试图运行它。</p>

      <p><code class="directive"><a href="../mod/mod_alias.html#scriptalias">ScriptAlias</a></code>指令形如:</p>

      <div class="example"><p><code>
        ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/
      </code></p></div>

      <p>如果Apache被安装到默认位置,默认的配置文件<code>httpd.conf</code>中就会有上述配置。<code class="directive"><a href="../mod/mod_alias.html#scriptalias">ScriptAlias</a></code>与<code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code>指令非常相似,都是定义了映射到一个特定目录的URL前缀,两者一般都用于指定位于<code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>以外的目录,其不同之处是<code class="directive">ScriptAlias</code>又多了一层含义,即URL前缀后面的任何文件都被视为CGI程序。所以,上述例子会指示Apache:任何以<code>/cgi-bin/</code>开头的资源都将映射到<code>/usr/local/apache2/cgi-bin/</code>目录中,且视之为CGI程序。</p>

      <p>例如,如果有URL为<code>http://www.example.com/cgi-bin/test.pl</code>的请求,Apache会试图执行<code>/usr/local/apache2/cgi-bin/test.pl</code>文件并返回其输出。当然,这个文件必须存在而且可执行,并以特定的方法产生输出,否则Apache返回一个出错消息。</p>
    

    <h3><a name="nonscriptalias" id="nonscriptalias">ScriptAlias目录以外的CGI</a></h3>
      

      <p>由于安全原因,CGI程序通常被限制在<code class="directive"><a href="../mod/mod_alias.html#scriptalias">ScriptAlias</a></code>指定的目录中,这样,管理员就可以严格控制谁可以使用CGI程序。但是,如果采取了恰当的安全措施,则没有理由不允许其他目录中的CGI程序运行。比如,你可能希望用户在<code class="directive"><a href="../mod/mod_userdir.html#userdir">UserDir</a></code>指定的宿主目录中存放页面,而他们有自己的CGI程序,但无权访问<code>cgi-bin</code>目录,这样,就产生了运行其他目录中CGI程序的需求。</p>

      <p>允许CGI在任意目录执行需要两个步骤:第一步,必须用<code class="directive"><a href="../mod/mod_mime.html#addhandler">AddHandler</a></code>或<code class="directive"><a href="../mod/core.html#sethandler">SetHandler</a></code>指令激活<code>cgi-script</code>处理器。第二步,必须在<code class="directive"><a href="../mod/core.html#options">Options</a></code>指令中启用<code>ExecCGI</code>选项。</p> 
    

    <h3><a name="options" id="options">用Options显式地允许CGI的执行</a></h3>
      

      <p>可以在主配置文件中,使用<code class="directive"><a href="../mod/core.html#options">Options</a></code>指令显式地允许特定目录中CGI的执行:</p>

      <div class="example"><p><code>
        &lt;Directory /usr/local/apache2/htdocs/somedir&gt;<br />
        <span class="indent">
          Options +ExecCGI<br />
        </span>
        &lt;/Directory&gt;
      </code></p></div>

      <p>上述指令使Apache允许CGI文件的执行。另外,还必须告诉服务器哪些文件是CGI文件。下面的<code class="directive"><a href="../mod/mod_mime.html#addhandler">AddHandler</a></code>指令告诉服务器所有带有<code>cgi</code>或<code>pl</code>后缀的文件是CGI程序:</p>

      <div class="example"><p><code>
        AddHandler cgi-script .cgi .pl
      </code></p></div>
    

    <h3><a name="htaccess" id="htaccess">.htaccess文件</a></h3>
      

      <p><a href="htaccess.html"><code>.htaccess</code>指南</a>示范了怎样在没有权限修改<code>httpd.conf</code>文件的情况下激活CGI程序。</p>
    

    <h3><a name="userdir" id="userdir">用户目录</a></h3>
      

      <p>为了允许用户目录中所有以"<code>.cgi</code>"结尾的文件作为CGI程序执行,你可以使用以下配置:</p>

      <div class="example"><p><code>
      &lt;Directory /home/*/public_html&gt;<br />
      <span class="indent">
        Options +ExecCGI<br />
        AddHandler cgi-script .cgi<br />
      </span>
      &lt;/Directory&gt;
      </code></p></div>

      <p>如果你想在用户目录中指定一个<code>cgi-bin</code>子目录,其中所有的文件都被当作CGI程序,你可以这样配置:</p>

      <div class="example"><p><code>
      &lt;Directory /home/*/public_html/cgi-bin&gt;<br />
      <span class="indent">
        Options ExecCGI<br />
        SetHandler cgi-script<br />
      </span>
      &lt;/Directory&gt;
      </code></p></div>

    

  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="writing" id="writing">编写CGI程序</a></h2>
    

    <p>编写CGI程序和"常规"程序之间有两个主要的不同。</p>

    <p>首先,在CGI程序的所有输出前面必须有一个HTTP的<a class="glossarylink" href="../glossary.html#mime-type" title="see glossary">MIME类型</a>的头,对客户端指明所接收内容的类型,大多数情况下,像这样:</p>

    <div class="example"><p><code>
      Content-type: text/html
    </code></p></div>

    <p>其次,输出要求是HTML形式的,或者是浏览器可以显示的其他某种形式。多数情况下,输出是HTML形式的,但偶然也会输出一个gif图片或者其他非HTML的内容。</p>

    <p>除了这两点,编写CGI程序和编写其他程序大致相同。</p>

    <h3><a name="firstcgi" id="firstcgi">第一个CGI程序</a></h3>
      

      <p>这个CGI程序的例子在浏览器中打印一行文字。把下列存为<code>first.pl</code>文件,并放在你的<code>cgi-bin</code>目录中。</p>

      <div class="example"><p><code>
        #!/usr/bin/perl<br />
        print "Content-type: text/html\n\n";<br />
        print "Hello, World.";
      </code></p></div>

      <p>即使不熟悉Perl语言,你也应该能看出它干了什么。第一行,告诉Apache这个文件可以用<code>/usr/bin/perl</code>(或者任何你正在使用的shell)解释并执行。第二行,打印上述要求的内容类型说明,并带有两个换行,在头后面留出空行,以示HTTP头的结束。第三行,打印文字"Hello, World."。程序到此结束。</p>

      <p>打开你喜欢的浏览器并输入地址:</p>

      <div class="example"><p><code>
        http://www.example.com/cgi-bin/first.pl
      </code></p></div>

      <p>或者是你存放程序的其他位置,就可以在浏览器窗口中看到一行:<code>Hello, World.</code> 。虽然并不怎么激动人心,但是一旦这个程序能正常运行,那么就可能运行其他任何程序。</p>
    
  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">

⌨️ 快捷键说明

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