网站搜索

在 Gentoo LEMP 上安装 FcgiWrap 并启用 Perl、Ruby 和 Bash 动态语言


本教程与 Gentoo 上的 LEMP 安装的前一篇教程严格相关,并处理其他服务器扩展问题,例如通过 Fcgiwrap Gateway 启用 Perl 或 Bash 或 Ruby 等动态脚本语言,并编辑 Nginx 虚拟主机配置文件以使用 .pl.rb.cgi 脚本提供动态内容。

要求

  1. Gentoo 上安装的 LEMP 堆栈 – https://www.howtoing.com/install-lemp-in-gentoo-linux/

第 1 步:在 Gentoo LEMP 上启用 FCGIWRAP

FcgiwrapNginx FastCGI 通用网关接口的一部分,它处理其他动态脚本语言,如 Perl 或 Bash 或 Ruby 脚本,通过 TCP 或 TCP 处理从 Nginx 收到的请求来工作。 Unix Sockets 以独立的方式将生成的结果返回给 Nginx,Nginx 将响应转发回最终客户端。

1. 首先,我们使用以下命令在 Gentoo Linux 上安装 FCcgiwrap 进程。

emerge --ask www-misc/fcgiwrap

2. 默认情况下,Fcgiwrap 软件包在 Gentoo 上不提供任何 init 脚本来管理进程。编译并安装软件包后,创建以下 init 脚本,帮助您使用三种方法管理 Fcgiwrap 进程:使用 Unix 域套接字 启动进程或使用本地 < b>TCP 套接字 或同时使用两者。

使用 TCP 套接字脚本

/etc/init.d/ 路径上创建一个包含以下文件内容的 init 文件。

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..."
}

正如您所看到的,脚本文件在开头保存了两个变量,分别是ipport。根据您自己的需要更改此变量,并确保它们不与系统上的其他服务重叠,尤其是端口变量 - 默认为 12345 - 进行相应更改。

在 IP 变量上使用 0.0.0.0 使进程能够绑定和侦听任何 IP(如果没有防火墙,则可以在外部访问),但出于安全原因,您应该将其更改为仅在本地侦听,在 127.0.0.1 上,除非您有其他原因,例如在不同节点上远程设置 Fcgiwrap 网关以实现性能或负载平衡。

3. 文件创建后,附加执行权限并使用启动、停止或状态开关管理守护进程。状态开关将向您显示相关的套接字信息,例如它侦听的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 系统路径上设置此套接字的默认路径。启动进程并使用 status 开关验证它或列出 /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 环回连接,并且使用较少的 TCP 开销。

步骤 2:在 Nginx 上启用 CGI 脚本

5. 为了使 Nginx 通过快速通用网关接口解析和运行 Perl 或 Bash 脚本,必须在根路径或位置语句上使用 Fcgiwrap 定义配置虚拟主机。

下面给出了一个示例 (localhost),它使用 .pl 在根路径 (/var/www/localhost/htdocs/) 中的所有文件上激活 Perl 和 CGI 脚本> 和 .cgi 扩展名,使用 Fcgiwrap TCP 套接字作为默认根文档路径,第二个位置使用 Unix 域套接字,带有 index.pl 文件第三个位置是将 TCP 套接字index.cgi 文件一起使用。

通过修改 fastcgi_pass 参数语句,将以下内容或其中的某些部分放入您想要在不同位置下使用 UNIX 或 TCP 套接字激活动态 Perl 或 Bash 脚本的虚拟主机配置文件中。

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 或特定虚拟主机配置文件的编辑后,移动到网站默认文档根路径,创建这两个文件夹以反映您的位置语句,并为每个位置及其特定扩展名创建索引文件。

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 守护进程(如果您已将 Nginx 配置为使用两个 CGI 套接字)。

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

步骤 3:激活 Fcgiwrap 上的 Ruby 支持

9. 如果您需要在 Nginx FCGI 上运行动态 Ruby 脚本,您必须使用以下命令在 Gentoo 上安装 Ruby 解释器。

emerge --ask ruby

10. 软件包编译并安装后,转到 Nginx sites-available 并编辑 localhost.conf 文件,在前面附加以下语句最后一个大括号 “ } ”,它激活对在 Nginx 本地主机提供的默认文档根路径下的第四个位置上运行 Ruby 脚本的支持。

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 路径下创建第四个目录,使用 .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守护进程以应用配置。

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

打开浏览器并导航到 URL http://localhost/fourth/,其中应显示以下内容。

现在就这样,您已经将 Nginx 配置为在 FastCGI 网关上提供动态 Perl、Ruby 和 Bash 脚本,但是请注意,在 Nginx CGI 网关上运行此类解释脚本可能很危险,并且会给您的服务器带来严重的安全风险,因为它们在您的系统下使用活动 shell 运行,但可以扩展静态 HTML 施加的静态障碍,为您的网站添加动态功能。