debian下配置apache2以支持perl_cgi
2011-02-26 09:59:31 阿炯

简介:这是在debian下为apache2配置fcgi的详细页面,介绍了和perl cgi有关的知识、技巧、经验。

CGI英文全称是 Common Gateway Interface,通常翻译为共同网关接口,是HTTP服务器与机器上的其他程序进行通信的一个接口。这个“其他程序”可以使用任何计算机语言来编写,它通过CGI这个接口从HTTP服务器取得输入,然后把运行的结果又通过CGI这个接口交给HTTP服务器,而HTTP服务器把这个结果送给浏览器。

perl 是一种解释形的计算机语言,具有强大且方便的文本处理能力,被誉为计算机的“瑞士军刀”,它被除了Windows之外的几乎所有的其他服务器操作系统所默认安装,BSD UINX 上有些系统命令就是用perl编写。因为HTTP动态页面主要就是文本的处理所以perl也就非常的适合来编写这个“其他程序”。这也就造成了一个很好笑的事实,上 google搜索CGI,出来的国内网站基本都是在谈perl,在很多人眼里,CGI成了一种语言,成了perl的代名词。

CGI的出现让Web从静态变为为动态,随着Web的越来越普及,很多的网站的都需要有动态的页面,以便与浏览者互交,CGI方式的缺点也越来越突出。因为HTTP要生成一个动态页面,系统就必须启动一个新的进程以运行CGI程序,不断地fork是一项很消耗时间和资源的工作。如果能够让HTTP服务器本身就支持一种语言,用这个语言来编写动态页面的话,这就至少不需要fork。因此就出现一种叫做动态网页设计语言的东西,如php、asp、jsp。这些不是真正的语言,他们都只是专门用于web的。

mod_perl简单点说就是类似php的方式(运行于apache的地址空间),让apache http服务器本身能够支持perl语言,除了能够不需要每一个请求就启动perl解释器来解释之外,还能够让你可以用perl来写apache 的模块。可以让你自己的程序成为http服务器本身的功能,以及用perl语言来设置httpd.conf等,详细情况请见http://perl.apache.org/

至于fastcgi,那又是另外的一种方式,他让这个CGI程序,以类似系统的守护进程那样的运行(独立于web服务器),而不是每次请求都是启动一个新的进程,从理论上讲,这是目前最为有效的提高效率的方式。而且和CGI一样,不局限于用什么语言来写,详细资料:http://www.fastcgi.com/。传统的cgi模式会造成了服务器负荷很重且效率低下,为解决这个问题,其中比较有名的是mod_perl和fastcgi(以下简称fcgi,而这种方案中有两种可实现的软件包:libapache2-mod-fastcgi与libapache2-mod-fcgid。)。

FastCGI的益处
1)可以支持在一个体系上运行多版本的解释器,如PHP4、PHP5。

2)只要安置一个apache 的module后,就可同时撑持PHP、Python、Perl等,没有需要为它们安置各自的module。

3)得到更好的权限控制,比PHP运行在安全模式更安全,一些虚拟主机供给商如DreamHost、BlueHost、Godaddy等都是接纳mod-fcgid或mod_fastcgi来实现对PHP的撑持。

这里可以选用的mod_fcgid,它是基于GPL协议的,而mod_fastcgi是'non-free'的,mod-fcgid与apache2共同工作得更好。

hto@pdb:~$ apt-cache show libapache2-mod-fastcgi
Package: libapache2-mod-fastcgi
Priority: optional
Section: non-free/web
Installed-Size: 272
Maintainer: Tatsuki Sugiura
Architecture: i386
Source: libapache-mod-fastcgi
Version: 2.4.6-1
Depends: apache2.2-common (>= 2.2.4), libc6 (>= 2.7-1)
Filename: pool/non-free/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.6-1_i386.deb
Size: 66232
MD5sum: b4a9215c2ed30401fb913c493281780b
SHA1: 25f4faf95b7999dce8e5143eb43cf5073cdb61d4
SHA256: e01b199b778a4b9148ae4c16ed9eeafb7b0f8a655ec1b8f4a69e6c9924a84984
Description: Apache 2 FastCGI module for long-running CGI scripts
This is a FastCGI module for the Apache 2.x web server. FastCGI is an
open standard for communicating between a web server and a long-running
web application. CGI scripts supporting FastCGI can be started as a
daemon and process multiple requests without restarting.  This is
particularly useful for scripts written in languages like Perl, as it
saves the cost of recompiling the scripts and its loaded modules with
each invocation.
Tag: role::shared-lib, suite::apache, web::cgi

hto@pdb:~$ apt-cache show libapache2-mod-fcgid
Package: libapache2-mod-fcgid
Priority: optional
Section: httpd
Installed-Size: 316
Maintainer: Tatsuki Sugiura
Architecture: i386
Version: 1:2.3.6-1~bpo50+1
Depends: libc6 (>= 2.7-1), apache2.2-common
Filename: pool/main/liba/libapache2-mod-fcgid/libapache2-mod-fcgid_2.3.6-1~bpo50+1_i386.deb
Size: 71668
MD5sum: 91707f8bce0320e5bbeea203db70f116
SHA1: ec7c92eddf978afc11fcf1f112ba6b5d9e82ff9d
SHA256: 3ae9d105cb2446e0efd6d0c807041970c94a783f23a17557613f7493c96abf49
Description: an alternative module compat with mod_fastcgi
mod_fcgid is a high performance alternative to mod_cgi or mod_cgid,
which starts a sufficient number instances of the CGI program to handle
concurrent requests, and these programs remain running to handle further
incoming requests. It is favored by the PHP developers, for example,
as a preferred alternative to running mod_php in-process, delivering
very similar performance.
.
It is a binary compatibility alternative to Apache module mod_fastcgi.
Homepage: http://httpd.apache.org/mod_fcgid/

Package: libapache2-mod-fcgid
Priority: optional
Section: net
Installed-Size: 196
Maintainer: Tatsuki Sugiura
Architecture: i386
Version: 1:2.2-1+lenny1
Depends: libc6 (>= 2.7-1), apache2.2-common
Filename: pool/main/liba/libapache2-mod-fcgid/libapache2-mod-fcgid_2.2-1+lenny1_i386.deb
Size: 42624
MD5sum: 0c795cf33563e6a7a3bfdfceb4848074
SHA1: ddf279312b84d9d447a6d8b1bbdb9ded0a737655
SHA256: 4d2c432b04e4568c7309a561cbd2a10554c000bf633e6da13847351b5c09478e
Description: an alternative module compat with mod_fastcgi
It is a binary compatibility alternative to Apache module mod_fastcgi.
mod_fcgid has a new process management strategy, which concentrates on
reducing the number of fastcgi server, and kick out the corrupt fastcgi
server as soon as possible.
Tag: devel::library, role::plugin, suite::apache, web::cgi

本文通过实例介绍如何在debian采用apache2与perl进行环境搭建。

在debian下为apache2构建一个fcgi的过程其实很简单。如果安装有'mod_perl'模块时,可以从系统中删除软件包'libapache2-mod-perl2',如果已安装了该软件包,可以通过命令查看:
# !apache2ctl
apache2ctl -D DUMP_MODULES  
Loaded Modules:

perl_module (shared)

Syntax OK

如果发现有一行,则需要卸载其。

在执行解析fcgi脚本时,需要安装'libcgi-fast-perl'软件包。

一)、fastcgi
# apt-get install apache2 apache2-doc apache2-utils
# apt-get install libapache2-mods-fastcgi libfcgi-perl
就可以安装好fcgi了。

配置fcgi
# vi /etc/apache2/mods-available/fastcgi.conf
修改后内容如下:

AddHandler fastcgi-script .fcgi .pl .psp
FastCgiIpcDir /var/lib/apache2/fastcgi


开启fastcgi模块
# a2enmod fastcgi

编写fastcgi脚本:
#!/usr/bin/perl
use strict;
use CGI::Fast qw(:standard);

my $counter = 0;
my $title = "Fast CGI counter";
while (new CGI::Fast) {
print header;
print start_html $title;
print h1 $title;
print "Invocation number: ", b($counter++), ", PID: ", b($$), ".", hr;
print end_html;
}

修改httpd.conf文件如下:
# cd /etc/apache2
# vi httpd.conf
在sites-enabled下的下建立站点并加入以下的内容:

Options Indexes FollowSymLinks MultiViews +ExecCGI
AllowOverride None
Order allow,deny
allow from all


二)、fcgid
安置mod-fcgid

apt-get install libapache2-mod-fcgid

修改相关参数
/etc/apache2/mods-enabled/fcgid.conf

AddHandler fcgid-script .fcgi .pl

可将/etc/apache2/mods-enabled/fcgid.conf改为

AddHandler fcgid-script .php .py .pl .fcgi

以增添对php py pl等扩展名的撑持。

这里列举其它一些不太重要参数,如:
IdleTimeout 600
ProcessLifeTime 3600
MaxProcessCount 8
DefaultMinClassProcessCount 3
DefaultMaxClassProcessCount 3
IPCConnectTimeout 8
IPCCommTimeout 48

环境基本搭建好了,可以重新起动apache2。
# apache2ctl -k restart
# cd /var/www/fcgi
# vi test.fpl

新建立一个实验文件看看能否运行,文件内容如下:
#!/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{usr1} = &sig_handler;
$sig{term} = &sig_handler;
$sig{pipe} = sub {die 'sigpipe ';};
while ($handling_request = ($request->accept() >= 0)) {
eval {&abort_request;} if (!eval {&do_request; 1;} && $@ ne 'sigpipe ');
$handling_request = 0;
last if $exit_requested;}
$request->finish();
exit(0);
sub do_request() {
print("content-type: text/html ", ++$count);
print("hello world! ");
$request->finish();
}
sub abort_request() {
$exit_requested = 1; # assume the error isn't recoverable
print stderr "fatal error, request aborted, shutting down: $@ ";
$request->finish();
}

在终端下改变test.fpl的文件属性
# chmod 755 test.fpl

好了,打开你的浏览器,在地址栏中输入:
http://localhost/fcgi-bin/test.fpl

你如果看到:
hello world!

说明你的fcgi环境搭建成功。