Ịwụnye FcgiWrap na Ịkwado Perl, Ruby na Bash Dynamic Languages na Gentoo LEMP


Nkuzi a nwere njikọ chiri anya na nke mbụ na ntinye LEMP na Gentoo ma na-emeso nsogbu ndị ọzọ agbatịkwuru dị ka ime ka asụsụ dị ike dị ka Perl ma ọ bụ Bash ma ọ bụ Ruby site na Fcgiwrap Gateway, na dezie faịlụ nhazi Nginx Virtual Hosts iji nye ọdịnaya siri ike site na iji .pl, .rb na .cgi edemede.

  1. Ngwunye LEMP arụnyere na Gentoo - https://linux-console.net/install-lemp-in-gentoo-linux/

Kwụpụ 1: Kwado FCGIWRAP na Gentoo LEMP

Fcgiwrap bụ akụkụ nke Nginx FastCGI Common Gateway Interface nke na-ahazi asụsụ scripting ndị ọzọ siri ike, dị ka Perl ma ọ bụ Bash ma ọ bụ Ruby, na-arụ ọrụ site na nhazi arịrịọ enwetara n'aka Nginx, site na TCP ma ọ bụ Unix Sockets, n'ụzọ nke onwe ya wee weghachi nsonaazụ arụpụtara azụ na Nginx, nke, n'ime oge, ga-ebugharị nzaghachi azụ na njedebe ndị ahịa.

1. Ka anyị buru ụzọ malite site na ịwụnye usoro FCcgiwrap na Gentoo Linux site na iji iwu a.

# emerge --ask www-misc/fcgiwrap

2. Site na ndabara Fcgiwrap ngwugwu anaghị enye ọ bụla init scripts na Gentoo iji jikwaa usoro. Mgbe a chịkọtara ngwugwu ma wụnye ya mepụta init scripts nke na-enyere gị aka ijikwa usoro Fcgiwrap site na iji ụzọ atọ: ma ọ bụ malite usoro ahụ site na iji Unix Domain Socketsma ọ bụ iji mpaghara b>TCP Socketsma ọ bụ iji ha abụọ n'otu oge.

Mepụta faịlụ init na ụzọ /etc/init.d/ nwere ọdịnaya faịlụ ndị a.

# nano /etc/init.d/fcgiwrap

Tinye ọdịnaya faịlụ ndị a.

#!/sbin/runscript

ip="0.0.0.0"
port="12345"

start() {
ebegin "Starting fcgiwrap process..."
       /usr/sbin/fcgiwrap -s tcp:$ip:$port &
        tcp_sock=`netstat -tulpn | grep fcgiwrap`
        echo "Socket details: $tcp_sock"
eend $? "Errors were encountered while starting fcgiwrap process"
}

stop() {
ebegin "Stopping fcgiwrap process..."
                pid=`ps a | grep fcgiwrap | grep tcp | cut -d" " -f1`
kill -s 1 $pid
                tcp_sock=`netstat -tulpn | grep fcgiwrap`
                 if test $tcp_sock =  2> /dev/null ; then
                 echo "Fcgiwrap process successfully stoped"
                tcp_sock=`netstat -atulpn | grep $port`
                if test $tcp_sock =  2> /dev/null ; then
                echo "No open fcgiwrap connection found..."
                else
                echo "Wait to close fcgiwrap open connections...please verify with 'status'"
                echo -e "Socket details: \n$tcp_sock"
                 fi
                else
                echo "Fcgiwarp process is still running!"
        echo "Socket details: $tcp_sock"
        fi
eend $? "Errors were encountered while stopping fcgiwrap process..."
}

status() {
ebegin "Status fcgiwrap process..."
      tcp_sock=`netstat -atulpn | grep $port`
    if test $tcp_sock =  2> /dev/null ; then
                       echo "Fcgiwrap process not running"
                     else
                echo "Fcgiwarp process is running!"
                 echo -e "Socket details: \n$tcp_sock"
                fi
eend $? "Errors were encountered while stopping fcgiwrap process..."
}

Dị ka ị na-ahụ faịlụ script na-ejide mgbanwe abụọ na mmalite, dịka ip na ọdụ ụgbọ mmiri. Gbanwee mgbanwe ndị a na mkpa nke gị wee hụ na ha ejikọtaghị na ọrụ ndị ọzọ na sistemụ gị, ọkachasị ọdụ ụgbọ mmiri - ndabara ebe a bụ 12345 - gbanwee ya.

Iji 0.0.0.0 na IP variable na-eme ka usoro ahụ kechie ma gee ntị na IP ọ bụla (n'èzí ị nwere ike ịnweta ma ọ bụrụ na ịnweghị firewall ), mana maka ihe nchebe ị kwesịrị ịgbanwe ya ka ọ gee ntị naanị na mpaghara, na 127.0.0.1, ọ gwụla ma ị nwere ihe ndị ọzọ dị ka nhazi ọnụ ụzọ Fcgiwrap nke dịpụrụ adịpụ na ọnụ ụzọ dị iche maka ịrụ ọrụ ma ọ bụ nhazi ibu.

3. Mgbe emechara faịlụ ahụ, tinye ikikere igbu ma jikwaa usoro daemon site na iji mmalite, kwụsị ma ọ bụ ọnọdụ mgbanwe. Mgbanwe ọkwa ga-egosi gị ozi sọket dị mkpa dị ka IP-PORTmekọrịta ọ na-ege ntị ma ọ bụrụ na njikọ ọ bụla na-arụ ọrụ ebe ebidobere ya. Ọzọkwa, ọ bụrụ na usoro ahụ nwere njikọ na-arụ ọrụ na TIME_WAIT steeti ị nweghị ike ịmalitegharị ya ruo mgbe njikọ TCP niile mechiri.

# chmod +x /etc/init.d/fcgiwrap
# service start fcgiwrap
# /etc/init.d/fcgiwrap status

Dịka e gosipụtara na mbụ Fcgiwrap nwere ike ịgba ọsọ n'otu oge na-eji oghere abụọ ahụ, yabụ ga-agbanwe ntakịrị aha nke edemede nke abụọ ka ọ bụrụ fcgiwrap-unix-socket, iji hụ na enwere ike ịmalite ha abụọ wee gbaa ọsọ n'otu oge.

# nano /etc/init.d/fcgiwrap-unix-socket

Jiri ọdịnaya faịlụ ndị a maka oghere UNIX.

#!/sbin/runscript
sock_detail=`ps a | grep fcgiwrap-unix | head -1`

start() {
ebegin "Starting fcgiwrap-unix-socket process..."
        /usr/sbin/fcgiwrap -s unix:/run/fcgiwrap-unix.sock &
        sleep 2
        /bin/chown nginx:nginx /run/fcgiwrap-unix.sock
        sleep 1
        sock=`ls -al /run/fcgiwrap-unix.sock`
        echo "Socket details: $sock"
eend $? "Errors were encountered while starting fcgiwrap process"
}

stop() {
ebegin "Stopping fcgiwrap-unix-socket process..."
                pid=`ps a | grep fcgiwrap | grep unix | cut -d" " -f1`
                rm -f /run/fcgiwrap-unix.sock                 
                kill -s 1 $pid
                echo "Fcgiwrap process successfully stoped"
                #killall /usr/sbin/fcgiwrap
        sleep 1
        echo "Socket details: $sock"
eend $? "Errors were encountered while stopping fcgiwrap process..."
}

status() {
ebegin "Status fcgiwrap-unix-socket process..."
  if test -S /run/fcgiwrap-unix.sock; then
       echo "Process is started with socket: $sock_detail"
        else
        echo "Fcgiwrap process not running!"
        fi
eend $? "Errors were encountered while stopping fcgiwrap process..."
}

4. Ọzọ mesie ike na faịlụ a nwere ike ịrụ ọrụ ma jiri otu mgbanwe ọrụ ahụ: mmalite, kwụsị ma ọ bụ ọnọdụ. Edobela m ụzọ ndabara maka oghere a na /run/fcgiwrap-unix.sock ụzọ sistemu. Malite usoro wee nyochaa ya site na iji ọnọdụ ịgbanwee ma ọ bụ depụta / run ọdịnaya ndekọ wee chọta oghere, ma ọ bụ jiri ps -a | grep fcgiwrapiwu.

# chmod +x /etc/init.d/fcgiwrap-unix-socket
# service start fcgiwrap-unix-socket
# /etc/init.d/fcgiwrap-unix-socket status
# ps -a | grep fcgiwrap

Dị ka e kwuru na mbụ Fcgiwrap nwere ike na-agba ọsọ na TCP na UNIX sockets n'out oge, ma ọ bụrụ na ị na-adịghị mkpa mpụga ọnụ ụzọ ámá na-arapara n'ahụ Unix Domain Socketnaanị, n'ihi na ọ na-eji interprocess nkwurịta okwu, nke dị ngwa ngwa karịa nkwurịta okwu n'elu. Njikọ TCP loopback, ma na-eji obere TCP n'elu.

Kwụpụ 2: Kwado scripts CGI na Nginx

5. N'ihi na Nginx na-atụgharị na-agba ọsọ Perl ma ọ bụ Bash scripts site Fast Common Gateway Interface, Virtual Hosts ga-ahazi na Fcgiwrap nkọwa na mgbọrọgwụ ụzọ ma ọ bụ ọnọdụ nkwupụta.

A na-egosi ihe atụ n'okpuru (localhost), nke na-eme ka Perl na CGI scripts na faịlụ niile etinyere na mgbọrọgwụ ụzọ (/var/www/localhost/htdocs/) na .pl > na .cgi ndọtị site na iji Fcgiwrap TCP Sockets maka ụzọ akwụkwọ mgbọrọgwụ ndabere, ebe nke abụọ na-eji Unix Domain Sockets, yana faịlụ index.pl na ebe nke atọ na-eji TCP sockets nwere faịlụ index.cgi.

Debe ọdịnaya ndị a, ma ọ bụ naanị akụkụ ụfọdụ nke ya, na faịlụ nhazi nhazi nke onye ọbịa ị chọrọ ka ị rụọ ọrụ Perl ma ọ bụ Bash scripts nwere UNIX ma ọ bụ TCP Sockets n'okpuru ebe dị iche iche, site na ịmegharị fastcgi_pass nkwupụta arụmụka.

# nano /etc/nginx/sites-available/localhost.conf

Dezie localhost.conf ka ọ dị na ndebiri dị n'okpuru.

server {
                                listen 80;
                                server_name localhost;

access_log /var/log/nginx/localhost_access_log main;
error_log /var/log/nginx/localhost_error_log info;

               root /var/www/localhost/htdocs/;
                location / {
                autoindex on;
                index index.html index.htm index.php;
                                }

## PHP –FPM Gateway ###
                            location ~ \.php$ {
                            try_files $uri =404;
                            include /etc/nginx/fastcgi.conf;
                            fastcgi_pass 127.0.0.1:9001;
				}

## Fcgiwrap Gateway on all files under root with TCP Sockets###
location ~ \.(pl|cgi|rb)$ {
                fastcgi_index index.cgi index.pl;
                include /etc/nginx/fastcgi.conf;
fastcgi_pass 127.0.0.1:12345;    
                                }                                                                                                                             

## Fcgiwrap Gateway on all files under root second folder with index.pl using UNIX Sockets###
location /second {
                                index index.pl; 
root /var/www/localhost/htdocs/;
                                location ~ \.(pl|cgi|rb)$ {
                                include /etc/nginx/fastcgi.conf;
                                fastcgi_pass unix:/run/fcgiwrap-unix.sock;      
                                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                                             }                                                                                                            
                                                }

## Fcgiwrap Gateway on all files under root third folder with index.cgi using TCP Sockets###
location /third {
                                index index.cgi;               
                                location ~ \.(pl|cgi|rb)$ {
                                include /etc/nginx/fastcgi.conf;
                                 fastcgi_pass 127.0.0.1:12345;       
                                }                                                                                             
  }

6. Mgbe ịmechara Nginx localhost.conf, ma ọ bụ faịlụ nhazi nke Virtual Host gị, gaa na ebe nrụọrụ weebụ ndabere akwụkwọ mgbọrọgwụ, mepụta folda abụọ ahụ iji gosipụta nkwupụta ọnọdụ gị, wee mepụta faịlụ index maka. ebe ọ bụla nwere ndọtị ya akọwapụtara.

# cd /var/www/localhost/htdocs
# mkdir second third

Mepụta faịlụ index.pl na ọnọdụ nke abụọ yana ọdịnaya ndị a.

# nano /var/www/localhost/htdocs/second/index.pl

Tinye ọdịnaya a ka ị nweta mgbanwe gburugburu ebe obibi.

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print <<HTML;
                <html>
                <head><title>Perl Index</title></head>
                <body>
                                <div align=center><h1>A Perl CGI index on second location with env variables</h1></div>
                </body>
HTML
print "Content-type: text/html\n\n"; foreach my $keys (sort keys %ENV) { print "$keys =
$ENV{$keys}<br/>\n";
}
exit;

Wee mepụta faịlụ index.cgi na ọnọdụ nke atọ yana ọdịnaya ndị a.

# nano /var/www/localhost/htdocs/third/index.cgi

Tinye ọdịnaya a ka ị nweta mgbanwe gburugburu ebe obibi.

#!/bin/bash
echo Content-type: text/html
echo ""
cat << EOF
<HTML>
<HEAD><TITLE>Bash script</TITLE></HEAD>
<BODY><PRE>
<div align=center><h1>A BASH CGI index on third location with env variables</h1></div>
EOF
env
cat << EOF
</BODY>
</HTML>
EOF

7. Mgbe emechara edezi, mee ka faịlụ abụọ ahụ rụọ ọrụ, malitegharịa Nginx nkesa ma hụ na ma Fcgiwrap sockets na-agba ọsọ.

# chmod +x /var/www/localhost/htdocs/second/index.pl
# chmod +x /var/www/localhost/htdocs/third/index.cgi
# service nginx restart
# service fcgiwrap start
# service fcgiwrap-unix-socket start

Na-esote, megharịa ihe nchọgharị mpaghara gị na URL na-eso.

http://localhost 

http://localhost/second/ 

http://localhost/third/

Nsonaazụ kwesịrị ịpụta dị ka n'okpuru nseta ihuenyo.

8. Ọ bụrụ na ihe niile dị na ya ma hazie ya nke ọma, mee ka ma Fcgiwrap daemons malite na-akpaghị aka, mgbe ịmalitegharịa site na ịnye iwu ndị a (ọ bụrụ na ị haziela Nginx ka ọ jiri ma CGI sockets).

# rc-update add fcgiwrap default
# rc-update add fcgiwrap-unix-socket default

Kwụpụ 3: Tinye nkwado Ruby na Fcgiwrap

9. Ọ bụrụ na ịchọrọ ịgba ọsọ Ruby scripts na Nginx FCGI ị ga-etinyerịrị onye ntụgharị Ruby na Gentoo site na iwu a.

# emerge --ask ruby

10. Mgbe achịkọtachara ma wụnye ngwugwu ahụ, gaa na Nginx saịtị dị wee dezie faịlụ localhost.conf site na itinye okwu ndị a n'ihu akara mkpịrịkọ ikpeazụ \ } , nke na-akwalite nkwado iji mee script Ruby na ebe anọ n'okpuru ụzọ mgbọrọgwụ akwụkwọ nke Nginx localhost na-eje ozi.

# nano /etc/nginx/sites-available/localhost.conf

Jiri ntuziaka Nginx ndị a.

## Fcgiwrap Gateway on all files under root fourth folder with index.rb under TCP Sockets###
                location /fourth {
                                index index.rb;
                                location ~ \.rb$ {
                                include /etc/nginx/fastcgi.conf;
                                fastcgi_pass 127.0.0.1:12345;       
                                                }                                                                                                             
                               }             
## Last curly bracket which closes Nginx server definitions ##
}

11. Ugbu a, iji nwalee nhazi mepụta ndekọ nke anọ n'okpuru /var/www/localhost/htdocsụzọ, mepụta ihe odide Ruby executable na .rb ndọtị wee gbakwunye ihe ndị a. ọdịnaya.

# mkdir /var/www/localhost/htdocs/fourth
# nano /var/www/localhost/htdocs/fourth/index.rb

Ihe atụ Ruby index.rb.

#!/usr/bin/ruby
puts "HTTP/1.0 200 OK"
puts "Content-type: text/html\n\n"
puts "<html><HEAD><TITLE>Ruby script</TITLE></HEAD>"
puts "<BODY><PRE>"
puts "<div align=center><h1>A Ruby CGI index on fourth location with env variables</h1></div>"
system('env')

12. Mgbe ị gbakwunyere ikike igbu egbu na faịlụ, malitegharịa Nginx daemon iji tinye nhazi.

# chmod +x /var/www/localhost/htdocs/fourth/index.rb
# service nginx restart

Mepee ihe nchọgharị gị wee gaa na URL http://localhost/fourth/, nke kwesịrị igosi gị ọdịnaya ndị a.

Nke ahụ bụ ya ugbu a, ị haziela Nginx ka ọ na-eje ozi Perl, Ruby na Bash scripts na FastCGI Gateway, mana, mara na ịgba ọsọ ụdị edemede a na Nginx CGI Gateway nwere ike ịdị ize ndụ ma tinye nnukwu ihe egwu na nchekwa gị n'ihi na ha na-agba ọsọ site na iji shells na-arụ ọrụ n'okpuru sistemụ gị, mana ọ nwere ike gbasaa ihe mgbochi static HTML kwadoro, na-agbakwunye ọrụ siri ike na ebe nrụọrụ weebụ gị.