nginx和perl可以通过apt-get来安装:
perl的fcgi配置:
cpan
(出现配置对话,选择no,手动输入源)
http://mirrors.sohu.com/CPAN/
http://mirrors.163.com/cpan/
//Can't locate FCGI.pm in @INC
perl -MCPAN -e 'install FCGI'
perl -MCPAN -e 'install FCGI::ProcManager'
cpan -i FCGI
cpan -i FCGI::ProcManager
//Can't locate IO/All.pm
perl -MCPAN -e 'install "IO::All"'
//身份认证、数据库连接
cpan Authen::SASL Filesys::Df DBI DBD::mysql
perl-fcgi.pl(fcgi socket):


1 #!/usr/bin/perl 2 # 3 # author Daniel Dominik Rudnicki 4 # thanks to: Piotr Romanczuk 5 # email daniel@sardzent.org 6 # version 0.4.3 7 # webpage http://www.nginx.eu/ 8 # 9 # BASED @ http://wiki.codemongers.com/NginxSimpleCGI 10 # 11 # 12 # use strict; 13 use FCGI; 14 use Getopt::Long; 15 use IO::All; 16 use Socket; 17 18 sub init { 19 GetOptions( "h" => \$help, 20 "verbose!"=>\$verbose, 21 "pid=s" => \$filepid, 22 "l=s" => \$logfile, 23 "S:s" => \$unixsocket, 24 "P:i" => \$unixport) or usage(); 25 usage() if $help; 26 27 print " Starting Nginx-fcgi\n" if $verbose; 28 print " Running with $> UID" if $verbose; 29 print " Perl $]" if $verbose; 30 31 if ( $> == "0" ) { 32 print "\n\tERROR\tRunning as a root!\n"; 33 print "\tSuggested not to do so !!!\n\n"; 34 exit 1; 35 } 36 37 if ( ! $logfile ) { 38 print "\n\tERROR\t log file must declared\n" 39 . "\tuse $0 with option -l filename\n\n"; 40 exit 1; 41 } 42 print " Using log file $logfile\n" if $verbose; 43 "\n\n" >> io($logfile); 44 addlog($logfile, "Starting Nginx-cfgi"); 45 addlog($logfile, "Running with $> UID"); 46 addlog($logfile, "Perl $]"); 47 addlog($logfile, "Testing socket options"); 48 49 if ( ($unixsocket && $unixport) || (!($unixsocket) && !($unixport)) ) { 50 print "\n\tERROR\tOnly one option can be used!\n"; 51 print "\tSuggested (beacuse of speed) is usage UNIX socket -S \n\n"; 52 exit 1; 53 } 54 55 if ($unixsocket) { 56 print " Daemon listening at UNIX socket $unixsocket\n" if $versbose; 57 addlog($logfile, "Deamon listening at UNIX socket $unixsocket"); 58 } else { 59 print " Daemon listening at TCP/IP socket *:$unixport\n" if $verbose; 60 # 61 addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport"); 62 } 63 64 if ( -e $filepid ) { 65 print "\n\tERROR\t PID file $filepid already exists\n\n"; 66 addlog($logfile, "Can not use PID file $filepid, already exists."); 67 exit 1; 68 } 69 70 if ( $unixsocket ) { 71 print " Creating UNIX socket\n" if $verbose; 72 $socket = FCGI::OpenSocket( $unixsocket, 10 ); 73 if ( !$socket) { 74 print " Couldn't create socket\n"; 75 addlog($logfile, "Couldn't create socket"); 76 exit 1; 77 } 78 print " Using UNIX socket $unixsocket\n" if $verbose; 79 } else { 80 print " Creating TCP/IP socket\n" if $verbose; 81 $portnumber = ":".$unixport; 82 $socket = FCGI::OpenSocket( $unixport, 10 ); 83 if ( !$socket ) { 84 print " Couldn't create socket\n"; 85 addlog($logfile, "Couldn't create socket"); 86 exit 1; 87 } 88 print " Using port $unixport\n" if $verbose; 89 } 90 addlog($logfile, "Socket created"); 91 92 if ( ! $filepid ) { 93 print "\n\tERROR\t PID file must declared\n" 94 . "\tuse $0 with option -pid filename\n\n"; 95 exit 1; 96 } 97 print " Using PID file $filepid\n" if $verbose; 98 addlog($logfile, "Using PID file $filepid"); 99 100 my $pidnumber = $$; 101 $pidnumber > io($filepid); 102 print " PID number $$\n" if $verbose; 103 addlog($logfile, "PID number $pidnumber"); 104 105 } 106 107 sub addzero { 108 my ($date) = shift; 109 if ($date < 10) { 110 return "0$date"; 111 } 112 return $date; 113 } 114 115 sub logformat { 116 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time); 117 my $datestring; 118 $year += 1900; 119 $mon++; 120 $mon = addzero($mon); 121 $mday = addzero($mday); 122 $min = addzero($min); 123 $datestring = "$year-$mon-$mday $hour:$min"; 124 return($datestring); 125 } 126 127 sub addlog { 128 my ($log_file, $log_message) = @_; 129 my $curr_time = logformat(); 130 my $write_message = "[$curr_time] $log_message"; 131 $write_message >> io($log_file); 132 "\n" >> io($log_file); 133 } 134 135 sub printerror { 136 my $message = @_; 137 print "\n Nginx FastCGI\tERROR\n" 138 . "\t $message\n\n"; 139 exit 1; 140 } 141 142 sub usage { 143 print "\n Nginx FastCGI \n" 144 . "\n\tusage: $0 [-h] -S string -P int\n" 145 . "\n\t-h\t\t: this (help) message" 146 . "\n\t-S path\t\t: path for UNIX socket" 147 . "\n\t-P port\t\t: port number" 148 . "\n\t-p file\t\t: path for pid file" 149 . "\n\t-l file\t\t: path for logfile" 150 . "\n\n\texample: $0 -S /var/run/nginx-perl_cgi.sock -l /var/log/nginx/nginx-cfgi.log -pid /var/run/nginx-fcgi.pid\n\n"; 151 exit 1; 152 } 153 154 155 init; 156 # 157 END() { } BEGIN() { } 158 *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit}; 159 if ($@) { 160 exit unless $@ =~ /^fakeexit/; 161 } ; 162 163 # fork part 164 my $pid = fork(); 165 166 if( $pid == 0 ) { 167 &main; 168 exit 0; 169 } 170 171 print " Forking worker process with PID $pid\n" if $verbose; 172 addlog($logfile, "Forking worker process with PID $pid"); 173 print " Update PID file $filepid\n" if $verbose; 174 addlog($logfile, "Update PID file $filepid"); 175 $pid > io($filepid); 176 print " Worker process running.\n" if $verbose; 177 addlog ($logfile, "Parent process $$ is exiting"); 178 exit 0; 179 180 sub main { 181 $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket ); 182 if ($request) { request_loop()}; 183 FCGI::CloseSocket( $socket ); 184 } 185 186 sub request_loop { 187 while( $request->Accept() >= 0 ) { 188 # processing any STDIN input from WebServer (for CGI-POST actions) 189 $stdin_passthrough = ''; 190 $req_len = 0 + $req_params{'CONTENT_LENGTH'}; 191 if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){ 192 while ($req_len) { 193 $stdin_passthrough .= getc(STDIN); 194 $req_len--; 195 } 196 } 197 198 # running the cgi app 199 if ( (-x $req_params{SCRIPT_FILENAME}) && 200 (-s $req_params{SCRIPT_FILENAME}) && 201 (-r $req_params{SCRIPT_FILENAME}) 202 ){ 203 foreach $key ( keys %req_params){ 204 $ENV{$key} = $req_params{$key}; 205 } 206 if ( $verbose ) { 207 addlog($logfile, "running $req_params{SCRIPT_FILENAME}"); 208 } 209 # http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens 210 # 211 open $cgi_app, '-|', $req_params{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n"; # addlog($logfile, "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !"); 212 213 if ($cgi_app) { 214 print <$cgi_app>; 215 close $cgi_app; 216 } 217 } else { 218 print("Content-type: text/plain\r\n\r\n"); 219 print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n"; 220 addlog($logfile, "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process."); 221 } 222 } 223 }
start-perl-fcgi.sh(启动控制脚本):


1 #!/bin/bash 2 #set -x 3 if [[ $# != 1 ]];then 4 echo "Usage $0 start|stop|restart" 5 exit 1 6 fi 7 dir=/home/perl 8 bindir=/usr/local/bin 9 stop () 10 { 11 #pkill -f $dir/perl-fcgi.pl 12 kill $(cat $dir/perl-fcgi.pid) 13 rm $dir/perl-fcgi.pid 2>/dev/null 14 rm $dir/perl-fcgi.sock 2>/dev/null 15 echo "stop perl-fcgi done" 16 } 17 18 start () 19 { 20 rm $dir/now_start_perl_fcgi.sh 2>/dev/null 21 22 chown www-data.www-data $dir 23 echo "$dir/perl-fcgi.pl -l $dir/perl-fcgi.log -pid $dir/perl-fcgi.pid -S $dir/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh 24 25 chown www-data.www-data $dir/now_start_perl_fcgi.sh 26 chmod u+x $dir/now_start_perl_fcgi.sh 27 28 sudo -u www-data $dir/now_start_perl_fcgi.sh 29 echo "start perl-fcgi done" 30 } 31 32 case $1 in 33 stop) 34 stop 35 ;; 36 start) 37 start 38 ;; 39 restart) 40 stop 41 start 42 ;; 43 esac
perlinfo.cgi(perl环境探针):记住上传探针后设置文件权限!!!


1 #!/usr/bin/perl 2 print "Content-type: text/html\n\n"; 3 4 #Location of Perl 5 $output = `whereis perl`; 6 @locations = split(" ",$output); 7 foreach $line (@locations) 8 { 9 $whereperl .= "$line<br>"; 10 } 11 12 #Location of Sendmail 13 $output = `whereis sendmail`; 14 @locations = split(" ",$output); 15 foreach $line (@locations) 16 { 17 $wheresendmail .= "$line<br>"; 18 } 19 20 #Location of Current Directory 21 $currentdirectory = `pwd`; 22 23 #Perl Variables 24 $perlversion = $]; 25 #Perl Os 26 $perlos = $^O; 27 28 29 #Module Paths 30 foreach $line (@INC) 31 { 32 $modulepaths .= "$line<br>"; 33 } 34 35 #Environment Variables 36 $environment = qq~ 37 <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolor="#000000"> 38 <tr> 39 <td colspan="2" bgcolor="#0033CC"> 40 <div align="center" class="tabletitle">Environment Variables</div> 41 </td> 42 </tr> 43 ~; 44 45 @allkeys = keys(%ENV); 46 47 foreach $key (@allkeys) 48 { 49 $value = $ENV{$key}; 50 if ($value eq "") {$value = "-";} 51 $environment .= qq~ 52 <tr> 53 <td width="150" class="tableitems">$key</td> 54 <td class="tablevalue">$value</td> 55 </tr> 56 ~; 57 } 58 $environment .= qq~ 59 </table> 60 ~; 61 62 63 $documentroot = $ENV{'DOCUMENT_ROOT'}; 64 if ($documentroot ne "") 65 { 66 @lines = `du -c -k $documentroot`; 67 $lastline = @lines-1; 68 ($diskusage) = split/[\t| ]/,$lines[$lastline]; 69 } 70 71 #Server Software 72 $serverip = $ENV{'SERVER_ADDR'}; 73 $servername = $ENV{'SERVER_NAME'}; 74 $serverport = $ENV{'SERVER_PORT'}; 75 76 $serversoftware = $ENV{'SERVER_SOFTWARE'}; 77 78 $serveruptime =`uptime`; 79 80 81 #Localtime 82 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 83 @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 84 $date = sprintf("%02d-%s-%04d",$mday,$months[$mon],$year+1900); 85 $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec); 86 $localtime = "$date, $time"; 87 88 #GMTtime 89 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time); 90 @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 91 $date = sprintf("%02d-%s-%04d",$mday,$months[$mon],$year+1900); 92 $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec); 93 $gmttime = "$date, $time"; 94 95 96 print qq~ 97 <html> 98 <head> 99 <title>Perlonline.com - Perlinfo.cgi</title> 100 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 101 <style type="text/css"> 102 <!-- 103 .tabletitle { font-family: Arial, Helvetica, sans-serif; font-size: 14px; font-weight: bold; background-position: center; color: #FFFFFF} 104 .tableitems { font-family: Arial, Helvetica, sans-serif; font-size: 12px} 105 .tablevalue { font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bolder} 106 --> 107 </style> 108 </head> 109 110 <body bgcolor="#FFFFFF" text="#000000"> 111 <table width="100%" border="1" cellpadding="2" cellspacing="0" bordercolor="#000000"> 112 <tr bgcolor="#0033CC"> 113 <td colspan="2" class="tabletitle"> 114 <div align="center">Server Information</div> 115 </td> 116 </tr> 117 <tr> 118 <td class="tableitems" width="150" valign="top">Name</td> 119 <td class="tablevalue">$servername</td> 120 </tr> 121 <tr> 122 <td class="tableitems" width="150" valign="top">IP</td> 123 <td class="tablevalue">$serverip</td> 124 </tr> 125 <tr> 126 <td class="tableitems" width="150" valign="top">Listing Port</td> 127 <td class="tablevalue">$serverport</td> 128 </tr> 129 <tr> 130 <td class="tableitems" width="150" valign="top">Document Root</td> 131 <td class="tablevalue">$documentroot</td> 132 </tr> 133 <tr> 134 <td class="tableitems" width="150" valign="top">Disk Usage by Root</td> 135 <td class="tablevalue">$diskusage Kb</td> 136 </tr> 137 <tr> 138 <td class="tableitems" width="150" valign="top">Software's Installed</td> 139 <td class="tablevalue">$serversoftware</td> 140 </tr> 141 </table> 142 <br> 143 <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolor="#000000"> 144 <tr bgcolor="#0033CC"> 145 <td colspan="2" class="tabletitle"> 146 <div align="center">Perl Information</div> 147 </td> 148 </tr> 149 <tr> 150 <td class="tableitems" width="150" valign="top">Perl version</td> 151 <td class="tablevalue">$perlversion</td> 152 </tr> 153 <tr> 154 <td class="tableitems" width="150" valign="top">Compiled For</td> 155 <td class="tablevalue">$perlos</td> 156 </tr> 157 <tr> 158 <td class="tableitems" width="150" valign="top">Module Paths</td> 159 <td class="tablevalue">$modulepaths</td> 160 </tr> 161 </table> 162 <br> 163 <table width="100%" border="1" bordercolor="#000000" cellpadding="2" cellspacing="0"> 164 <tr bgcolor="#0033CC"> 165 <td colspan="2" class="tabletitle"> 166 <div align="center">Location of Important Unix Programs</div> 167 </td> 168 </tr> 169 <tr> 170 <td class="tableitems" width="150" valign="top">Perl</td> 171 <td class="tablevalue">$whereperl</td> 172 </tr> 173 <tr> 174 <td class="tableitems" width="150" valign="top">Sendmail</td> 175 <td class="tablevalue">$wheresendmail</td> 176 </tr> 177 </table> 178 <br> 179 <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolor="#000000"> 180 <tr bgcolor="#0033CC"> 181 <td colspan="2" class="tabletitle"> 182 <div align="center">Time</div> 183 </td> 184 </tr> 185 <tr> 186 <td class="tableitems" width="150" valign="top">Server Time (Local)</td> 187 <td class="tablevalue">$localtime</td> 188 </tr> 189 <tr> 190 <td class="tableitems" width="150" valign="top">Server Time (GMT)</td> 191 <td class="tablevalue">$gmttime</td> 192 </tr> 193 </table> 194 <br> 195 $environment 196 <p align="center" class="tablevalue"> </p> 197 <p align="center" class="tablevalue">All rights Reserved 2001. <a href="http://www.perlonline.biz">Perlonline.biz</a></p> 198 </body> 199 </html> 200 ~;
nginx配置:
1 location ~ .*\.(pl|cgi)$ { 2 root /home/web; 3 fastcgi_index index.pl; 4 fastcgi_pass unix:/home/perl/perl-fcgi.sock; 5 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 6 include fastcgi_params; 7 }
出现下图表示成功: