View RSS Feed

admin

Nginx and Perl-FastCGI on Debian 5 (Lenny)

Rate this Entry
โดย เมื่อ 03-06-2010 เวลา 15:43:12 (1207 เปิดอ่าน)
Install Required Packages

Issue the following commands to update your system and install the nginx web server and compiler tools (Perl should already be installed):

Code:
apt-get update
apt-get upgrade
apt-get install nginx build-essential psmisc wget
/etc/init.d/nginx start
Various additional dependency packages will be installed along with the ones we requested. Once the installation process finishes, you may wish to make sure nginx is running by browsing to your Linode's IP address (found on the "Network" tab in the Linode Manager). You should get the default "Welcome to nginx!" page.
Configure Your Site

In this guide, we'll be using the domain "bambookites.com" as our example site. You should substitute your own domain name in the configuration steps that follow. First, we'll need to create directories to hold our content and log files:

Code:
mkdir -p /srv/www/www.bambookites.com/public_html
mkdir /srv/www/www.bambookites.com/logs
chown -R www-data:www-data /srv/www/www.bambookites.com
Next, we'll need to define our site's virtual host file:
File: /etc/nginx/sites-available/www.bambookites.com

Code:
server {
    listen   80;
    server_name www.bambookites.com bambookites.com;
    access_log /srv/www/www.bambookites.com/logs/access.log;
    error_log /srv/www/www.bambookites.com/logs/error.log;

    location / {
        root   /srv/www/www.bambookites.com/public_html;
        index  index.html index.htm;
    }

    location ~ \.pl$ {
        gzip off;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:8999;
        fastcgi_index index.pl;
        fastcgi_param  SCRIPT_FILENAME  /srv/www/www.bambookites.com/public_html$fastcgi_script_name;
    }
}
Issue the following commands to enable the site:

Code:
cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/www.bambookites.com
/etc/init.d/nginx restart
You may wish to create a test HTML page under /srv/www/www.bambookites.com/public_html/ and view it in your browser to verify that nginx is properly serving your site (Perl will not work yet). Please note that this will require an entry in DNS pointing your domain name to your Linode's IP address.

Install spawn-fcgi

Visit the spawn-fcgi project page and locate the download link to the latest version. Issue the following commands, substituting your link for the one shown below if a newer version is available.

Code:
cd /opt
wget http://www.lighttpd.net/download/spa...i-1.6.3.tar.gz
tar -xf spawn*
cd spawn*
./configure
make
cp src/spawn-fcgi /usr/bin/spawn-fcgi
Install the Perl module for FastCGI support with the following command. When asked about automatic configuration for CPAN, you may enter "Yes" or choose to manually configure it.

Code:
perl -MCPAN -e 'install FCGI'
Create the wrapper script for spawn-fcgi:
File: /usr/bin/fastcgi-wrapper.pl (credit: Denis S. Filimonov)

Code:
#!/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 = ) { 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";
            }

        }
}
Make the script executable with the following command:

Code:
chmod a+x /usr/bin/fastcgi-wrapper.pl
Create an init script for fast-cgi:
File: /etc/init.d/perl-fastcgi

Code:
#!/bin/bash
PERL_SCRIPT=/usr/bin/fastcgi-wrapper.pl
RETVAL=0
case "$1" in
    start)
      $PERL_SCRIPT
      RETVAL=$?
  ;;
    stop)
      killall -9 perl
      RETVAL=$?
  ;;
    restart)
      killall -9 perl
      $PERL_SCRIPT
      RETVAL=$?
  ;;
    *)
      echo "Usage: perl-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL
Issue the following commands to make the init script run at startup and launch it for the first time:

Code:
chmod 755 /etc/init.d/perl-fastcgi
update-rc.d perl-fastcgi defaults
/etc/init.d/perl-fastcgi start
Test Perl with FastCGI

Create a file called "test.pl" in your site's "public_html" directory with the following contents:
File: /srv/www/www.bambookites.com/public_html/test.pl

Code:
#!/usr/bin/perl

print "Content-type:text/html\n\n";
print <Perl Environment Variableshead>

Perl Environment Variablesh1> EndOfHTML foreach $key (sort(keys %ENV)) { print "$key = $ENV{$key} \n"; } print "";



Make the script executable by issuing the following command:

Code:
chmod a+x /srv/www/www.bambookites.com/public_html/test.pl

When you visit http://www.bambookites.com/test.pl in your browser, your Perl environment variables should be shown. Congratulations, you've configured the nginx web server to use Perl with FastCGI for dynamic content!

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.


License

This guide is licensed under a Creative Commons Attribution-No Derivative Works 3.0 United States License. Please feel free to redistribute unmodified copies of it as long as attribution is provided, preferably via a link to this page.

ที่มา http://library.linode.com/web-server...debian-5-lenny
ป้ายกำกับ: Nginx and Perl-FastCGI, Nginx on Debian 5 เพิ่ม / แก้ไข ป้ายกำกับ
หัวข้อ
How To , Linux , Software

ความคิดเห็น


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90