Apache + Perl + FastCGI安装于配置

1. 安装apache

将apache源代码解压缩:

一、编译和安装apache:

# tar zxvf httpd-2.2.11.tar.gz

# cd httpd-2.2.11

# ./configure

# make

# make install

默认安装在/usr/local/apache2中。

安装fcgi

将fcgi解压缩:

# tar zxvf fcgi-2.4.0.tar.gz

编译和安装:

# cd fcgi-2.4.0

# ./configure

# make

# make install

将必要的动态库拷贝到/usr/lib中(cgi程序将依赖这些库文件):

# cp ./libfcgi/.libs/* /usr/lib/

二、安装mod_fastcgi

将mod_fastcgi-2.4.6.tar.gz解压缩:

# tar zxvf mod_fastcgi-2.4.6.tar.gz

编译和安装:

# cp Makefile.AP2 Makefile

# make

# make install

三、配置apache

a) 打开/usr/local/apache2/conf/httpd.conf文件

b) 添加一行用来加载fastcgi模块:

LoadModule fastcgi_module modules/mod_fastcgi.so

c) 添加一行将cgi目录设置为/data/www/opensource/cgi-bin:

scriptAlias /cgi-bin/ "/data/www/opensource/cgi-bin/"

d) 将

<Directory "/usr/local/apache2/cgi-bin">

...

...

</Directory>

修改成:

<Directory "/data/www/www/cgi-bin">

AllowOverride all

Options all

Order allow,deny

Allow from all

</Directory>

e) 添加一行用于让apache用fastcgi模块解析.cgi后缀的文件:

AddHandler fastcgi-script .cgi .fpl

四、重启apache

/usr/local/apache2/bin/apachectl -k restart

PERL的FCGI模块:

1、首先我们安装FastCGI在Perl下的模块。最新版本在http://www.fastcgi.com/

里下载。

最新版本:FCGI-0.56.tar.gz

2、下载 FCGI-0.45.tar.gz 并且解开

$ gunzip -c FCGI-0.56.tar.gz | tar xvf -

3、编译及安装

$ perl Makefile.PL

$ make

$ make install

4、测试

$ cp echo.fpl {你www里Fastcgi所在目录}

$ lynx {你www里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, aud

io-file,

x-sun-attachment, text/enriched, text/richtext, application/andrew-ins

et, x-be2

, application/postscript, message/external-body, message/partial, appl

ication/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/bi

n:/home/m

yhsu/bin:/usr/X11R6/bin:/usr/sbin:/opt/kde/bin:/usr/X11R6/bin:/usr/sbi

n:/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:

最后,给大家一个fastcgi编程的例子:

#!/usr/local/bin/perl

use CGI::Fast;

my $counter = 0;

while (my $cgi = new CGI::Fast) {

print("Content-type: text/html\n\n");

print("We have served $counter requests");

$counter++;

}

附:一个留言版:

#!/usr/bin/perl -w

use CGI::Fast qw(:standard);

my $data_file="data.txt";

while (my $cgi=new CGI::Fast){

print "Content-Type:text/html\n\n";

#my ($rw);

#$rw=$cgi->param("rw");

#if ($rw eq "r" || $rw eq ""){

print qq~

<form name="" action="rr.fpl">

<p>name:

<input type="text" name="name">

</p>

<p>message:

<textarea name="content" wrap="VIRTUAL"></textarea>

</p>

<p>

<input type="submit" value="ok">

<input type="reset" valie="clean">

<input type="hidden" name="rw" value="w">

</p>

</form>

~;

my @line=&Read_Data;

foreach (@line){

my ($name,$content)=split(/&&/,$_);

print qq~

name:$name

<br>

message:$content

<hr>

~;

}

#}else{

my $name=$cgi->param("name");

my $content=$cgi->param("content");

#local $ok=1;

if ($name eq "" || $content eq ""){

&Print_Erro("please input all message!");

}else{

#if ($ok){

&Write_Data($name,$content);

&Print_Succ("me sucessfull");

#}

}

}

sub Read_Data {

open (FILE,$data_file);

my @line=<FILE>;

close (FILE);

return @line;

}

sub Write_Data {

my ($name,$content)=@_;

open (FILE,">>$data_file");

print FILE "$name&&$content\n";

close (FILE);

}

sub Print_Succ {

print qq href="java script:history.go(-1);">look at</a>~;

}

sub Print_Erro {

$ok=0;

print qq href="java script:history.go(-1);">go back</a>~;

}