ค้นหาเว็บไซต์

การติดตั้ง FcgiWrap และการเปิดใช้งาน Perl, Ruby และ Bash Dynamic Languages บน Gentoo LEMP


บทช่วยสอนนี้เกี่ยวข้องกับบทช่วยสอนก่อนหน้าในการติดตั้ง LEMP บน Gentoo อย่างเคร่งครัด และจัดการกับปัญหาเพิ่มเติมของเซิร์ฟเวอร์อื่นๆ เช่น การเปิดใช้งานภาษาสคริปต์แบบไดนามิก เช่น Perl หรือ Bash หรือ Ruby ผ่าน Fcgiwrap Gateway และแก้ไขไฟล์การกำหนดค่า Nginx Virtual Hosts เป็น ให้บริการเนื้อหาแบบไดนามิกโดยใช้สคริปต์ .pl, .rb และ .cgi

ความต้องการ

  1. ติดตั้ง LEMP stack บน Gentoo – https://linux-console.net/install-lemp-in-gentoo-linux/

ขั้นตอนที่ 1: เปิดใช้งาน FCGIWRAP บน Gentoo LEMP

Fcgiwrap เป็นส่วนหนึ่งของ Nginx FastCGI Common Gateway Interface ซึ่งประมวลผลภาษาสคริปต์ไดนามิกอื่นๆ เช่น สคริปต์ Perl หรือ Bash หรือ Ruby ทำงานโดยการประมวลผลคำขอที่ได้รับจาก Nginx ผ่าน TCP หรือ Unix Sockets ในลักษณะอิสระและส่งกลับผลลัพธ์ที่สร้างขึ้นกลับไปยัง Nginx ซึ่งในระยะยาวจะส่งต่อการตอบกลับไปยังไคลเอนต์ปลายทาง

1. ขั้นแรกมาเริ่มต้นด้วยการติดตั้งกระบวนการ FCcgiwrap บน Gentoo Linux โดยใช้คำสั่งต่อไปนี้

emerge --ask www-misc/fcgiwrap

2. ตามค่าเริ่มต้น แพ็คเกจ Fcgiwrap จะไม่จัดเตรียมสคริปต์ init ใด ๆ บน Gentoo เพื่อจัดการกระบวนการ หลังจากรวบรวมและติดตั้งแพ็คเกจแล้ว ให้สร้างสคริปต์ init ต่อไปนี้ที่ช่วยคุณจัดการกระบวนการ Fcgiwrap โดยใช้สามวิธี: เรียกใช้กระบวนการโดยใช้ Unix Domain Sockets หรือใช้ภายในเครื่อง < b>TCP Sockets หรือใช้ทั้งสองอย่างพร้อมกัน

การใช้สคริปต์ซ็อกเก็ต TCP

สร้างไฟล์ init บนเส้นทาง /etc/init.d/ โดยมีเนื้อหาไฟล์ดังต่อไปนี้

nano /etc/init.d/fcgiwrap

เพิ่มเนื้อหาไฟล์ต่อไปนี้

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

ดังที่คุณเห็นว่าไฟล์สคริปต์เก็บตัวแปรสองตัวไว้ที่จุดเริ่มต้น ตามลำดับ ip และ พอร์ต เปลี่ยนตัวแปรนี้ตามความต้องการของคุณเอง และตรวจสอบให้แน่ใจว่าตัวแปรเหล่านี้ไม่ทับซ้อนกับบริการอื่นๆ ในระบบของคุณ โดยเฉพาะตัวแปรพอร์ต - ค่าเริ่มต้นคือ 12345 - เปลี่ยนตามนั้น

การใช้ 0.0.0.0 บนตัวแปร IP ช่วยให้กระบวนการผูกและฟังบน IP ใด ๆ (สามารถเข้าถึงได้จากภายนอกหากคุณไม่มีไฟร์วอลล์ ) แต่ด้วยเหตุผลด้านความปลอดภัย คุณควรเปลี่ยนให้ฟังในเครื่องเท่านั้น บน 127.0.0.1 เว้นแต่คุณจะมีเหตุผลอื่นๆ เช่น ตั้งค่าเกตเวย์ Fcgiwrap จากระยะไกลบนโหนดอื่นเพื่อประสิทธิภาพหรือการปรับสมดุลโหลด

3. หลังจากสร้างไฟล์แล้ว ให้เพิ่มสิทธิ์ในการดำเนินการต่อท้ายและจัดการกระบวนการ daemon โดยใช้สวิตช์เริ่ม หยุด หรือสถานะ สวิตช์สถานะจะแสดงข้อมูลซ็อกเก็ตที่เกี่ยวข้อง เช่น IP-PORT จับคู่ที่รับฟัง และหากมีการเชื่อมต่อที่ใช้งานอยู่เมื่อเริ่มต้นแล้ว นอกจากนี้ หากกระบวนการมีการเชื่อมต่อที่ใช้งานอยู่ในสถานะ TIME_WAIT คุณจะไม่สามารถรีสตาร์ทได้จนกว่าการเชื่อมต่อ TCP ทั้งหมดจะปิด

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

การใช้สคริปต์ซ็อกเก็ต Unix

ตามที่นำเสนอก่อนหน้านี้ Fcgiwrap สามารถทำงานพร้อมกันโดยใช้ทั้งสองซ็อกเก็ต ดังนั้นจะเปลี่ยนชื่อของสคริปต์ที่สองเป็น fcgiwrap-unix-socket เล็กน้อย เพื่อให้แน่ใจว่าทั้งสองสามารถเริ่มต้นและรันในเวลาเดียวกันได้

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

ใช้เนื้อหาไฟล์ต่อไปนี้สำหรับซ็อกเก็ต 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. ตรวจสอบอีกครั้งว่าไฟล์นี้สามารถเรียกใช้งานได้และใช้สวิตช์บริการเดียวกัน: เริ่มต้น, หยุด หรือ สถานะ ฉันได้ตั้งค่าเส้นทางเริ่มต้นสำหรับซ็อกเก็ตนี้บนเส้นทางระบบ /run/fcgiwrap-unix.sock เริ่มต้นกระบวนการและตรวจสอบโดยใช้สวิตช์ สถานะ หรือรายการเนื้อหาไดเรกทอรี /run และค้นหาซ็อกเก็ต หรือใช้ ps -a | คำสั่ง grep fcgiwrap

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

ตามที่กล่าวไว้ก่อนหน้านี้ Fcgiwrap สามารถทำงานได้ทั้งกับซ็อกเก็ต TCP และ UNIX พร้อมกัน แต่หากคุณไม่ต้องการการเชื่อมต่อเกตเวย์ภายนอก ให้ยึด ซ็อกเก็ตโดเมน Unix เท่านั้น เนื่องจากใช้การสื่อสารระหว่างกระบวนการ ซึ่งเร็วกว่าการสื่อสารผ่าน การเชื่อมต่อ TCP loopback และใช้โอเวอร์เฮด TCP น้อยลง

ขั้นตอนที่ 2: เปิดใช้งานสคริปต์ CGI บน Nginx

5. เพื่อให้ Nginx แยกวิเคราะห์และเรียกใช้สคริปต์ Perl หรือ Bash ผ่าน Fast Common Gateway Interface โฮสต์เสมือนจะต้องได้รับการกำหนดค่าด้วยคำจำกัดความ Fcgiwrap บนเส้นทางรูทหรือคำสั่งตำแหน่ง

ตัวอย่างที่แสดงด้านล่าง (localhost) ซึ่งเปิดใช้งานสคริปต์ Perl และ CGI บนไฟล์ทั้งหมดที่วางอยู่ในเส้นทางราก (/var/www/localhost/htdocs/) ด้วย .pl และส่วนขยาย .cgi โดยใช้ Fcgiwrap TCP Sockets สำหรับพาธเอกสารรูทเริ่มต้น ตำแหน่งที่สองโดยใช้ Unix Domain Sockets พร้อมด้วยไฟล์ index.pl และตำแหน่งที่สามใช้ ซ็อกเก็ต TCP กับไฟล์ index.cgi

วางเนื้อหาต่อไปนี้หรือเพียงบางส่วนลงในไฟล์การกำหนดค่า Virtual Host ที่คุณต้องการ คุณต้องการเปิดใช้งานสคริปต์ Perl หรือ Bash แบบไดนามิกด้วย UNIX หรือ TCP Sockets ภายใต้ตำแหน่งที่แตกต่างกัน โดยการแก้ไขคำสั่งอาร์กิวเมนต์ fastcgi_pass

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

แก้ไข localhost.conf ให้มีลักษณะเหมือนในเทมเพลตด้านล่าง

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. หลังจากที่คุณแก้ไข Nginx localhost.conf หรือไฟล์การกำหนดค่า Virtual Host เฉพาะของคุณเสร็จแล้ว ให้ย้ายไปยังเส้นทางรูทเอกสารเริ่มต้นของเว็บไซต์ของคุณ สร้างทั้งสองโฟลเดอร์นั้นเพื่อแสดงตำแหน่งของคุณ และสร้างไฟล์ดัชนีสำหรับทุกสถานที่ที่มีนามสกุลเฉพาะ

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

สร้างไฟล์ index.pl บนตำแหน่งที่สองโดยมีเนื้อหาดังต่อไปนี้

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

เพิ่มเนื้อหานี้เพื่อรับตัวแปรสภาพแวดล้อม

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

จากนั้นสร้างไฟล์ index.cgi บนตำแหน่งที่สามโดยมีเนื้อหาดังต่อไปนี้

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

เพิ่มเนื้อหานี้เพื่อรับตัวแปรสภาพแวดล้อม

#!/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. เมื่อแก้ไขเสร็จแล้ว ให้ทั้งสองไฟล์สามารถเรียกใช้งานได้ รีสตาร์ทเซิร์ฟเวอร์ Nginx และตรวจสอบให้แน่ใจว่าซ็อกเก็ต Fcgiwrap ทั้งสองทำงานอยู่

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

จากนั้น เปลี่ยนเส้นทางเบราว์เซอร์ในเครื่องของคุณตาม URL ต่อไปนี้

http://localhost 

http://localhost/second/ 

http://localhost/third/

ผลลัพธ์ควรปรากฏตามภาพหน้าจอด้านล่าง

8. หากทุกอย่างถูกต้องและกำหนดค่าอย่างถูกต้อง ให้เปิดใช้งาน Fcgiwrap daemons ทั้งสองเพื่อเริ่มต้นโดยอัตโนมัติ หลังจากรีบูตโดยใช้คำสั่งต่อไปนี้ (ในกรณีที่คุณได้กำหนดค่า Nginx ให้ใช้ซ็อกเก็ต CGI ทั้งสอง)

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

ขั้นตอนที่ 3: เปิดใช้งานการสนับสนุน Ruby บน Fcgiwrap

9. หากคุณต้องการเรียกใช้สคริปต์ Ruby แบบไดนามิกบน Nginx FCGI คุณต้องติดตั้งล่าม Ruby บน Gentoo ด้วยคำสั่งต่อไปนี้

emerge --ask ruby

10. หลังจากรวบรวมและติดตั้งแพ็คเกจแล้ว ให้ย้ายไปยัง Nginx sites-available และแก้ไขไฟล์ localhost.conf โดยต่อท้ายคำสั่งต่อไปนี้ก่อน วงเล็บปีกกาสุดท้าย “ } ” ซึ่งเปิดใช้งานการสนับสนุนเพื่อเรียกใช้สคริปต์ Ruby บนตำแหน่งที่สี่ภายใต้เส้นทางรูทเอกสารเริ่มต้นที่ให้บริการโดย Nginx localhost

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

ใช้คำสั่ง Nginx ต่อไปนี้

## 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. ตอนนี้ เพื่อทดสอบการกำหนดค่า ให้สร้างไดเร็กทอรีที่สี่ภายใต้พาธ /var/www/localhost/htdocs ให้สร้างสคริปต์ดัชนี Ruby ที่ปฏิบัติการได้ด้วย .rb ขยายและเพิ่มเนื้อหาต่อไปนี้

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

ตัวอย่าง 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. หลังจากที่คุณเพิ่มสิทธิ์ในการดำเนินการในไฟล์แล้ว ให้รีสตาร์ท Nginx daemon เพื่อใช้การกำหนดค่า

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

เปิดเบราว์เซอร์ของคุณและไปที่ URL http://localhost/fourth/ ซึ่งจะแสดงเนื้อหาต่อไปนี้

ในตอนนี้ คุณได้กำหนดค่า Nginx ให้ให้บริการสคริปต์ Perl, Ruby และ Bash แบบไดนามิกบน FastCGI Gateway แต่โปรดทราบว่าการเรียกใช้สคริปต์ที่ตีความประเภทนี้บน Nginx CGI Gateway อาจเป็นอันตรายและก่อให้เกิดความเสี่ยงด้านความปลอดภัยอย่างร้ายแรงบนเซิร์ฟเวอร์ของคุณเนื่องจาก ทำงานโดยใช้เชลล์ที่ใช้งานอยู่ภายใต้ระบบของคุณ แต่สามารถขยายอุปสรรคคงที่ที่กำหนดโดย HTML แบบคงที่ เพิ่มฟังก์ชันการทำงานแบบไดนามิกให้กับเว็บไซต์ของคุณ