お知らせ

  • 利用規約を守って投稿してください。また、よくある質問および投稿の手引きも参照してください。
  • メッセージの投稿にはアカウントが必要です。未登録の方は、ユーザ登録ページからアカウントを作成することができます。

#1 2011-12-31 11:27:03

Akira Imakura
メンバ
From: 徳島
登録日: 2007-11-17

CGIについて

ubuntu 10.04 を使っています。次のようなcgiファイルをhello1.cgiと名づけ、/usr/lib/cgi-binに保存しました。

#!/usr/bin/perl -T
use strict;
use warnings;
use CGI qw(:standard);

print header;
print start_html('Hello');
print h1('This is hello.cgi');
print end_html;

ブラウザを起動し、アドレスにhttp://localhost/cgi-bin/hello1.cgiを記述して、表示しようとしましたが、

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
Apache/2.2.14 (Ubuntu) Server at localhost Port 80

という表示が出ます。CGIを使うためには、Apacheの設定の変更が必要なのでしょうか。あるいは、#!/usr/bin/perl -T の記述が、間違っているのでしょうか。なおApacheの設定変更は、/etc/apache2/sites-enabled/000-defaultのDocumentRootを変更しているだけです。

オフライン

 

#2 2011-12-31 17:06:16

si
メンバ
From: hokkaido kitami, jp
登録日: 2007-01-15

Re: CGIについて

この辺を見てください: https://forums.ubuntulinux.jp/viewtopic.php?id=4565
検索すれば、同様の事柄出てますので、大概の事は解ると思いますよ。

オフライン

 

#3 2011-12-31 20:17:50

himajin216
メンバ
登録日: 2011-09-18

Re: CGIについて

Internal Server Error の原因の大半は

1.プログラムの文法間違い
2.実行権限のつけ忘れ

ですが、hello1.cgi に実行権限をつけましたか?

オフライン

 

#4 2011-12-31 21:23:29

zagan
メンバ
登録日: 2008-08-12

Re: CGIについて

クライアント側は以下のサイトとか参考になりませんか?(読み込んではないです)
CGIセッティングの7ヶ条

サーバー側ですが
1. apache2用perlモジュールがインストールされているかどうか
  libapache2-mod-perl2 だっけかな?
2. /etc/apache2/mods-enabled/mime.conf
  AddHandler cgi-scriptのエントリに.cgiや.plが追記されてるかどうかを確認

Apache Tutorial: CGI による動的コンテンツとか一番参考になるサイトです。

オフライン

 

#5 2011-12-31 21:25:59

zagan
メンバ
登録日: 2008-08-12

Re: CGIについて

途中で送信してしまいました。

特にディレクティブでのOptions +ExecCGIの記述を忘れないようにしましょう。

って書く前にプレビュー押したつもりが。。。

オフライン

 

#6 2012-01-02 09:30:24

Akira Imakura
メンバ
From: 徳島
登録日: 2007-11-17

Re: CGIについて

/etc/apache2/sites-available/default には、下記の記述がありますので、この設定はできているようです。

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

/etc/apache2/mods-enabled/mime.conf の
#AddHandler cgi-script .cgi
の#をはずして保存し、再起動して、URLにhttp://localhost/cgi-bin/hello1.cgiを指定してみましたが、やはり

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.14 with Suhosin-Patch Server at localhost Port 80

と表示されます。

オフライン

 

#7 2012-01-02 09:58:30

zagan
メンバ
登録日: 2008-08-12

Re: CGIについて

Akira Imakuraさん による投稿:

http://localhost/cgi-bin/hello1.cgi

このアドレスだと、/var/www直下にDocumentRootを指定している。つまりデフォルトの設定と同じなんですが.
#1で書かれている内容と矛盾しませんか?

Akira Imakuraさん による投稿:

/etc/apache2/sites-enabled/000-defaultのDocumentRootを変更しているだけです。

オフライン

 

#8 2012-01-02 11:07:52

si
メンバ
From: hokkaido kitami, jp
登録日: 2007-01-15

Re: CGIについて

まずは
* /var/log 以下の apache ログを見る
* -T が効くのは、suEXEC が動く場合と、書いてあるページがあるようなので、これを取ってみる
* 実行権が付いているか、余計な権限が付いてないか、確認する
* 標準設定の /var/www/cgi-bin に置いてみる
を試してみては?

オフライン

 

#9 2012-01-03 11:22:52

Akira Imakura
メンバ
From: 徳島
登録日: 2007-11-17

Re: CGIについて

/etc/apache2/sites-enabled/000-defaultはデフォルトでは

NameVirtualHost *
<VirtualHost *>
    ServerAdmin webmaster@localhost
   
    DocumentRoot /var/www/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

となっています。
/var/wwwを/home/shiken/docに変えた場合、/home/shiken/docにcgi-binディレクトリをつくり、/usr/lib/cgi-bin/の所も変えておく必要があるのですね。
NameVirtualHost *
<VirtualHost *>
    ServerAdmin webmaster@localhost
   
    DocumentRoot /home/shiken/doc/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /home/shiken/doc/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /home/shiken/doc/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

と変更すると、URLにhttp://localhost/cgi-bin/hello1.cgiを指定して、正常に表示されました。
/var/www/のデフォルトのままの設定でも、、URLにhttp://localhost/cgi-bin/hello1.cgiを指定すると、正常に表示されまません。ScriptAlias /cgi-bin/ /var/www/cgi-bin/ と変更する必要があるようです。デフォルトのScriptAlias /cgi-bin/ /usr/lib/cgi-bin/のままで、cgiファイルを正常に表示させるには、URLをどのように指定したらいいのでしょうか。

オフライン

 

#10 2012-01-03 13:20:13

si
メンバ
From: hokkaido kitami, jp
登録日: 2007-01-15

Re: CGIについて

logは見ましたか?
一つ忘れてましたが、apparmorの設定はどうなってますか?
(sudo aa-status で設定状況が分かり、/etc/apparmor.d 以下に設定ファイルがあります)

オフライン

 

#11 2012-01-03 17:50:44

Akira Imakura
メンバ
From: 徳島
登録日: 2007-11-17

Re: CGIについて

現在正月で家に帰っており、手持ちのubuntu 8.04のパソコンで見ています。
siさんの言うログは/var/log/apache2/access.logの記述のことでしょうか。
またapparmorのようなセキュリティに関する情報をインターネット上で公開するのは、少し気が引けます。

オフライン

 

#12 2012-01-04 02:10:04

si
メンバ
From: hokkaido kitami, jp
登録日: 2007-01-15

Re: CGIについて

Akira Imakura による投稿:

現在正月で家に帰っており、手持ちのubuntu 8.04のパソコンで見ています。
siさんの言うログは/var/log/apache2/access.logの記述のことでしょうか。
またapparmorのようなセキュリティに関する情報をインターネット上で公開するのは、少し気が引けます。

access.logとエラーログ、必要によっては、セキュアログです。
ubuntuでの web サーバは、かつて(8.04server)、1度しか設定したことが無く、最近の設定は分からないので、apparmorの設定を確認してみては? ということを聞きました。

オフライン

 

#13 2012-01-04 10:27:52

Akira Imakura
メンバ
From: 徳島
登録日: 2007-11-17

Re: CGIについて

ScriptAlias /cgi-bin/ /home/shiken/doc/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
と、書くことで、正常に表示されていたのですが、新しいcgiファイルを/home/shiken/doc/cgi-bin/に置くと、読めないことがわかりました。/usr/lib/cgi-binを残していたのが、よくないのだろうと思い、
    ScriptAlias /cgi-bin/ /home/shiken/doc/cgi-bin/
    <Directory "/home/shiken/doc/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
と書き換えてみましたが、やはり、
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.14 with Suhosin-Patch Server at localhost Port 80
が表示されて、正常に表示できません。

http://www.apache.jp/manual/howto/cgi.htmlには、
ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/
/cgi-bin/  で始まるリソースへのあらゆるリクエストに対して、ディレクトリ /usr/local/apache2/cgi-bin/ から提供し、それらを CGI プログラムとして扱うよう Apache に示します。
例えば、URL http://www.example.com/cgi-bin/test.pl が要求された場合、Apache は ファイル /usr/local/apache2/cgi-bin/test.pl を実行し、その出力を返すことを試みます。

と書いてあります。この説明の通りなら、デフォルトの
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
で、URLにhttp://localhost/cgi-bin/hello1.cgを指定すると、Apacheは/usr/lib/cgi-bin/に置いたhello1.cgiを読むはずだと思うのですが、読みませんね。

現在
    ScriptAlias /cgi-bin/ /home/shiken/doc/cgi-bin/
    <Directory "/home/shiken/doc/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
の設定で、以前につくったhello1.cgiは正常に表示されるのですが、hello1.cgiのコードをコピーして新しく、hello2.cgiと名付け、/home/shiken/doc/cgi-bin/に置くと、正常に表示できない状態です。どこが問題なのでしょうか。

オフライン

 

#14 2012-01-04 11:36:32

si
メンバ
From: hokkaido kitami, jp
登録日: 2007-01-15

Re: CGIについて

>ScriptAlias /cgi-bin/ /home/shiken/doc/cgi-bin/
>    <Directory "/home/shiken/doc/cgi-bin">
上記のような一般的な作法から大きく外れたことには、止めたほうが良いように思います。
webサーバのように、公開が前提で、セキュリティを考慮した標準設定があるものは、素直にそのガイドに従ったほうが安全です。

オフライン

 

#15 2012-01-05 11:58:30

jackalope
メンバ
登録日: 2009-01-01

Re: CGIについて

#4のApache Tutorial...のリンク先を読んでの感想。

#2のリンク先の情報は古すぎて現状と合わないので参考にしない。
#4の2.はScriptAliaseで指定したディレクトリ以外でCGIを使う時の物ですので現状の設定では無関係。
#7は投稿者は何かを勘違いしているので無視。
#8の「標準設定の /var/www/cgi-bin に置いてみる」/var/www/cgi-binは標準ではないので無視。
#14の「上記のような一般的な作法から大きく外れたこと...」は勘違い?

ここから本題です。
>hello1.cgiのコードをコピーして新しく、hello2.cgiと名付け、/home/shiken/doc/cgi-bin/に置くと、正常に表示できない状態です。どこが問題なのでしょうか。

#3,#8で指摘されているようにファイルのパーミッション設定が原因でしょう。
下記のコマンドを試してください。

コード:

sudo chown www-data:www-data /home/shiken/doc/cgi-bin/hello2.cgi
sudo chmod 700 /home/shiken/doc/cgi-bin/hello2.cgi

これでも期待した結果が得られないのであれば、現在の/etc/apache2/sites-enabled/000-defaultの内容とlocate hello2.cgiの結果を投稿してください。

オフライン

 

#16 2012-01-11 20:14:05

Akira Imakura
メンバ
From: 徳島
登録日: 2007-11-17

Re: CGIについて

正月明けはたまった仕事で忙しく、このサイトを見る暇もありませんでした。1月7日からの連休になり、またCGIのことをしてみようかと思い、このサイトを開けようとすると、サーバー障害でつながりませんでした。googleで検索していると、http://ameblo.jp/tad2v/entry-10536462015.htmlのサイトにパーミッションを変更するように、書いてあったので、次のコードでパーミッションを変えてみました。すると正常に表示されました。

sudo chmod 777 /home/shiken/doc/cgi-bin/
sudo chmod 755 /home/shiken/doc/cgi-bin/hello1.cgi

今日このサイトを見ると、jackalopeさんも同じことを指摘していました。パーミッションの問題であったようです。

オフライン

 

Board footer

Powered by FluxBB