Gentoo LEMP で FcgiWrap をインストールし、Perl、Ruby、および Bash 動的言語を有効にする
このチュートリアルは、Gentoo での LEMP インストールに関する以前のチュートリアルに厳密に関連しており、Fcgiwrap ゲートウェイを介して Perl や Bash、Ruby などの動的スクリプト言語を有効にすることや、Nginx 仮想ホスト設定ファイルを編集するなど、他のサーバー拡張問題を扱います。 .pl、.rb、.cgi スクリプトを使用して動的コンテンツを提供します。
要件
- Gentoo にインストールされた LEMP スタック – https://linux-console.net/install-lemp-in-gentoo-linux/
ステップ 1: Gentoo LEMP で FCGIWRAP を有効にする
Fcgiwrap は Nginx FastCGI Common Gateway Interface の一部であり、Perl、Bash、Ruby スクリプトなどの他の動的スクリプト言語を処理し、TCP や Nginx から受信したリクエストを処理することで動作します。 Unix ソケットは独立した方法で生成された結果を Nginx に返し、最終的には応答をエンド クライアントに転送します。
1. まず、次のコマンドを使用して Gentoo Linux に FCcgiwrap プロセスをインストールします。
emerge --ask www-misc/fcgiwrap
2. デフォルトでは、Fcgiwrap パッケージは、プロセスを管理するための Gentoo 上のinit スクリプトを提供しません。パッケージがコンパイルされインストールされたら、次の init スクリプトを作成します。これは、Unix ドメイン ソケット を使用してプロセスを起動するか、ローカル < を使用してプロセスを起動するかの 3 つのアプローチを使用して Fcgiwrap プロセスを管理するのに役立ちます。 b>TCP ソケットまたは両方を同時に使用します。
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..."
}
ご覧のとおり、スクリプト ファイルの先頭には 2 つの変数、それぞれ ip と port が含まれています。この変数は必要に応じて変更し、システム上の他のサービス、特にポート変数と重複しないようにしてください (デフォルトは 12345 です)。それに応じて変更してください。
IP 変数に 0.0.0.0 を使用すると、プロセスは任意の IP (ファイアウォールがない場合は外部からアクセス可能) にバインドしてリッスンできるようになりますが、セキュリティ上の理由から、ローカルでのみリッスンするように変更する必要があります。パフォーマンスや負荷分散のために別のノードに Fcgiwrap ゲートウェイをリモートで設定するなどの他の理由がない限り、127.0.0.1 に接続します。
3. ファイルが作成されたら、実行権限を追加し、開始、停止、またはステータス スイッチを使用してデーモン プロセスを管理します。ステータス スイッチには、リッスンするIP-ポート ペアや、初期化されたアクティブな接続があるかどうかなど、関連するソケット情報が表示されます。また、プロセスに TIME_WAIT 状態のアクティブな接続がある場合は、すべての TCP 接続が閉じるまで再起動できません。
chmod +x /etc/init.d/fcgiwrap
service start fcgiwrap
/etc/init.d/fcgiwrap status
Unixソケットスクリプトの使用
前に示したように、Fcgiwrap は両方のソケットを使用して同時に実行できるため、両方を同時に開始して実行できるように、2 番目のスクリプトの名前を 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 ソケットを使用し、2 番目の場所は Unix ドメイン ソケット を使用し、index.pl ファイルを使用します。 3 番目の場所は、index.cgi ファイルで TCP ソケットを使用しています。
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 または特定の仮想ホスト構成ファイルの編集が完了したら、Web サイトのデフォルトのドキュメント ルート パスに移動し、現在の場所を反映するためにこれら 2 つのフォルダーを作成します。ステートメントを作成し、すべての場所に特定の拡張子を持つインデックス ファイルを作成します。
cd /var/www/localhost/htdocs
mkdir second third
次の内容を含む index.pl ファイルを 2 番目の場所に作成します。
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;
次に、3 番目の場所に次の内容の 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 デーモンが自動的に起動できるようにします (両方の CGI ソケットを使用するように Nginx を構成している場合)。
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 ローカルホストによって提供されるデフォルトのドキュメント ルート パスの下の 4 番目の場所で 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 パスの下に 4 番目のディレクトリを作成し、.rbを使用して実行可能な Ruby インデックス スクリプトを作成します。 b> 拡張子を追加し、次の内容を追加します。
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/ に移動すると、次のコンテンツが表示されます。
これで、動的な Perl、Ruby、および Bash スクリプトを FastCGI Gateway で提供するように Nginx を設定しましたが、この種の解釈されたスクリプトを Nginx CGI Gateway で実行することは危険であり、サーバーに深刻なセキュリティ リスクをもたらす可能性があることに注意してください。システムの下でアクティブ シェルを使用して実行されますが、静的 HTML によって課される静的バリアを拡張して、Web サイトに動的な機能を追加できます。