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

📄 cgi.html.en

📁 Apache V2.0.15 Alpha For Linuxhttpd-2_0_15-alpha.tar.Z
💻 EN
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><title>Apache Tutorial: Dynamic Content with CGI</title><link rev="made" href="mailto:rbowen@rcbowen.com"></head><!-- Background white, links blue (unvisited), navy (visited), red (active) --><body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#000080"alink="#FF0000"><!--#include virtual="header.html" --><h1 align="CENTER">Dynamic Content with CGI</h1><a name="__index__"></a> <!-- INDEX BEGIN --> <ul><li><a href="#dynamiccontentwithcgi">Dynamic Content withCGI</a></li><li><a href="#configuringapachetopermitcgi">Configuring Apache topermit CGI</a><ul><li><a href="#scriptalias">ScriptAlias</a></li><li><a href="#cgioutsideofscriptaliasdirectories">CGI outside ofScriptAlias directories</a><ul><li><a href="#explicitlyusingoptionstopermitcgiexecution">Explicitly usingOptions 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 notworking!</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 behindthe 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 name="dynamiccontentwithcgi">Dynamic Content withCGI</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 serverto interact with external content-generating programs, which are oftenreferred to as CGI programs or CGI scripts.  It is the simplest, andmost common, way to put dynamic content on your web site. Thisdocument will be an introduction to setting up CGI on your Apache webserver, and getting started writing CGI programs.</p><hr><h2><a name="configuringapachetopermitcgi">Configuring Apache topermit CGI</a></h2><p>In order to get your CGI programs to work properly, you'll need tohave Apache configured to permit CGI execution. There are several waysto do this.</p><h3><a name="scriptalias">ScriptAlias</a></h3><p>The <code>ScriptAlias</code> directive tells Apache that aparticular directory is set aside for CGI programs. Apache will assumethat every file in this directory is a CGI program, and will attempt toexecute 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 tomapped to a particular directory. <code>Alias</code> and<code>ScriptAlias</code> are usually used for directories that areoutside of the <code>DocumentRoot</code> directory. The differencebetween <code>Alias</code> and <code>ScriptAlias</code> is that<code>ScriptAlias</code> has the added meaning that everything underthat URL prefix will be considered a CGI program. So, the example abovetells 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 CGIprogram.</p><p>For example, if the URL<code>http://dev.rcbowen.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 returnoutput in a particular way, or Apache will return an error message.</p><h3><a name="cgioutsideofscriptaliasdirectories">CGI outside ofScriptAlias directories</a></h3><p>CGI programs are often restricted to <code>ScriptAlias</code>'eddirectories for security reasons.  In this way, administrators cantightly control who is allowed to use CGI programs.  However, if theproper security precautions are taken, there is no reason whyCGI programs cannot be run from arbitrary directories.  For example,you may wish to let users have web content in their home directorieswith the <code>UserDir</code> directive. If they want to have theirown CGI programs, but don't have access to the main<code>cgi-bin</code> directory, they will need to be able to run CGIprograms elsewhere.</p><h3><a name="explicitlyusingoptionstopermitcgiexecution">Explicitly usingOptions to permit CGI execution</a></h3><p>You could explicitly use the <code>Options</code> directive, insideyour main server configuration file, to specify that CGI execution waspermitted 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 CGIfiles. You will also need to tell the server what files are CGI files.The following <code>AddHandler</code> directive tells the serverto 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 name="htaccessfiles">.htaccess files</a></h3><p>A <code>.htaccess</code> file is a way to set configurationdirectives on a per-directory basis. When Apache serves a resource, itlooks in the directory from which it is serving a file for a filecalled <code>.htaccess</code>, and, if it finds it, it will applydirectives found therein. <code>.htaccess</code> files can be permittedwith the <code>AllowOverride</code> directive, which specifies whattypes of directives can appear in these files, or if they are notallowed at all. To permit the directive we will need for this purpose,the following configuration will be needed in your main serverconfiguration:</p><pre>        AllowOverride Options</pre><p>In the <code>.htaccess</code> file, you'll need the followingdirective:</p><pre>        Options +ExecCGI</pre><p>which tells Apache that execution of CGI programs is permitted inthis directory.</p><hr><h2><a name="writingacgiprogram">Writing a CGI program</a></h2><p>There are two main differences between ``regular'' programming, andCGI programming.</p><p>First, all output from your CGI program must be preceded by aMIME-type header. This is HTTP header that tells the client what sortof 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 thata 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 gifimage, or other non-HTML content.</p><p>Apart from those two things, writing a CGI program will look a lotlike any other program that you might write.</p><h3><a name="yourfirstcgiprogram">Your first CGI program</a></h3><p>The following is an example CGI program that prints one line to yourbrowser. 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 seewhat is happening here. The first line tells Apache (or whatever shellyou happen to be running under) that this program can be executed byfeeding the file to the interpreter found at the location<code>/usr/bin/perl</code>. The second line prints the content-typedeclaration we talked about, followed by two carriage-return newline

⌨️ 快捷键说明

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