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

📄 cgi.html.en

📁 Apache_2.0.59-Openssl_0.9 配置tomcat. Apache_2.0.59-Openssl_0.9 配置tomcat.
💻 EN
📖 第 1 页 / 共 2 页
字号:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
              This file is generated from xml source: DO NOT EDIT
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      -->
<title>Apache Tutorial: Dynamic Content with CGI - Apache HTTP Server</title>
<link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="../style/css/manual-loose-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" />
<link href="../images/favicon.ico" rel="shortcut icon" /></head>
<body id="manual-page"><div id="page-header">
<p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
<p class="apache">Apache HTTP Server Version 2.0</p>
<img alt="" src="../images/feather.gif" /></div>
<div class="up"><a href="./"><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/">Documentation</a> &gt; <a href="../">Version 2.0</a> &gt; <a href="./">How-To / Tutorials</a></div><div id="page-content"><div id="preamble"><h1>Apache Tutorial: Dynamic Content with CGI</h1>
<div class="toplang">
<p><span>Available Languages: </span><a href="../en/howto/cgi.html" title="English">&nbsp;en&nbsp;</a> |
<a href="../ja/howto/cgi.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
<a href="../ko/howto/cgi.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a></p>
</div>
</div>
<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#intro">Introduction</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#configuring">Configuring Apache to permit CGI</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#writing">Writing a CGI program</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#troubleshoot">But it's still not working!</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#behindscenes">What's going on behind the scenes?</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#libraries">CGI modules/libraries</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#moreinfo">For more information</a></li>
</ul></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">Introduction</a></h2>
    

    <table class="related"><tr><th>Related Modules</th><th>Related Directives</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>The CGI (Common Gateway Interface) defines a way for a web
    server to interact with external content-generating programs,
    which are often referred to as CGI programs or CGI scripts. It
    is the simplest, and most common, way to put dynamic content on
    your web site. This document will be an introduction to setting
    up CGI on your Apache web server, and getting started writing
    CGI programs.</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">Configuring Apache to permit CGI</a></h2>
    

    <p>In order to get your CGI programs to work properly, you'll
    need to have Apache configured to permit CGI execution. There
    are several ways to do this.</p>

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

      <p>The 
      <code class="directive"><a href="../mod/mod_alias.html#scriptalias">ScriptAlias</a></code>

      directive tells Apache that a particular directory is set
      aside for CGI programs. Apache will assume that every file in
      this directory is a CGI program, and will attempt to execute
      it, when that particular resource is requested by a
      client.</p>

      <p>The <code class="directive"><a href="../mod/mod_alias.html#scriptalias">ScriptAlias</a></code>
      directive looks like:</p>

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

      <p>The example shown is from your default <code>httpd.conf</code>
      configuration file, if you installed Apache in the default
      location. The <code class="directive"><a href="../mod/mod_alias.html#scriptalias">ScriptAlias</a></code>
      directive is much like the <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code> directive, which defines a URL prefix that
      is to mapped to a particular directory. <code class="directive">Alias</code>
      and <code class="directive">ScriptAlias</code> are usually used for
      directories that are outside of the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code> directory. The difference between
      <code class="directive">Alias</code> and <code class="directive">ScriptAlias</code>
      is that <code class="directive">ScriptAlias</code> has the added meaning
      that everything under that URL prefix will be considered a CGI
      program. So, the example above tells Apache that any request for a
      resource beginning with <code>/cgi-bin/</code> should be served from
      the directory  <code>/usr/local/apache2/cgi-bin/</code>, and should be
      treated as a CGI program.</p>

      <p>For example, if the URL
      <code>http://www.example.com/cgi-bin/test.pl</code>
      is requested, Apache will attempt to execute the file 
      <code>/usr/local/apache2/cgi-bin/test.pl</code>
      and return the output. Of course, the file will have to
      exist, and be executable, and return output in a particular
      way, or Apache will return an error message.</p>
    

    <h3><a name="nonscriptalias" id="nonscriptalias">CGI outside of ScriptAlias directories</a></h3>
      

      <p>CGI programs are often restricted to <code class="directive"><a href="../mod/mod_alias.html#scriptalias">ScriptAlias</a></code>'ed directories for security reasons.
      In this way, administrators can tightly control who is allowed to
      use CGI programs. However, if the proper security precautions are
      taken, there is no reason why CGI programs cannot be run from
      arbitrary directories. For example, you may wish to let users
      have web content in their home directories with the 
      <code class="directive"><a href="../mod/mod_userdir.html#userdir">UserDir</a></code> directive.
      If they want to have their own CGI programs, but don't have access to
      the main <code>cgi-bin</code> directory, they will need to be able to
      run CGI programs elsewhere.</p>

      <p>There are two steps to allowing CGI execution in an arbitrary
      directory.  First, the <code>cgi-script</code> handler must be
      activated using the <code class="directive"><a href="../mod/mod_mime.html#addhandler">AddHandler</a></code> or <code class="directive"><a href="../mod/core.html#sethandler">SetHandler</a></code> directive.  Second,
      <code>ExecCGI</code> must be specified in the <code class="directive"><a href="../mod/core.html#options">Options</a></code> directive.</p> 
    

    <h3><a name="options" id="options">Explicitly using Options to permit CGI execution</a></h3>
      

      <p>You could explicitly use the <code class="directive"><a href="../mod/core.html#options">Options</a></code> directive, inside your main server configuration
      file, to specify that CGI execution was permitted in a particular
      directory:</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>The above directive tells Apache to permit the execution
      of CGI files. You will also need to tell the server what
      files are CGI files. The following <code class="directive"><a href="../mod/mod_mime.html#addhandler">AddHandler</a></code> directive tells the server to treat all
      files with the <code>cgi</code> or <code>pl</code> extension as CGI
      programs:</p>

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

    <h3><a name="htaccess" id="htaccess">.htaccess files</a></h3>
      

      <p>The <a href="htaccess.html"><code>.htaccess</code> tutorial</a>
      shows how to activate CGI programs if you do not have
      access to <code>httpd.conf</code>.</p>
    

    <h3><a name="userdir" id="userdir">User Directories</a></h3>
      

      <p>To allow CGI program execution for any file ending in
      <code>.cgi</code> in users' directories, you can use the
      following configuration.</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>If you wish designate a <code>cgi-bin</code> subdirectory of
      a user's directory where everything will be treated as a CGI
      program, you can use the following.</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">Writing a CGI program</a></h2>
    

    <p>There are two main differences between ``regular''
    programming, and CGI programming.</p>

    <p>First, all output from your CGI program must be preceded by
    a MIME-type header. This is HTTP header that tells the client
    what sort of content it is receiving. Most of the time, this
    will look like:</p>

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

    <p>Secondly, your output needs to be in HTML, or some other
    format that a browser will be able to display. Most of the
    time, this will be HTML, but occasionally you might write a CGI
    program that outputs a gif image, or other non-HTML
    content.</p>

    <p>Apart from those two things, writing a CGI program will look
    a lot like any other program that you might write.</p>

    <h3><a name="firstcgi" id="firstcgi">Your first CGI program</a></h3>
      

      <p>The following is an example CGI program that prints one
      line to your browser. Type in the following, save it to a
      file called <code>first.pl</code>, and put it in your 
      <code>cgi-bin</code> directory.</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>Even if you are not familiar with Perl, you should be able
      to see what is happening here. The first line tells Apache
      (or whatever shell you happen to be running under) that this
      program can be executed by feeding the file to the
      interpreter found at the location <code>/usr/bin/perl</code>.
      The second line prints the content-type declaration we
      talked about, followed by two carriage-return newline pairs.
      This puts a blank line after the header, to indicate the end
      of the HTTP headers, and the beginning of the body. The third
      line prints the string "Hello, World.". And that's the end
      of it.</p>

      <p>If you open your favorite browser and tell it to get the
      address</p>

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

      <p>or wherever you put your file, you will see the one line 
      <code>Hello, World.</code> appear in your browser window.
      It's not very exciting, but once you get that working, you'll
      have a good chance of getting just about anything working.</p>
    
  </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="troubleshoot" id="troubleshoot">But it's still not working!</a></h2>
    

    <p>There are four basic things that you may see in your browser
    when you try to access your CGI program from the web:</p>

    <dl>
      <dt>The output of your CGI program</dt>
      <dd>Great! That means everything worked fine.  If the output is correct,
      but the browser is not processing it correctly, make sure you have the
      correct <code>Content-Type</code> set in your CGI program.</dd>

      <dt>The source code of your CGI program or a "POST Method Not
      Allowed" message</dt>
      <dd>That means that you have not properly configured Apache
      to process your CGI program. Reread the section on 
      <a href="#configuring">configuring
      Apache</a> and try to find what you missed.</dd>

      <dt>A message starting with "Forbidden"</dt>
      <dd>That means that there is a permissions problem. Check the
      <a href="#errorlogs">Apache error log</a> and the section below on
      <a href="#permissions">file permissions</a>.</dd>

      <dt>A message saying "Internal Server Error"</dt>
      <dd>If you check the 
      <a href="#errorlogs">Apache error log</a>, you will probably
      find that it says "Premature end of

⌨️ 快捷键说明

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