基于CentOS搭建nginx+perl的perl平台

基于CentOS搭建nginx+perl的perl平台
1.nginx安装

./configure \
     "--user=www" \
     "--group=www" \
     "--prefix=/usr/local/nginx/" \
     "--with-http_stub_status_module" \
     "--with-http_ssl_module" \
     "--with-md5=/usr/lib" \
     "--with-sha1=/usr/lib"
make
make install

2.perl安装

#yum install perl*
#perl -MCPAN -e 'install FCGI'
#perl -MCPAN -e 'install FCGI::ProcManager'

3.fcgi脚本配置
3.1.新建脚本:

#vi /usr/local/bin/cgiwrap-fcgi.pl

写入:

#!/usr/bin/perl -w 
use FCGI; 
use Socket; 
use FCGI::ProcManager; 
sub shutdown { FCGI::CloseSocket($socket); exit; } 
sub restart  { FCGI::CloseSocket($socket); &main; } 
use sigtrap 'handler', \&shutdown, 'normal-signals'; 
use sigtrap 'handler', \&restart,  'HUP'; 
require 'syscall.ph'; 
use POSIX qw(setsid); 

#&daemonize; we don't daemonize when running under runsv 
#this keeps the program alive or something after exec'ing perl scripts 
END()   { } 
BEGIN() { } 
{ 
    no warnings; 
    *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 { 
$proc_manager = FCGI::ProcManager->new( {n_processes => 32} );
$socket = FCGI::OpenSocket( "127.0.0.1:9002", 10 ); #use IP sockets 
#$socket = FCGI::OpenSocket( "/var/run/nginx/perl_cgi-dispatch.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folder!! 
#foreach $item (keys %ENV) { delete $ENV{$item}; } 
    #$proc_manager = FCGI::ProcManager->new( {n_processes => 5} ); 
    #$socket = FCGI::OpenSocket( "/opt/nginx/fcgi/cgi.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, 
        &FCGI::FAIL_ACCEPT_ON_INTR ); 
    $proc_manager->pm_manage(); 
    if ($request) { request_loop() } 
    FCGI::CloseSocket($socket); 
} 

sub request_loop { 
    while ( $request->Accept() >= 0 ) { 
        $proc_manager->pm_pre_dispatch(); 

        #processing any STDIN input from WebServer (for CGI-POST actions) 
        $stdin_passthrough = ''; 
        { no warnings; $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 ); 
            pipe( PARENT_ERR, CHILD_ERR ); 
            my $pid = open( CHILD_O, "-|" ); 
            unless ( defined($pid) ) { 
                print("Content-type: text/plain\r\n\r\n"); 
                print 
"Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n"; 
                next; 
            } 
            $oldfh = select(PARENT_ERR); 
            $|     = 1; 
            select(CHILD_O); 
            $| = 1; 
            select($oldfh); 
            if ( $pid > 0 ) { 
                close(CHILD_RD); 
                close(CHILD_ERR); 
                print PARENT_WR $stdin_passthrough; 
                close(PARENT_WR); 
                $rin = $rout = $ein = $eout = ''; 
                vec( $rin, fileno(CHILD_O),    1 ) = 1; 
                vec( $rin, fileno(PARENT_ERR), 1 ) = 1; 
                $ein    = $rin; 
                $nfound = 0; 

                while ( $nfound = 
                    select( $rout = $rin, undef, $ein = $eout, 10 ) ) 
                { 
                    die "$!" unless $nfound != -1; 
                    $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1; 
                    $r2 = vec( $rout, fileno(CHILD_O),    1 ) == 1; 
                    $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1; 
                    $e2 = vec( $eout, fileno(CHILD_O),    1 ) == 1; 

                    if ($r1) { 
                        while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) { 
                            print STDERR $errbytes; 
                        } 
                        if ($!) { 
                            $err = $!; 
                            die $!; 
                            vec( $rin, fileno(PARENT_ERR), 1 ) = 0 
                                unless ( $err == EINTR or $err == EAGAIN ); 
                        } 
                    } 
                    if ($r2) { 
                        while ( $bytes = read( CHILD_O, $s, 4096 ) ) { 
                            print $s; 
                        } 
                        if ( !defined($bytes) ) { 
                            $err = $!; 
                            die $!; 
                            vec( $rin, fileno(CHILD_O), 1 ) = 0 
                                unless ( $err == EINTR or $err == EAGAIN ); 
                        } 
                    } 
                    last if ( $e1 || $e2 ); 
                } 
                close CHILD_RD; 
                close PARENT_ERR; 
                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(PARENT_ERR); 
                close(STDIN); 
                close(STDERR); 

                #fcntl(CHILD_RD, F_DUPFD, 0); 
                syscall( &SYS_dup2, fileno(CHILD_RD),  0 ); 
                syscall( &SYS_dup2, fileno(CHILD_ERR), 2 ); 

                #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 exist or is not executable by this process.\n"; 
        } 
    } 
}

给予执行权限:

#chmod +x /usr/local/bin/cgiwrap-fcgi.pl

3.2.启动管理脚本

#vi /usr/local/bin/perl-fcgi

写入代码:

#!/bin/sh
#perl-fcgi start shell
#by hamgua(哈密瓜)
#bbs:http://bbs.linuxtone.org
perl_start() {
  /usr/local/bin/cgiwrap-fcgi.pl > /dev/null 2>&1 & 
}

perl_stop() {
  for i in ` ps aux|grep perl|grep -v grep|awk '{print $2}'`; do kill -9 $i; done
}
perl_restart() {
  for i in ` ps aux|grep perl|grep -v grep|awk '{print $2}'`; do kill -9 $i; done > /dev/null 2>&1
  /usr/local/bin/cgiwrap-fcgi.pl > /dev/null 2>&1 &
}

case "$1" in
  start)
        echo -n "Starting perl-fcgi: "
        perl_start
        ps aux|grep perl-fcgi-pm|grep -v "grep"|awk '{print $2}' > /dev/null 2>&1
  if [ "$?" != 0 ] ; then
   echo " failed"
   exit 1
  else
          echo " ok"
  fi
        ;;
  stop)
        echo -n "Stopping perl-fcgi: "
        perl_stop
        ps aux|grep perl-fcgi-pm|grep -v "grep"|awk '{print $2}' > /dev/null 2>&1
  if [ "$?" != 0 ] ; then
   echo " ok"
   exit 1
  else
          echo " failed"
  fi
        ;;
  restart)
        echo -n "Restarting perl-fcgi:"
        perl_stop
        sleep 1
        perl_start
        echo " ok"
        ;;
  *)
          echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
          exit 3
        ;;
esac

exit 0

给予执行权限:

#chmod u+x /usr/local/bin/perl-fcgi

4.nginx配置

server
       {
               listen       80;
               server_name  perl.linuxtone.org;
               index index.html index.pl index.perl index.cgi;
               root /data/www/wwwroot/perl;
        
               location ~ .*\.(pl|perl|cgi)?$
               {
                       fastcgi_pass 127.0.0.1:9002;
                       fastcgi_index index.cgi;
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                       include   fastcgi_params;
               } 
     }

最后重启nginx

/etc/init.d/nginx restart

收工

点赞
  1. 老妖说道:
    Internet Explorer Windows 7
    真能折腾啊,呵呵
  2. www.qipaip.com说道:
    Google Chrome Windows 7
    很好,很有意思56