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

📄 asgxc.htm

📁 apache技术手册
💻 HTM
📖 第 1 页 / 共 3 页
字号:

<P>Data is read and written through the standard input, output, and error streams. FastCGI also provides macros that map files and streams to native calls supported by your operating system.

<BR>

<P>Like CGI, you can create FastCGI applications in almost any language. However, you are currently limited to the ports of the library. Currently available are Perl, C/C++, Tcl, Java, and very soon Python, which should be available by the time you read this. Also in the works is a multithreaded C/C++ library that has not been released, but should be available as part of the 1.6 release. The multithreaded library allows a single application process to handle concurrent requests, which will allow you to implement things like HTTP-based chat applications.

<BR>

<P>Even though FastCGI is not universal, most developers should find themselves at home in one of the programming languages previously mentioned. As soon as FastCGI gains more acceptance, there will be additional libraries implemented. Developers are encouraged to port the FastCGI libraries to their programming language of choice, ensuring that the openness of the extension is more widely supported. The success of FastCGI will depend on getting many vendors and programmers to support it. Given its current feature set, it should have no trouble reaching this goal.

<BR>

<P>The design of FastCGI also wins big on the learning curve because unlike server APIs, you are still programming a CGI, so you can leverage what you already know. The only issues that you will need to address have to do with reorganizing your application so that the initialization code, which is done once, is kept separate from the application body. FastCGI applications are long-lived; they are kept alive between transactions. This also means that memory needs to be managed because unlike CGIs, which have a short life span, FastCGI processes may execute for undetermined amounts of time.

<BR>

<P>Data sent to a FastCGI application by the server is accessed through special streams that provide complete binary compatibility with the CGI standard. This also allows a FastCGI program to run as a regular CGI program. A FastCGI program can determine, at runtime, if it is being run as CGI or as FastCGI and behave accordingly.

<BR>

<P>This translates into an environment that allows you to migrate FastCGI programs down, should you ever need to. This provides server independence because the same binary can be run on two servers&#151;say Apache and Netscape&#151;under the same operating system without even needing to be rebuilt or require programming modifications, even if the server couldn't support FastCGI. This feature alone is very interesting from a legacy and recycling standpoint. Also, all servers support FastCGI. The FastCGI Developer's Kit comes with a program called cgi-fcgi that allows you to run FastCGI responder applications. The cgi-fcgi program allows any Web server that supports CGI to run FastCGI.

<BR>

<P>FastCGI applications communicate with Apache using a single full-duplex connection. Through this connection, the server transmits the environment variables and stdin to the FastCGI application; stdout and stderr streams from the application are sent back to the server.

<BR>

<BR>

<A NAME="E68E186"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>What a FastCGI Application Looks Like</B></FONT></CENTER></H3>

<BR>

<P>A modified version of my HelloWorld.c looks like this:

<BR>

<PRE>

<FONT COLOR="#000080">#include &quot;fcgi_stdio.h&quot;

#include &lt;stdio.h&gt;

int main (void)

{

 int timesVisited = 0;

 while(FCGI_Accept() &gt;=0)

 {

 printf (&quot;Content-type: text/html\r\n\r\n&quot;);

 printf(&quot;&lt;HTML&gt;&quot;);

 printf(&quot;&lt;HEAD&gt;&lt;TITLE&gt;Hello World!&lt;/TITLE&gt;&lt;/HEAD&gt;&quot;);

 printf(&quot;&lt;BODY&gt;&lt;H1&gt;Hello, this is a FastCGI program!&lt;/H1&gt;&quot;);

 printf(&quot;&lt;BIG&gt;&lt;P&gt;I can tell that you are visiting from %s.&lt;/P&gt;&quot;);

 printf(&quot;&lt;p&gt;This page has been accessed: %d times&lt;/P&gt;&lt;/BIG&gt;&lt;/BODY&gt;&quot;, getenv(&quot;REMOTE_HOST&quot;), ++timesVisited);

 printf(&quot;&lt;/HTML&gt;&quot;);

 }

}</FONT></PRE>

<P>This version makes use of the fact that the application is persistent and will maintain a count of the times the program is run (until the program dies).

<BR>

<P>As you can see from this example, FastCGI applications follow this sequence:

<BR>

<OL>

<LI>Initialization. Persistent connections or data that should be available from request to request are initialized in this section. Initialization is done only once. The initial environment for FastCGI applications is set through the AppClass directive, which is added by the FastCGI module.

<BR>

<BR>

<LI>The Response Loop. This loop is started by the FCGI_Accept() routine, implemented in the FastCGI library. A call to this routine blocks program execution until the program receives a client request. When it receives one, the routine unblocks, sets up an environment for the program, and runs one loop through the body. This routine is also responsible for determining the context under which the program is running (FastCGI or CGI) and sets the environment accordingly.

<BR>

<BR>

<LI>Body. This portion of the program gets executed by each request. This is the meat of your program. Each request will have its own environment variables, just as in a regular CGI.

<BR>

<BR>

<LI>End of Response. A subsequent call to FCGI_Accept informs the server that the program has completed a request and is ready for another. FCGI_Accept once more blocks execution until the next request.

<BR>

<BR>

</OL>

<P>This process is repeated until the FastCGI application is killed by the System administrator or the Web server. If the process were to die, the FastCGI module or cgi-fcgi program in the Web server is responsible for making a FastCGI process available to handle the request. This means that if it dies, the server will fork another process to replace the original.

<BR>

<BR>

<A NAME="E69E212"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>FastCGI and C</B></FONT></CENTER></H4>

<BR>

<P>FastCGI implements two libraries: fcgi_stdio and fcgiapp. Both of these libraries are provided for building C applications utilizing the FastCGI toolkit.

<BR>

<P>The fcgi_stdio library implements stdio-compatible functionality and can be used for developing or porting existing C CGI applications. This library provides full binary compatibility between FastCGI and CGI programs.

<BR>

<P>fcgiapp provides additional functionality to FastCGI applications at the expense of losing the CGI compatibility. There's also an increase in the knowledge required to develop the application.

<BR>

<BR>

<A NAME="E69E213"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>FastCGI with Perl and Tcl</B></FONT></CENTER></H4>

<BR>

<P>You can run Perl- and Tcl-interpreted programs under FastCGI. In order to run them, you'll have to compile a specially modified version of the interpreter that has been modified to work with FastCGI. You will not, however, need to maintain both a regular and a special version of the interpreter. The special version will work as expected when used under a normal context. Future versions of Perl and Tcl will incorporate changes that will make it possible to use unmodified versions of the program with FastCGI.

<BR>

<P>Patches for both Perl and Tcl are available. Some prebuilt binaries are also available at http://www.fastcgi.com.

<BR>

<BR>

<A NAME="E68E187"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Installing and Configuring FastCGI</B></FONT></CENTER></H3>

<BR>

<P>To build and run FastCGI applications, you'll need to have two different pieces of software: the FastCGI module for Apache and the FastCGI Developer's Kit.

<BR>

<P>Both the toolkit and the Apache module are included in the CD-ROM that accompanies this book; however, you should make sure that they are still current. At the time of this writing, the Apache module was still based on a beta version of 1.1. The latest and greatest versions of the module and developer's kit are available at the FastCGI Web sites: <A HREF="javascript:if(confirm('http://www.fastcgi.com/servers  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.fastcgi.com/servers'" tppabs="http://www.fastcgi.com/servers"> http://www.fastcgi.com/servers</A> and <A HREF="javascript:if(confirm('http://www.fastcgi.com/applibs  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.fastcgi.com/applibs'" tppabs="http://www.fastcgi.com/applibs"> http://www.fastcgi.com/applibs</A>, respectively. The version of the toolkit included on the CD-ROM is 1.5.

<BR>

<BR>

<A NAME="E69E214"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Incorporating FastCGI Functionality into Apache</B></FONT></CENTER></H4>

<BR>

<P>To add the FastCGI module to Apache is simple. Copy apache-fastcgi/src/mod_fastcgi.c to the Apache source directory (/usr/local/etc/httpd/src). Then you need to add an entry for the module in Apache's src/Configuration file. Add the following line at the end of the Module listings:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">Module fastcgi_module mod_fastcgi.o</FONT></PRE>

<P>You'll need to run Apache's configuration program, src/Configure, and rebuild the Apache by issuing a make command while focused on the Apache src directory.

<BR>

<P>While Apache builds, you can add the following configuration directives to your servers conf/srm.conf configuration file:

<BR>

<PRE>

<FONT COLOR="#000080">AddType application/x-httpd-fcgi .fcgi

Alias /fcgi-bin/ /usr/local/etc/httpd/fcgi-bin

# AppClass</FONT></PRE>

<P>Create the fcgi-bin directory, while in /usr/local/etc/httpd:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">% mkdir fcgi-bin</FONT></PRE>

<P>As you may be able to guess by now, FastCGI applications should be run from a directory other than the cgi-bin directory.

<BR>

<P>After Apache builds, issue the following commands:

<BR>

<PRE>

<FONT COLOR="#000080">% cd /usr/local/etc/httpd

% mv httpd httpd.prev

% mv src/httpd .

% strip httpd

% kill -HUP 'cat /usr/local/etc/httpd/logs/httpd.pid`</FONT></PRE>

<P>This will restart the server and force rereading of the new configuration directives you just added. Watch for any error messages. If you get an error, more than likely one of the directives you added is incorrect.

<BR>

<P>If the restart of the server proceeded without problems, congratulations&#151;your sever is now ready to run FastCGI programs.

<BR>

<BR>

<A NAME="E69E215"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>cgi-fcgi</B></FONT></CENTER></H4>

<BR>

<P>An alternative way of implementing FastCGI on Apache, or any other server, is to use the cgi-fcgi program. cgi-fcgi is a standard CGI program that uses TCP/IP sockets to communicate with FastCGI applications. cgi-fcgi takes the pathname or host/port name as a parameter and connects to the FastCGI application listening on that TCP port. cgi-fcgi then forwards the environment variables and stdin to the FastCGI application and returns to the server the stdout and stderr streams. When the FastCGI application ends the connection, cgi-fcgi flushes and exits.

<BR>

<P>Although this is not as efficient as a module imbedded into the server because the cgi-fcgi program gets forked with every request, it is much better than not having it. For example, if your Perl FastCGI program takes 1 second to compile and 2 seconds to connect to a database, you'll still have significant savings over forking Perl and recompiling the program with each request. cgi-fcgi is a tiny program when compared to Perl and many other programs.

<BR>

<BR>

<A NAME="E68E188"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Building the FastCGI Developer's Kit</B></FONT></CENTER></H3>

<BR>

<P>To build the developer's kit, you'll need to configure it. The folks at Open Market have provided a nice script that automates the configuration process to run it. Just type ./configure while focused inside the fcgi-devel-kit directory. After a few moments you should be able to type make and have the libraries built. You will need this kit to build FastCGI-savvy interpreters or C programs.

<BR>

<P>After you build the kit, you may be interested in installing the library libfcgi/libfcgi.a to some place useful such as /usr/local/lib. Remember to do a ranlib on the library after you move it to refresh it. While you're at it, you may want to copy the include directory to /usr/local/include/fcgi. That way it will be easier for you to reference it while building your own programs.

<BR>

<P>After the kit is built, you may want to try your luck at building the sample HelloWorld.c program listed earlier. Note that you may need to change the location of the fcgi_stdio.h header file to reflect its new location.

<BR>

<P>To build the FastCGI program, issue the following commands:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">cc -o Hello.fcgi -lfcgi HelloWorld.c</FONT></PRE>

<P>Put the resulting Hello.fcgi on your fcgi-bin directory. Before you can access it, you'll need to add an AppClass entry into your srm.conf:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">AppClass /usr/local/etc/httpd/fcgi-bin/Hello.fcgi</FONT></PRE>

<P>and restart the server with

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">% kill -HUP `cat /usr/local/etc/httpd/logs/httpd.pid`</FONT></PRE>

<P>The AppClass directive takes care of starting and maintaining the FastCGI application. At this point you should be able to access it by pointing your browser to http://localhost/fcgi-bin/Hello.fcgi.

<BR>

<P>You should get a similar result to those displayed in Figure C.2.

<BR>

<P><B> <A HREF="javascript:if(confirm('http://docs.rinet.ru:8080/Apachu/xc02.gif  \n\nThis file was not retrieved by Teleport Pro, because it was redirected to an invalid location.  You should report this problem to the site\'s webmaster.  \n\nDo you want to open it from the server?'))window.location='http://docs.rinet.ru:8080/Apachu/xc02.gif'" tppabs="http://docs.rinet.ru:8080/Apachu/xc02.gif">Figure C.2. The FastCGI version of </B><B>Hello World!</B><B>. </B><B>Notice that it keeps state. My version is fancier than the </B><B>HelloWorld</B><B> </B><B>listing.</A></B>

<BR>

<BR>

<A NAME="E68E189"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Making Perl Fly</B></FONT></CENTER></H3>

<BR>

<P>You will notice an even bigger improvement on Perl CGIs. This is because FastCGI will keep the Perl program running; the interpreter won't have to fork, exec, compile, and execute for each request. A good thing.

<BR>

<P>Before you can incorporate FastCGI into your Perl programs, you have to build a special version of Perl. The FastCGI Developer's Kit contains the patches you'll need to build a version of Perl that supports FastCGI. After you build this version, there's no need to keep your old Perl around. A FastCGI-savvy Perl binary will work just fine with regular Perl scripts.

<BR>

<P>Future versions of Perl may have support for FastCGI right out of the box. Currently there's an active discussion on the perl5-porters mailing list regarding the issues that need to change in Perl's implementation to support FastCGI as a true Perl module&#151;that is, requiring no recompiling. The Tcl7.5 FastCGI module, when it makes its debut, won't require a rebuilt of Tcl. The new Python module also doesn't need a rebuild.

<BR>

<P>At the time of this writing, the patches available for FastCGI were for version 5.002 of Perl. By the time you read this, they will be updated to the current Perl version, 5.003.

⌨️ 快捷键说明

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