perl模块官网:http://www.cpan.org/
——————源码安装perl的FCGI模块——————
安装perl cgi模块
wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.74.tar.gz
tar -zxvf FCGI-0.74.tar.gz
cd FCGI-0.74
perl Makefile.PL
make && make install
安装FCGI-ProcManager:
wget http://www.cpan.org/modules/by-module/FCGI/FCGI-ProcManager-0.24.tar.gz
tar -xzxf FCGI-ProcManager-0.24.tar.gz
cd FCGI-ProcManager-0.24
perl Makefile.PL
make && make install
——————使用自带命令安装perl的FCGI模块的方法——————
#perl -MCPAN -e 'install FCGI'
#perl -MCPAN -e 'install FCGI::ProcManager'
——————Configure FastCGI Wrapper #创建perl fastcgi分离器——————
First create the FastCGI wrapper script (credit: Denis S. Filimonov) at /usr/bin/fastcgi-wrapper.pl with the following contents:
File excerpt:/usr/bin/fastcgi-wrapper.pl
#!/usr/bin/perl
use FCGI;
use Socket;
use POSIX qw(setsid);
require 'syscall.ph';
&daemonize;
#this keeps the program alive or something after exec'ing perl scripts
END() { } BEGIN() { }
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; };
eval q{exit};
if ($@) {
exit unless $@ =~ /^fakeexit/;
};
&main;
sub daemonize() {
chdir '/' or die "Can't chdir to /: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
umask 0;
}
sub main {
$socket = FCGI::OpenSocket( "/usr/local/nginx/perl_cgi-dispatch.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
$request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
if ($request) { request_loop()};
FCGI::CloseSocket( $socket );
}
sub request_loop {
while( $request->Accept() >= 0 ) {
#processing any STDIN input from WebServer (for CGI-POST actions)
$stdin_passthrough ='';
$req_len = 0 + $req_params{'CONTENT_LENGTH'};
if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){
my $bytes_read = 0;
while ($bytes_read < $req_len) {
my $data = '';
my $bytes = read(STDIN, $data, ($req_len - $bytes_read));
last if ($bytes == 0 || !defined($bytes));
$stdin_passthrough .= $data;
$bytes_read += $bytes;
}
}
#running the cgi app
if ( (-x $req_params{SCRIPT_FILENAME}) && #can I execute this?
(-s $req_params{SCRIPT_FILENAME}) && #Is this file empty?
(-r $req_params{SCRIPT_FILENAME}) #can I read this file?
){
pipe(CHILD_RD, PARENT_WR);
my $pid = open(KID_TO_READ, "-|");
unless(defined($pid)) {
print("Content-type: text/plain\r\n\r\n");
print "Error: CGI app returned no output - ";
print "Executing $req_params{SCRIPT_FILENAME} failed !\n";
next;
}
if ($pid > 0) {
close(CHILD_RD);
print PARENT_WR $stdin_passthrough;
close(PARENT_WR);
while(my $s = <KID_TO_READ>) { print $s; }
close KID_TO_READ;
waitpid($pid, 0);
} else {
foreach $key ( keys %req_params){
$ENV{$key} = $req_params{$key};
}
# cd to the script's local directory
if ($req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/]+$/) {
chdir $1;
}
close(PARENT_WR);
close(STDIN);
#fcntl(CHILD_RD, F_DUPFD, 0);
syscall(&SYS_dup2, fileno(CHILD_RD), 0);
#open(STDIN, "<&CHILD_RD");
exec($req_params{SCRIPT_FILENAME});
die("exec failed");
}
}
else {
print("Content-type: text/plain\r\n\r\n");
print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not ";
print "exist or is not executable by this process.\n";
}
}
}
Then create an init script to control the FastCGI process that matches the one shown below:
——————创建perl-fastcgi分离器控制及启动脚本——————
File excerpt:/etc/rc.d/init.d/perl-fastcgi
#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /opt/nginx/conf/nginx.conf
# pidfile: /opt/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
perlfastcgi="/usr/bin/fastcgi-wrapper.pl"
prog=$(basename perl)
lockfile=/var/lock/subsys/perl-fastcgi
start() {
[ -x $perlfastcgi ] || exit 5
echo -n $"Starting $prog: "
daemon $perlfastcgi
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
echo -n $”Reloading $prog: ”
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
Next issue the following commands to make the scripts executable and set the perl-fastcgi process to start on boot:
chmod +x /usr/bin/fastcgi-wrapper.pl
chmod +x /etc/rc.d/init.d/perl-fastcgi
/etc/rc.d/init.d/perl-fastcgi start
chkconfig --add perl-fastcgi
chkconfig perl-fastcgi on
2,获取fastcgi-wrapper.pl(直接获得第三方分离器)
wget http://www.ruby-forum.com/attachment/1583/fastcgi-wrapper.pl
chmod +x fastcgi-wrapper.pl
mv fastcgi-wrapper.pl /usr/local/bin/
如果有必要修改下
fastcgi_pass unix:/var/run/nginx/perl_cgi-dispatch.sock #次目录要切实存在
这一行的路径,确认你修改的路径存在。启动
/usr/local/bin/fastcgi-wrapper.pl &
查看进程是否启动
ps aux|grep fcgi-wrapper.pl
它会建立/var/run/nginx/perl_cgi-dispatch.sock
在Nginx的配置文件上要用到
3,建立perl-cgi.conf文件,大致如下
location ~ .*\.(pl|cgi)?$$ {
gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
fastcgi_pass unix:/var/run/nginx/perl_cgi-dispatch.sock;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
}
4,修改nginx.conf文件
在server内添加 include perl_fcgi.conf;
增加:
}
location ~ .*\.(pl|cgi)?$ {
fastcgi_pass unix:/usr/local/nginx/perl_cgi-dispatch.sock;
fastcgi_index index.cgi;
include fcgi.conf;
}
配置完成,重新启动nginx
nginx -s reload
5,测试 test.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><body>Hello, world.</body></html>";
chmod +x test.cgi
参考:
nginx :Simple CGI
https://bbs.zephyrxt.com/thread-8928-1-1.html
nginx perl cgi配置
https://bbs.zephyrxt.com/thread-8927-1-1.html
总结了一下让nginx支持perl cgi的步骤
https://bbs.zephyrxt.com/thread-8926-1-1.html
Nginx and Perl-FastCGI on CentOS 5
https://bbs.zephyrxt.com/thread-8925-1-1.html