CGI(Perl)を利用するためのApacheの設定

XAMPP をインストールすると Perl の実行環境もインストールされ、 Apache で CGI が利用できるように設定が行われます。実際に Apache 経由で CGI を利用するために、デフォルトでどのような設定になっているのかを確認します。

(Last modified: )

CGIに関する設定を確認する

Apache における CGI に関連する設定は Apache の設定ファイルである httpd.conf ファイルにて行います。 httpd.conf ファイルは、 XAMPP をインストールしたディレクトリの下にある c:\xampp\apache\conf\httpd.conf にあります。

CGIに関する設定を確認する(1)

httpd.conf ファイルはテキストファイルですので、テキストエディタを使って開いたり、編集して保存することができます。

CGIに関する設定を確認する(2)

なお httpd.conf ファイルは XAMPP コントールパネルからも開くことができます。詳しくは「Apacheの設定ファイル(httpd.conf)を編集する」を参照されてください。

それでは httpd.conf ファイルの中の CGI に関連する設定項目を見ていきます。

Options ExecCGI

最初に Options ExecCGI です。指定したディレクトリで CGI を実行できるようにするには、対象のディレクトリで Options ExecCGI を記述します。

DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks Includes ExecCGI
</Directory>

Apache のドキュメントルートである C:/xampp/htdocs ディレクトリに対して Options ExecCGI が記述されています。よって、ドキュメントルート以下のすべてのディレクトリで CGI を実行することが可能です。

※ 詳しくは「ドキュメントルート以下の任意のディレクトリでCGIを実行する」を参照されてください。

AddHandler cgi-script .cgi .pl .asp

次に AddHandler ディレクティブを使ってどの拡張子のファイルが CGI として実行されるのかを指定します。

<IfModule mime_module>
    #
    # AddHandler allows you to map certain file extensions to "handlers":
    # actions unrelated to filetype. These can be either built into the server
    # or added with the Action directive (see below)
    #
    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    #
    AddHandler cgi-script .cgi .pl .asp
</IfModule>

拡張子が .cgi 、 .pl 、 .asp のファイルへのアクセスがあった場合に、 CGI として実行されます。

ScriptAlias

次に ScriptAlias ディレクティブを使って /cgi-bin/ へのアクセスがあった場合に、指定したディレクトリにあるファイルが実行されるように設定します。

<IfModule alias_module>
    #
    # ScriptAlias: This controls which directories contain server scripts. 
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the target directory are treated as applications and
    # run by the server when requested rather than as documents sent to the
    # client.  The same rules about trailing "/" apply to ScriptAlias
    # directives as to Alias.
    #
    ScriptAlias /cgi-bin/ "C:/xampp/cgi-bin/"
</IfModule>

ScriptAcliasによって /cgi-bin/C:/xampp/cgi-bin/ にマッピングされると同時にこのディレクトリでの CGI の実行が許可されます。なおマッピングとは http://localhost/cgi-bin/test.cgi へアクセスがあった場合に C:/xampp/cgi-bin/test.cgi を実行するという意味です。

このディレクトリ内にあるファイルは mod_cgi の cgi-script ハンドラで処理されます(つまり拡張子に関係なく CGI として処理されます)。

以上が Apache の httpd.conf ファイルの中で CGI に関係する設定項目です。 CGI で使用するプログラミング言語として Perl を利用する場合は、 Perl で記述されたプログラムファイルの拡張子を .cgi や .pl とするか、または C:/xampp/cgi-bin/ ディレクトリに配置するようにして下さい。

-- --

Apache の httpd.conf ファイルの中で、デフォルトで設定されている CGI に関する設定を確認しました。

( Written by Tatsuo Ikura )

プロフィール画像

著者 / TATSUO IKURA

これから IT 関連の知識を学ばれる方を対象に、色々な言語でのプログラミング方法や関連する技術、開発環境構築などに関する解説サイトを運営しています。