Zoeken op website

FcgiWrap installeren en dynamische talen van Perl, Ruby en Bash inschakelen op Gentoo LEMP


Deze tutorial is strikt gerelateerd aan de vorige over LEMP Installatie op Gentoo en behandelt andere uitgebreide serverproblemen zoals het inschakelen van dynamische scripttalen zoals Perl of Bash of Ruby via Fcgiwrap Gateway, en het bewerken van Nginx Virtual Hosts-configuratiebestanden naar serveer dynamische inhoud met behulp van de scripts .pl, .rb en .cgi.

Vereisten

  1. LEMP-stack geïnstalleerd op Gentoo – https://linux-console.net/install-lemp-in-gentoo-linux/

Stap 1: Schakel FCGIWRAP in op Gentoo LEMP

Fcgiwrap is een onderdeel van Nginx FastCGI Common Gateway Interface die andere dynamische scripttalen verwerkt, zoals Perl- of Bash- of Ruby-scripts, werkt door verzoeken te verwerken ontvangen van Nginx, via TCP of Unix Sockets, op een onafhankelijke manier, en stuurt het geproduceerde resultaat terug naar Nginx, die op termijn de antwoorden terugstuurt naar de eindclients.

1. Laten we eerst beginnen met het installeren van het FCcgiwrap proces op Gentoo Linux met behulp van de volgende opdracht.

emerge --ask www-misc/fcgiwrap

2. Standaard biedt het Fcgiwrap-pakket geen init-scripts op Gentoo om het proces te beheren. Nadat de pakketten zijn gecompileerd en geïnstalleerd, maakt u de volgende init-scripts die u helpen het Fcgiwrap-proces te beheren met behulp van drie benaderingen: het starten van het proces met Unix Domain Sockets of het gebruik van lokale < b>TCP-sockets of beide tegelijkertijd gebruiken.

TCP Socket-script gebruiken

Maak een init-bestand op het pad /etc/init.d/ met de volgende bestandsinhoud.

nano /etc/init.d/fcgiwrap

Voeg de volgende bestandsinhoud toe.

#!/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..."
}

Zoals u kunt zien bevat het scriptbestand aan het begin twee variabelen, respectievelijk ip en port. Wijzig deze variabelen volgens uw eigen behoeften en zorg ervoor dat ze niet overlappen met andere services op uw systeem, vooral de poortvariabele (standaard is hier 12345). Wijzig dienovereenkomstig.

Door 0.0.0.0 op de IP-variabele te gebruiken, kan het proces binden en luisteren op elk IP-adres (buiten toegankelijk als u geen firewall heeft), maar om veiligheidsredenen moet u dit wijzigen om alleen lokaal te luisteren. op 127.0.0.1, tenzij u andere redenen heeft, zoals het op afstand instellen van de Fcgiwrap-gateway op een ander knooppunt voor prestaties of taakverdeling.

3. Nadat het bestand is gemaakt, voegt u uitvoeringsrechten toe en beheert u het daemonproces met behulp van start-, stop- of statusschakelaars. De statusschakelaar toont u relevante socketinformatie, zoals het IP-POORT-paar waarnaar wordt geluisterd en of er een actieve verbinding is geïnitialiseerd. Als het proces actieve verbindingen heeft in de status TIME_WAIT, kunt u het proces pas opnieuw starten nadat alle TCP-verbindingen zijn gesloten.

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

Unix Socket-script gebruiken

Zoals eerder gepresenteerd kan Fcgiwrap gelijktijdig draaien met beide sockets, dus zal de naam van het tweede script enigszins veranderen in fcgiwrap-unix-socket, om ervoor te zorgen dat beide tegelijkertijd kunnen worden gestart en uitgevoerd.

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

Gebruik de volgende bestandsinhoud voor UNIX-socket.

#!/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. Zorg er opnieuw voor dat dit bestand uitvoerbaar is en gebruik dezelfde serviceopties: start, stop of status. Ik heb het standaardpad voor deze socket ingesteld op /run/fcgiwrap-unix.sock systeempad. Start het proces en verifieer het met behulp van de status schakelaar of lijst de /run mapinhoud op en zoek de socket, of gebruik ps -a | grep fcgiwrap opdracht.

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

Zoals eerder vermeld kan Fcgiwrap tegelijkertijd met zowel TCP- als UNIX-sockets draaien, maar als je geen externe gateway-verbindingen nodig hebt, kun je alleen Unix Domain Socket gebruiken, omdat het gebruik maakt van communicatie tussen processen, wat sneller is dan communicatie via TCP-loopback-verbindingen en gebruikt minder TCP-overhead.

Stap 2: Schakel CGI-scripts in op Nginx

5. Om ervoor te zorgen dat Nginx Perl- of Bash-scripts kan parseren en uitvoeren via de Fast Common Gateway Interface, moeten virtuele hosts worden geconfigureerd met Fcgiwrap-definities op hoofdpad- of locatie-instructies.

Hieronder wordt een voorbeeld gegeven (localhost), dat Perl- en CGI-scripts activeert op alle bestanden die in het hoofdpad (/var/www/localhost/htdocs/) zijn geplaatst met .pl en .cgi extensie die Fcgiwrap TCP Sockets gebruikt als standaard rootdocumentpad, de tweede locatie die Unix Domain Sockets gebruikt, met een index.pl bestand en de derde locatie gebruikt TCP-sockets met een index.cgi-bestand.

Plaats de volgende inhoud, of slechts enkele delen ervan, in het gewenste Virtual Host-configuratiebestand waarmee u dynamische Perl- of Bash-scripts wilt activeren met UNIX- of TCP-sockets op verschillende locaties, door de argumentinstructie fastcgi_pass te wijzigen.

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

Bewerk localhost.conf zodat het eruitziet als in de onderstaande sjabloon.

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. Nadat u klaar bent met het bewerken van Nginx localhost.conf, of uw specifieke Virtual Host-configuratiebestand, gaat u naar het standaard hoofdpad van uw website en maakt u deze twee mappen om uw locatie weer te geven statement, en maak indexbestanden voor elke locatie met zijn specifieke extensie.

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

Maak een index.pl-bestand op de tweede locatie met de volgende inhoud.

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

Voeg deze inhoud toe om omgevingsvariabelen op te halen.

#!/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;

Maak vervolgens een index.cgi-bestand op de derde locatie met de volgende inhoud.

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

Voeg deze inhoud toe om omgevingsvariabelen op te halen.

#!/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. Wanneer u klaar bent met bewerken, maakt u beide bestanden uitvoerbaar, start u de Nginx-server opnieuw op en zorgt u ervoor dat beide Fcgiwrap-sockets actief zijn.

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

Verwijs vervolgens uw lokale browser door naar de volgende URL.

http://localhost 

http://localhost/second/ 

http://localhost/third/

Het resultaat zou moeten verschijnen zoals op onderstaande schermafbeeldingen.

8. Als alles aanwezig is en correct is geconfigureerd, schakel dan beide Fcgiwrap-daemons in om automatisch te starten, na het opnieuw opstarten, door de volgende opdrachten te geven (in het geval dat u Nginx hebt geconfigureerd om beide CGI-sockets te gebruiken).

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

Stap 3: Activeer Ruby-ondersteuning op Fcgiwrap

9. Als je dynamische Ruby-scripts moet uitvoeren op Nginx FCGI, moet je de Ruby-interpreter op Gentoo installeren met de volgende opdracht.

emerge --ask ruby

10. Nadat het pakket is gecompileerd en geïnstalleerd, gaat u naar Nginx sites-available en bewerkt u het bestand localhost.conf door de volgende instructies toe te voegen laatste accolade “ } ”, die ondersteuning activeert om Ruby-scripts uit te voeren op een vierde locatie onder het standaard hoofdpad van het document dat wordt bediend door Nginx localhost.

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

Gebruik de volgende Nginx-richtlijnen.

## 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. Om de configuratie te testen, maakt u nu de vierde map onder het pad /var/www/localhost/htdocs en maakt u een uitvoerbaar Ruby-indexscript met .rb b> extensie en voeg de volgende inhoud toe.

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

Ruby index.rb-voorbeeld.

#!/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. Nadat u uitvoeringsrechten aan het bestand heeft toegevoegd, start u de Nginx-daemon opnieuw om de configuraties toe te passen.

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

Open uw browser en navigeer naar de URL http://localhost/fourth/, die u de volgende inhoud zou moeten tonen.

Dat is het voor nu, je hebt Nginx geconfigureerd om dynamische Perl-, Ruby- en Bash-scripts op FastCGI Gateway te leveren, maar houd er rekening mee dat het uitvoeren van dit soort geïnterpreteerde scripts op Nginx CGI Gateway gevaarlijk kan zijn en ernstige veiligheidsrisico's voor je server kan opleveren, omdat ze wordt uitgevoerd met actieve shells onder uw systeem, maar kan de statische barrière uitbreiden die wordt opgelegd door statische HTML, waardoor dynamische functionaliteit aan uw website wordt toegevoegd.