在debian下为apache2配置fcgi

本文介绍了如何在Debian系统上为Apache2配置FastCGI,以提高Perl CGI应用的性能。通过安装必要的软件包,配置Apache2的fastcgi.conf和httpd.conf文件,以及创建和测试fcgi工作目录下的Perl脚本,最终实现了一个简单的FastCGI环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

perl作为一 种解释性语言,其在CGI类WEB开发中具有独到的优势。但CGI本身令人诟病的性能让人却步。因为cgi的工作过程是当客户端发出请求时,由服务器为其 打开一个httpd过程为其服务。这造成了服务器负荷很重‘而效率低下。为解决这个问题,分别有几种方案。其中比较有名的是mod_perl和 FastCGI(以下简称FCGI)。
在DEBIAN下为apache2构建一个fcgi的过程其实很简单。
#>apt-get install libapache2-mods-fastcgi,libfcgi-perl
就可以安装好fcgi了。
配置fcgi
#>vi /etc/apache2/mods-available/fastcgi.conf
修改后内容如下:
<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi .fpl
  #FastCgiWrapper /usr/lib/apache2/suexec2
  FastCgiIpcDir /var/lib/apache2/fastcgi
</IfModule>
创建一个到mod-enabled的联结,以便apache2起动时能加载fcgi
#>ln -s /etc/apache2/mods-available/fastcgi.conf /etc/apache2/mods-enabled
为fcgi工作搭建一个窝:
#>cd /home/myname
#>mkdir ./fcgi
#>chmod 755/home/myname/fcgi

修改httpd.conf文件如下:
#>cd /etc/apache2
#>vi httpd.conf
在httpd.conf中加入以下的内容:
ScriptAlias /fcgi-bin/ /home/myname/fcgi/
<Location /fcgi/>
        SetHandler fastcgi-script
        Options +ExecCGI
</Location>
好了,环境基本搭建好了,重新起动APACHE2
#>apache2 -k restart
#>cd /home/myname/fcgi
#>vi test.fpl
用vi建立一个实验文件看看能否运行。文件内容如下:
#!/usr/bin/perl -w
use FCGI;
use strict;
my $count = 0;
my $handling_request = 0;
my $exit_requested = 0;
my $request = FCGI::Request();
sub sig_handler {
       $exit_requested = 1;
        exit(0) if !$handling_request;}
$SIG = /&sig_handler;
$SIG = /&sig_handler;
$SIG = sub {die 'SIGPIPE/n';};
while ($handling_request = ($request->Accept() >= 0)) {
        eval if (!eval {&do_request; 1;} && $@ ne 'SIGPIPE/n');
        $handling_request = 0;
         last if $exit_requested;}
$request->Finish();
exit(0);
sub do_request() {
      print("Content-type: text/html/r/n/r/n", ++$count);
      print("hello world!/n");
      $request->Finish();
       }
sub abort_request() {
     $exit_requested = 1; # assume the error isn't recoverable
     print STDERR "fatal error, request aborted, shutting down: $@/n";
     $request->Finish();
      }

在终端下改变test.fpl的文件属性
#>chmod 755 test.fpl
好了,打开你的浏览器,在地址栏中输入:
http://localhost/fcgi-bin/test.fpl
你如果看到:
1hello world!
刷新页面,前面的数字在不断增加,祝贺你,你的fcgi环境搭建成功。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值