スポンサーリンク
サーバエンジニアにとって最も身近な開発言語はbashだと思います。プログラミングはできないけどシェル(スクリプト)は書けるよ、という人はけっこう多いのではないでしょうか。
一方、WEBページ等で使うcgiには一般的にPHPやperlが使われることが多いです。なので、webサーバなどのインフラまではサーバエンジニア、そこから先は開発の人 という線引になることが多いかと思います。
しかしフルスタックエンジニアが求められる現在、どうせなら最後まで自分でやりたい!でもPHPは書けない。。ならシェルスクリプトをcgiにすればいいじゃないか!ということでいろいろ調べてみました。
まずは必要なパッケージをインストールします。
ここではnginxとfcgiwrapのインストール手順を書きます。
apt で一発です。
apt install -y nginx
起動も一瞬です
service nginx start
psコマンドを打って以下のような感じで表示されれば起動できています。
# ps -ef | grep nginx
root 9295 1 0 15:24 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 9296 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9297 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9298 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9299 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9300 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9301 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9302 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9303 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9304 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9305 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9306 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9307 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9308 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9309 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9310 9295 0 15:24 ? 00:00:00 nginx: worker process
www-data 9311 9295 0 15:24 ? 00:00:00 nginx: worker process
root 9406 9023 0 15:25 pts/20 00:00:00 grep --color=auto nginx
最後に自分自身にcurlコマンドを打って、htmlのレスポンスが返ってくれば正しく使えています。
curl http://localhost
レスポンスはこんな感じ(nginxデフォルトのindex.html)
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
次にfastcgiをインストールします。
このfastcgiがなぜ必要かというと、本来webサーバ(ここではnginx)は静的なhtmlコンテンツを返す機能だけしかなく、プログラムを実行するような機能は無い物なのです。
そこでその機能を提供するのがfastcgiで、nginxとはtcp or unixソケットを使って実行結果のやり取りをします。
さて本題のインストールです。これもaptで一発です。
apt install fcgiwrap
次にfastcgiが動くようにnginxを設定します。
ここでは新しい設定ファイルをincludeディレクトリに作成します。
cd /etc/nginx/conf.d/
vim bash_cgi.conf
とりあえず動くだけでよければ、以下の設定でOKです
# vim bash_cgi.conf
server {
listen 80;
server_name localhost;
location ~ \.sh$ {
root /usr/share/nginx/html/;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
これで、.shで終わるファイルはすべて実行可能になります。
とりあえずhello world的なcgiを作ります。
vim /usr/share/nginx/html/hello.sh
#!/bin/sh
echo 'Content-type: text/html'
echo ''
### 上記2行がないと動作しない
echo '<html>'
echo '<head></head>'
echo '<body>'
echo '<h1>hello world!!</h1>'
echo '</body>'
echo '</html>'
これでnginxを再起動します。
systemctl restart nginx.service
ここまでで準備は完了です。試しにlocalhostから確認してみます。
curl http://localhost/hello.sh
以下のように結果が返ってくればOKです。
<html>
<head></head>
<body>
<h1>hello world!!</h1>
</body>
</html>
cgiではクエリパラメータ(URLの末尾に?でつなげるやつ)やフォームパラメータ(POSTで渡すやつ)で値を受け取りたいことが多いです。
bashのcgiの場合は以下の方法で受け取ることができます。
変数${QUERY_STRING}に格納されます。
サンプルスクリプト
#!/bin/sh
echo 'Content-type: text/html'
echo ''
echo '<html>'
echo '<head></head>'
echo '<body>'
echo '<h1>hello world!!</h1>'
echo "<p>your parameter is ${QUERY_STRING} .</p>"
echo '</body>'
echo '</html>'
こう実行すると
curl "http://127.0.0.1/query_param.sh?test=1&test2=2"
こういう結果が返ってきます
<html>
<head></head>
<body>
<h1>hello world!!</h1>
<p>your parameter is test=1&test2=2 .</p>
</body>
</html>
標準入力に格納されるらしいです。(あんまり意味がわかってない..)
以下のように`cat`
で取得できます。
#!/bin/sh
echo 'Content-type: text/html'
echo ''
echo '<html>'
echo '<head></head>'
echo '<body>'
echo '<h1>hello world!!</h1>'
echo "<p>your parameter is `cat` .</p>"
echo '</body>'
echo '</html>'
こう実行すると
curl -X POST -d "test=1" -d "test2=2" "http://127.0.0.1/form_param.sh"
こういう結果が返ってきます。
<html>
<head></head>
<body>
<h1>hello world!!</h1>
<p>your parameter is test=1&test2=2 .</p>
</body>
</html>
以上となります。
スポンサーリンク