CGI利用に関するApacheの設定
現在利用しているWindows用のXAMPPのバージョンではPerlは「Strawberry Perl 7.0.56 Portable」というものが利用できるようになっています。CGIプログラムをApache上で実行させる場合にPerlを利用したりしますが、まずはCGIに関する設定がどのようになっているのかを確認します。
Apacheの設定ファイルである「http.conf」ファイルは「(XAMPPインストールディレクトリ)\apache\conf\」に含まれています。
「http.conf」ファイルはテキストファイルですのでテキストエディタで開き、「Options FollowSymLinks Includes 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 FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
「C:/xampp/htdocs」はApacheのドキュメントルートです。ここではドキュメントルート以下のディレクトリに対して「ExecCGI」を設定しており、ドキュメントルート以下のすべてのディレクトリでCGIが実行できるように設定しています。(詳しくは「特定のディレクトリでCGIを実行する(Option, AddHandler)」を参照して下さい)。
次に「AddHandler cgi-script .cgi .pl .asp」で検索して下さい。次のような記述が見つかります。
<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>
「ExecCGI」が許可されたディレクトリの中で、どのような拡張子のファイルがCGIのファイルなのかを指定しています。ここでは拡張子が「.cgi」「.pl」「.asp」のファイルをCGIとして指定しています。
次に「ScriptAlias /cgi-bin/」で検索して下さい。次のような記述が見つかります。
<IfModule alias_module> # # Redirect: Allows you to tell clients about documents that used to # exist in your server's namespace, but do not anymore. The client # will make a new request for the document at its new location. # Example: # Redirect permanent /foo http://www.example.com/bar # # Alias: Maps web paths into filesystem paths and is used to # access content that does not live under the DocumentRoot. # Example: # Alias /webpath /full/filesystem/path # # If you include a trailing / on /webpath then the server will # require it to be present in the URL. You will also likely # need to provide asection to allow access to # the filesystem path. # # 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として処理されます)。
次に先ほどの記述のすぐ下あたりに次のような記述があります。
# # "C:/xampp/cgi-bin" should be changed to whatever your ScriptAliased # CGI directory exists, if you have that configured. # <Directory "C:/xampp/cgi-bin"> AllowOverride All Options None Require all granted </Directory>
先ほどScriptAcliasによってマッピングの対象となった「C:/xampp/cgi-bin/」に対しての設定です。
まず「AllowOverride None」によってこのディレクトリに含まれるサブディレクトリでは設定変更できなくしています。「Options None」で特別な機能は全て無効となります(ただしScriptAcliasによって指定されていますのでCGIは実行できます)。そして「Require all granted」で全てのアクセスを許可しています。
CGIプログラムとしてPerlを利用する場合は、Perlで記述されたプログラムファイルの拡張子を「.cgi」や「.pl」とするか、または「C:/xampp/cgi-bin/」ディレクトリに配置するようにして下さい。
( Written by Tatsuo Ikura )