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

📄 cgi.html.en

📁 apache 安装教程 apache 安装教程
💻 EN
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">  <head>    <meta name="generator" content="HTML Tidy, see www.w3.org" />    <title>Apache Tutorial: Dynamic Content with CGI</title>  </head>  <!-- Background white, links blue (unvisited), navy (visited), red (active) -->  <body bgcolor="#FFFFFF" text="#000000" link="#0000FF"  vlink="#000080" alink="#FF0000">        <div align="CENTER">      <img src="../images/sub.gif" alt="[APACHE DOCUMENTATION]" />      <h3>Apache HTTP Server Version 1.3</h3>        <p><small><em>Is this the version you want?  For more recent         versions, check our <a href="/docs/">documentation          index</a>.</em></small></p>    </div>    <h1 align="CENTER">Dynamic Content with CGI</h1>    <a id="__index__" name="__index__"></a> <!-- INDEX BEGIN -->         <ul>      <li><a href="#dynamiccontentwithcgi">Dynamic Content with      CGI</a></li>      <li>        <a href="#configuringapachetopermitcgi">Configuring Apache        to permit CGI</a>         <ul>          <li><a href="#scriptalias">ScriptAlias</a></li>          <li>            <a href="#cgioutsideofscriptaliasdirectories">CGI            outside of ScriptAlias directories</a>             <ul>              <li><a              href="#explicitlyusingoptionstopermitcgiexecution">Explicitly              using Options to permit CGI execution</a></li>              <li><a href="#htaccessfiles">.htaccess files</a></li>            </ul>          </li>        </ul>      </li>      <li>        <a href="#writingacgiprogram">Writing a CGI program</a>         <ul>          <li><a href="#yourfirstcgiprogram">Your first CGI          program</a></li>        </ul>      </li>      <li>        <a href="#butitsstillnotworking">But it's still not        working!</a>         <ul>          <li><a href="#filepermissions">File permissions</a></li>          <li><a href="#pathinformation">Path information</a></li>          <li><a href="#syntaxerrors">Syntax errors</a></li>          <li><a href="#errorlogs">Error logs</a></li>        </ul>      </li>      <li>        <a href="#whatsgoingonbehindthescenes">What's going on        behind the scenes?</a>         <ul>          <li><a href="#environmentvariables">Environment          variables</a></li>          <li><a href="#stdinandstdout">STDIN and STDOUT</a></li>        </ul>      </li>      <li><a href="#cgimoduleslibraries">CGI      modules/libraries</a></li>      <li><a href="#formoreinformation">For more      information</a></li>    </ul>    <!-- INDEX END -->    <hr />    <h2><a id="dynamiccontentwithcgi"    name="dynamiccontentwithcgi">Dynamic Content with CGI</a></h2>    <table border="1">      <tr>        <td valign="top"><strong>Related Modules</strong><br />         <br />         <a href="../mod/mod_alias.html">mod_alias</a><br />         <a href="../mod/mod_cgi.html">mod_cgi</a><br />         </td>        <td valign="top"><strong>Related Directives</strong><br />         <br />         <a        href="../mod/mod_mime.html#addhandler">AddHandler</a><br />         <a href="../mod/core.html#options">Options</a><br />         <a        href="../mod/mod_alias.html#scriptalias">ScriptAlias</a><br />         </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>    <hr />    <h2><a id="configuringapachetopermitcgi"    name="configuringapachetopermitcgi">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 id="scriptalias" name="scriptalias">ScriptAlias</a></h3>    <p>The <code>ScriptAlias</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>ScriptAlias</code> directive looks like:</p><pre>        ScriptAlias /cgi-bin/ /usr/local/apache/cgi-bin/</pre>    <p>The example shown is from your default    <code>httpd.conf</code> configuration file, if you installed    Apache in the default location. The <code>ScriptAlias</code>    directive is much like the <code>Alias</code> directive, which    defines a URL prefix that is to mapped to a particular    directory. <code>Alias</code> and <code>ScriptAlias</code> are    usually used for directories that are outside of the    <code>DocumentRoot</code> directory. The difference between    <code>Alias</code> and <code>ScriptAlias</code> is that    <code>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/apache/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/apache/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 id="cgioutsideofscriptaliasdirectories"    name="cgioutsideofscriptaliasdirectories">CGI outside of    ScriptAlias directories</a></h3>    <p>CGI programs are often restricted to    <code>ScriptAlias</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>UserDir</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>    <h3><a id="explicitlyusingoptionstopermitcgiexecution"    name="explicitlyusingoptionstopermitcgiexecution">Explicitly    using Options to permit CGI execution</a></h3>    <p>You could explicitly use the <code>Options</code> directive,    inside your main server configuration file, to specify that CGI    execution was permitted in a particular directory:</p><pre>        &lt;Directory /usr/local/apache/htdocs/somedir&gt;                Options +ExecCGI        &lt;/Directory&gt;</pre>    <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>AddHandler</code> directive    tells the server to treat all files with the <code>cgi</code>    or <code>pl</code> extension as CGI programs:</p><pre>     AddHandler cgi-script cgi pl</pre>    <h3><a id="htaccessfiles" name="htaccessfiles">.htaccess    files</a></h3>    <p>A <code>.htaccess</code> file is a way to set configuration    directives on a per-directory basis. When Apache serves a    resource, it looks in the directory from which it is serving a    file for a file called <code>.htaccess</code>, and, if it finds    it, it will apply directives found therein.    <code>.htaccess</code> files can be permitted with the    <code>AllowOverride</code> directive, which specifies what    types of directives can appear in these files, or if they are    not allowed at all. To permit the directive we will need for    this purpose, the following configuration will be needed in    your main server configuration:</p><pre>        AllowOverride Options</pre>    <p>In the <code>.htaccess</code> file, you'll need the    following directive:</p><pre>        Options +ExecCGI</pre>    <p>which tells Apache that execution of CGI programs is    permitted in this directory.</p>    <hr />    <h2><a id="writingacgiprogram"    name="writingacgiprogram">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><pre>        Content-type: text/html</pre>    <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 id="yourfirstcgiprogram" name="yourfirstcgiprogram">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><pre>        #!/usr/bin/perl        print "Content-type: text/html\r\n\r\n";        print "Hello, World.";</pre>    <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,

⌨️ 快捷键说明

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