免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1011 | 回复: 0
打印 上一主题 下一主题

RHEL4+nginx+sendmail+perl+openwebmail(二) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-27 10:25 |只看该作者 |倒序浏览
五、安装Perl的FastCGI模块:

1.安装
FCGI: http://www.cpan.org/modules/by-module/FCGI/FCGI-0.67.tar.gz
#tar zxvf FCGI-0.67.tar.gz
#cd FCGI-0.67
#perl Makefile.PL
#make && make install

#rpm   -ivh   perl-FCGI-ProcManager-0.18-1.el4.rf.noarch.rpm

2.配置Perl的FastCGI脚本(从网上找到的,未找到原始出处):


#vi /etc/init.d/perl-fast
*********************************************************************************
#!/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);
#export FCGI_SOCKET_PATH="/tmp/perl-fastcgi.sock"
#export FCGI_NPROCESSES=4
#&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 {
#.... IP sockets
#$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 );
#.... UNIX sockets
#$socket = FCGI::OpenSocket( "/temp/perl-fastcgi.sock", 10 );

#foreach $item (keys %ENV) { delete $ENV{$item}; }
#..fastcgi........
my $n_processes = $ENV{FCGI_NPROCESSES} || 4;
$proc_manager = FCGI::ProcManager->new( {n_processes => $n_processes} );
#..unix socket
$socket = FCGI::OpenSocket( "$ENV{FCGI_SOCKET_PATH}", 10 );
#..Socket..
chmod 0777, $ENV{FCGI_SOCKET_PATH};

; #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
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, "
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  /etc/init.d/perl-fast
#vi /etc/profile
添加以下内容
export FCGI_SOCKET_PATH="/tmp/perl-fastcgi.sock"
export FCGI_NPROCESSES=4
#source /etc/profile
#/etc/init.d/perl-fast&

六、为Nginx添加FastCGI的Perl支持

server {
        listen       80;
        server_name  mail.test5.com;
        charset gb2312;
        access_log  logs/access.log  main;
        location / {
            root   /share/htdocs;
            index  index.php index.html;
            }
        location ~* .*\.pl$ {
             root  /share/htdocs;
             include perl.conf;
             }
}

#vi perl.conf
--------------------------------------------------------------------------
fastcgi_pass  unix:/tmp/perl-fastcgi.sock;                        
fastcgi_index openwebmail.pl;                                    
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;                    
fastcgi_read_timeout 60;  
---------------------------------------------------------------------------
七、重启nginx
#Kill -HUP `cat /var/run/nginx.pid`
八、访问openwebmail
在地址栏中: 访问http://mail.test5.com/cgi-bin/openwebmail/openwebmail.pl     
搞定,openwebmail顺利登录,收发邮件正常




本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/14353/showart_1668580.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP