nginx添加prel支持

目录

基础介绍

目前互联网开发中,广泛采用的主流的配置是LNMP即linux+nginx+mysql+php。这类服务器的搭建部署教程网络上很多,也有很多集成安装包,可以很方便的完成安装。按照本文的步骤操作前,请确保你的nginx服务已经安装妥当。

对于一些用prel语言开发网络应用来说,搭建基于apache的web服务是很轻松的,毕竟网络上有很多相关介绍。用nginx话就必须像php那样采用cgi的方式来执行prel程序。下面就来介绍如何搭建基于nginx的prel支持。

准备工作

1、操作系统:CentOS 6.2(64位版本)

2、已经安装nginx服务,或者是利用一键安装(如lnmp.org)包完成了lnmp的安装。

安装步骤

配置EPEL/REMI YUM源:

企业版 Linux 附加软件包(以下简称 EPEL)是一个由特别兴趣小组创建、维护并管理的,针对 红帽企业版 Linux(RHEL)及其衍生发行版(比如 CentOS、 Scientific Linux)的一个高质量附加软件包项目。

EPEL 项目主页地址https://fedoraproject.org/wiki/EPEL/zh-cn。EPEL只面向企业版RedHat 和CentOS,目前有版本5和版本6的支持,安装也很简单.EPEL 包含一个叫做’epel-release’的包,这个包包含了 EPEL 源的 gpg 密钥和软件源信息。您可以通过 yum 安装到您的企业版 Linux 发行版上。

CentOS 6(64位版本)的rpm地址应该是:http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm,你可以针对自己的系统选择安装不同的版本。

注意: 你也可以选择安装扩展REMI源,REMI源有最新版本的PHP或MYSQL等软件,速度也不错,remi依赖于EPEL源(地址为:http://rpms.famillecollet.com/enterprise/remi-release-6.rpm)。

安装EPEL:

#安装
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm

#安装后编辑epel.repo 修改enabled选项,值设为1(enabled=1)
vi /etc/yum.repos.d/epel.repo

#yum 更新
yum update

#安装perl的fastcgi
yum install make automake gcc gcc-c++ fcgi-perl wget

配置安装prel的FastCGI Wrapper:

fastcgi-wrapper.pl:

在/usr/bin/文件夹下建立名为:astcgi-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( "127.0.0.1:8999", 10 ); #use IP sockets
        $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";
            }

        }
}

创建prel的FastCGI服务:

perl-fastcgi:

在/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

赋予执行权限,并让注册自启动服务:

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

到这里系统prel的cgi支持就安装完成了,下一步要做的就是,修改nginx配置文件,重启nginx服务了。

nginx服务器配置

配置文件

nginx配置prel的cgi和配置php的方法是一样的,下面是一个nginx的配置示例,以bugzilla为例:

#问题跟踪 bugzilla
server
    {
        listen       80;
        server_name bugzilla.demo.com;
        index index.html index.htm index.cgi;
        root  /home/www/bugzilla;

        location ~ .*\.(cgi)?$
            {
                try_files $uri =404;
                fastcgi_pass  127.0.0.1:8999;
                fastcgi_index index.cgi;
                include fcgi.conf;
            }

        location ~ .*\.(pl)?$
            {
                try_files $uri =404;
                fastcgi_pass  127.0.0.1:8999;
                fastcgi_index index.pl;
                include fcgi.conf;
            }

        location /status {
            stub_status on;
            access_log   off;
        }

        location / {
            if (!-e $request_filename){
                rewrite ^/.*$ /index.php last;
            }
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
            {
                expires      30d;
            }

        location ~ .*\.(js|css)?$
            {
                expires      12h;
            }

        access_log  /home/wwwlogs/access.log  access;
    }

重启nginx服务,OK!