欢迎光临
我们一直在努力

实战 FastCGI(转)二-CGI教程,CGI文档

建站超值云服务器,限时71元/月

2. 安装 fastcgi

要使用 fastcgi 你必需有一个可供 fastcgi 程序执行的环境 (run-time environment),以及 撰写 fastcgi 程序的开发环境。以下就以阿帕契服务器 (apache web server) 做为 fastcgi 的执行平台,简述如何在阿帕契服务器使用 fastcgi。

由于阿帕契服务器自由、开放、跨平台的特性,现今许多系统或发行套件 (distribution) 都内含阿帕契服务器,如果你直接用预先编译好的阿帕契程序,请自行找出符合该系统设定规则的安装路径。以下列出一些阿帕契服务器相关的路径设定规则,后面的范例将以阿帕契内定值为主,其它的系统请自行参考:

系统 执行文件路径 设定文件路径
阿帕契内定值 /usr/local/apache/bin /usr/local/apache/etc
freebsd /usr/local/sbin /usr/local/etc/apache
red hat linux /usr/sbin /usr/etc

2.1 在阿帕契服务器上安装 fastcgi 模块
安装 mod_fastcgi 这个模块,可以让你的阿帕契服务器支持 fastcgi 协议。mod_fastcgi 现在最新版本为 2.2.2 版,此版主要适用于 apache 1.3 版以上。如果你的 apache 还是 1.2 版,请配合 mod_fastcgi 2.0.18 版使用。以下设定以 apache 1.3.6 及 mod_fast 2.2.2 为示范。

2.1.1 标准安装 (利用 apaci)

1. 首先下载 apache_1.3.6.tar.gz 及 mod_fastcgi_2.2.2.tar.gz ,解开:
$ gunzip -c apache_1.3.6.tar.gz | tar xvf –
$ gunzip -c mod_fastcgi_2.2.2.tar.gz | tar xvf –
2. 把 mod_fastcgi 的原始码复制到 apache 的目录下:
$ cp -rp mod_fastcgi_2.2.2 apache_1.3.6/src/modules/fastcgi
3. 设定 apache 加入 mod_fastcgi 模块:
$ cd apache_1.3.6
$ ./configure -activate-module=src/modules/fastcgi/libfastcgi.a -enable-module=info -enable-shared=info [ more apaci options ]
4. 编译及安装
$ make
$ make install
5. 看一下编译出来的执行文件是否含有 mod_fastcgi 模块:
$ /usr/local/apache/sbin/httpd -l
compiled-in modules:
http_core.c

mod_fastcgi.c

2.1.2 将 mod_fastcgi 安装成一个 dso
假设你已经在系统上安装好 apache 1.3 版以上,并且你的平台支持 dso (dynamic shared object) 的方式动态加入模块,那么你可以透过 apxs (apache extension tool) 将 mod_fastcgi 安装成一个 dso 模块。

1. 下载 mod_fastcgi_2.2.2.tar.gz 并且解开:
$ gunzip -c mod_fastcgi_2.2.2.tar.gz | tar xvf –
2. 编译 mod_fastcgi 模块成 dso:
$ cd mod_fastcgi_2.2.2
$ /usr/local/apache/sbin/apxs -o mod_fastcgi.so -c *.c
3. 安装
$ /usr/local/apache/sbin/apxs -i -a -n fastcgi mod_fastcgi.so

2.2 加入使用 mod_fastcgi 的相关设定
为了让 apache 区分出那一些联机请求属于 fastcgi 来处理的,我们必需在阿帕契的设定档内加入让 apache 可以辨别 fastcgi 的设定。

1. mod_fastcgi 模块会向 apache 登记一个 fastcgi-script 的处理类型 (handler type),我们可以设定所有以 fcg 以及 fpl (for perl) 为扩展名的程序都是符合 fastcgi 协议的应用程序:
addhandler fastcgi-script .fcg .fpl
2. 接下来我们定义 /usr/local/www/fcgi-bin 这个目录用来存放已经写好的 fastcgi 程序:
scriptalias /fcgi-bin/ /usr/local/www/fcgi-bin/
3. 检查设定档文法是否正确:
$ /usr/local/apache/sbin/apachectl configtest
syntax ok
4. 重新激活阿帕契服务器,让新设定生效:
$ /usr/local/apache/sbin/apachectl graceful
/usr/local/apache/bin/apachectl graceful: httpd gracefully restarted

apache 1.3.4 版之后将原本的 httpd.conf 、srm.conf、access.conf 合并成一个档案。所以你所使用的 apache 如果是 1.3.4 版之后,请直接修改 httpd.conf3 这个档,如果是 1.3.3 版之前,我建议把 mod_fastcgi 模块相关设定加在 srm.conf 这个档里头。实际的设定文件路径和设定文件文件名可能依每个人的环境不同而有差异,请根据您自己的环境做适当的调整。

2.3 安装 fastcgi 开发套件

1. 下载 fcgi-devkit-2.1.tar.gz 并且解开:
$ gunzip -c fcgi-devkit-2.1.tar.gz | tar xvf –
2. 编译
$ cd fcgi-devkit-2.1
$ ./configure
$ make
3. 将 c 的标头档 (header file) 及函式库 (library) 安装至系统:
$ cp -rp include /usr/local/include/fastcgi
$ cp libfcgi/libfcgi.a /usr/local/lib

2.4 测试 fastcgi
在 fcgi-devkit 套件中内含一个简单的 fastcgi 范例程序 – echo.c,我们用它来做测试系统是否安装正确。直接把已经编译好的 echo.fcg 复制到预设放置 fastcgi 程序的目录下:

$ cd fcgi-devkit-2.1/example
$ cp echo.fcg /usr/local/www/fcgi-bin

现在赶快用浏览器连到 http://localhost/fcgi-bin/echo.fcg 看看,如果看到以下结果表示您大功告成啦:

fastcgiecho
requestnumber1,processid:1013

nodatafromstandardinput.

requestenvironment:
fcgi_role=responder
document_root=/usr/local/apache/htdocs
http_accept=text/html,text/plain,application/applefile,application/x-metamai
l-patch,sun-deskset-message,mail-file,default,postscript-file,audio-file,
x-sun-attachment,text/enriched,text/richtext,application/andrew-inset,x-be2
,application/postscript,message/external-body,message/partial,application/p
gp,application/pgp,video/mpeg,video/*,image/*,audio/*,audio/mod,text/sgm
l,video/mpeg,image/jpeg,image/tiff,image/x-rgb,image/png,image/x-xbitmap,
image/x-xbm,image/gif,application/postscript,*/*;q=0.01
http_accept_encoding=gzip,compress
http_accept_language=en
http_host=localhost
http_negotiate=trans
http_user_agent=lynx/2.8.1pre.9libwww-fm/2.14
path=/usr/local/bin:/bin:/usr/bin:/usr/x11r6/bin:/usr/sbin:/opt/kde/bin:/home/m
yhsu/bin:/usr/x11r6/bin:/usr/sbin:/opt/kde/bin:/usr/x11r6/bin:/usr/sbin:/opt/kd
e/bin
remote_addr=127.0.0.1
remote_port=1024
script_filename=/usr/local/www/fcgi-bin/echo.fcg
server_admin=myhsu@localhost.localdomain
server_name=localhost.localdomain
server_port=80
server_signature=

apache/1.3.6serveratlocalhost.localdomainport80

server_software=apache/1.3.6(unix)mod_fastcgi/2.2.2
unique_id=n1ptln8aaaeaaapddrkgateway_interface=cgi/1.1
server_protocol=http/1.0request_method=getquery_string=
request_uri=/fcgi-bin/echo.fcgscript_name=/fcgi-bin/echo.fcg

initialenvironment:

请注意在以上程序所显示的 request number 和 process id 这两个变量,当我们继续重新加载这支程序时,request number 会一直累加而 process id 的值都不会改变。这表示这支程序在第一次激活之后就一直执行着没有结束,而且在每次联机请求中所参照到的变量空间是相同的 (所以 request number 会不断加一)。
到此为止,我们已经成功建立起一个可供发展 fastcgi 应用程序的环境及执行 fastcgi 应用程序的网站执行平台。

2.5 安装 fcgi 模块 for perl
如果要使用 perl 来撰写 fastcgi 的程序,必须安装 fcgi.pm 这个模块,安装的方法如下:

1. 下载 fcgi-0.45.tar.gz 并且解开
$ gunzip -c fcgi-0.45.tar.gz | tar xvf –
2. 编译及安装
$ perl makefile.pl
$ make
$ make install
3. 测试
$ cp echo.fpl /usr/local/www/fcgi-bin
$ lynx http://localhost/fcgi-bin/echo.fpl
如果顺利的话,应该会看到如下的结果:
fastcgi echo (perl)
request number 1
no data from standard input.
request environment:

document_root=/usr/local/apache/htdocs
fcgi_role=responder
gateway_interface=cgi/1.1
http_accept=text/html, text/plain, application/applefile, application/x-metamai
l-patch, sun-deskset-message, mail-file, default, postscript-file, audio-file,
x-sun-attachment, text/enriched, text/richtext, application/andrew-inset, x-be2
, application/postscript, message/external-body, message/partial, application/p
gp, application/pgp, video/mpeg, video/*, image/*, audio/*, audio/mod, text/sgm
l, video/mpeg, image/jpeg, image/tiff, image/x-rgb, image/png, image/x-xbitmap,
image/x-xbm, image/gif, application/postscript, */*;q=0.01
http_accept_encoding=gzip, compress
http_accept_language=en
http_host=localhost
http_negotiate=trans
http_user_agent=lynx/2.8.1pre.9 libwww-fm/2.14
path=/usr/local/bin:/bin:/usr/bin:/usr/x11r6/bin:/usr/sbin:/opt/kde/bin:/home/m
yhsu/bin:/usr/x11r6/bin:/usr/sbin:/opt/kde/bin:/usr/x11r6/bin:/usr/sbin:/opt/kd
e/bin
query_string=
remote_addr=127.0.0.1
remote_port=1427
request_method=get
request_uri=/fcgi-bin/echo.fpl
script_filename=/usr/local/www/fcgi-bin/echo.fpl
script_name=/fcgi-bin/echo.fpl
server_admin=myhsu@localhost.localdomain
server_name=localhost.localdomain
server_port=80
server_protocol=http/1.0
server_signature=

apache/1.3.6 server at localhost.localdomain port 80
server_software=apache/1.3.6 (unix) mod_fastcgi/2.2.2
unique_id=n1vibx8aaaeaaaqnkko
more on its way … wait a few seconds
initial environment:

同样的,如果持续连结 http://localhost/fcgi-bin/echo.fpl 可以看到 request number 不断增加,表示 echo.fpl 已经被激活而且持续执行着。

至此,一个可供执行 fastcgi 程序的网站服务器以及撰写 fastcgi 程序的开发环境已经建置完成,接下来就只等着新的程序放上去啰。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 实战 FastCGI(转)二-CGI教程,CGI文档
分享到: 更多 (0)