環境構築(apache2 ドキュメントルートでCGIを動かそう)

apache2の標準設定ではCGIは「/cgi-bin」配下で動作します。
これをドキュメントルート配下全てのディレクトリでCGIが動くように変更しましょう。

httpd.conf編集(C:\apache2\conf\httpd.conf)

1.ドキュメントルートのディレクトリ設定を変更します。

httpd.confを開くと以下のようなコメントがいっぱいのディレクトリ設定のブロックがあります。
#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "C:/apache2/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.2/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks
    #
    # 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 None
 
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
 
</Directory>
 

この中の、「Options」に「ExecCGI」を追加します。
Options Indexes FollowSymLinks
↓
Options Indexes FollowSymLinks ExecCGI
 

2.AddHandlerの設定

「/cgi-bin」でCGIを動かす分には不要ですが、ドキュメントルートに「ExecCGI」を追加して動かす場合、AddHandlerの設定が必要です。
以下のような記述をhttpd.confから探して、コメントを解除します。
<IfModule mime_module>
省略
    # AddHandler cgi-script .cgi
省略
<IfModule mime_module>
 

こんな感じ
# AddHandler cgi-script .cgi
↓
AddHandler cgi-script .cgi
 

「.rb」ファイルでrubyのCGIを動かしたい場合は以下のようにするとよいです。
AddHandler cgi-script .cgi .rb
 

apache2再起動

httpd.confの設定が終わったら、apache2を再起動し「http://localhost/hello.cgi」にアクセスするとCGIが動く筈です。
※「C:\apache2\htdocs」に前回作った「hello2.cgi」をコピーしました。


設定が上手くできてない場合、以下の様な画面が出ると思います。
その場合は…がんばれ!
最終更新:2012年11月11日 23:41