FLYING

/* TODO: 気の利いた説明を書く */

ドキュメントルートに配置したCGIを実行する

Apacheのデフォルトだと,ScriptAliasディレクティブ*1でドキュメントルートとは別のディレクトリにまとめて置く設定になっています。これを,ドキュメントルート以下に置いてある拡張子が「cgi」のファイルをCGIスクリプトとして実行するように設定を書き換えます。

Apacheの設定ファイルを編集します。

$ sudo vi /etc/httpd/conf/httpd.conf

次のように各項目を設定します。

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/home/tondol/public_html">

#
# 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.2/mod/core.html#options
# for more information.
#
    Options FollowSymLinks ExecCGI

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride All

#
# Controls who can get stuff from this server.
#
    Order allow,deny
    Allow from all

</Directory>

#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname 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/ "/var/www/cgi-bin/"

#
# 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

Apacheを再起動します。

$ sudo /etc/init.d/httpd restart

試しにRubyスクリプトCGIとして動かしてみます。

$ cd ~/public_html
$ vi test.cgi
#!/usr/local/bin/ruby
# -*- encoding: utf-8 *-*

print "Content-Type: text/plain\n\n"
print "Ruby worked!\n"
$ chmod 755 test.cgi

ブラウザで作成したCGIスクリプトにアクセスして,「Ruby worked!」と表示されたら成功です!

*1:ScriptAliasで設定されたディレクトリは拡張子に関わらずCGIスクリプトとして実行されます。それ以外は通常のAliasディレクティブと同じです