perl

Perlでドハマりなう

症状

****.cgi ファイルをブラウザで実行時、Interal Error が出る。

test.cgi

#! /usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><body>";
print "<p>";
print "Hello World.";
print "</p>";
print "</body></html>";

まずはお馴染み、 Hello World から

  • /usr/bin/perlperl のパス
  • /user/local/bin/perl といったパスの時もあるらしい
  • #! はperlの1行目に書くshebang(シェバン、シェバング)と呼ばれるもの
  • acache のエラーログ

    [Tue Apr 16 00:30:52 2013] [error] [client 192.168.11.3] (2)No such file or directory: exec of '/var/www/vhosts/webapp/test.cgi' failed
    [Tue Apr 16 00:30:52 2013] [error] [client 192.168.11.3] Premature end of script headers: test.cgi
    Premature end of script headers: test.cgi

    スクリプトヘッダ途中終了の意
    エラー範囲がおおまかすぎて、原因を見つけるのが難しいらしい

    (2)No such file or directory: exec of '/var/www/vhosts/webapp/test.cgi' failed

    どうやら調べてみるとまたもや .htaccesshttpd.conf の設定をしないといけないみたい
    今回も .htaccess でアプローチ

    Options +ExecCGI
    AddType text/html cgi
    AddHandler cgi-script cgi

    .htaccess ファイルを作り、そこに上記のコードを記述したら無事動いてくれました。
    他にもftp経由での移動時、改行コードが変換されたりすると出るらしい

    shebang の修復、設定

    .htaccess

    #! /usr/bin/perl --

    --(ハイフン2つ)がミソらしい。