Apache CGI を有効にする

2010/12/21
★★

Apahce 名前ベースのヴァーチャルホスト (name-based virtual hosting)に引き続き、Apache で CGI を有効にする方法を紹介する。

[root@centkun ~]# vi /etc/httpd/conf/httpd.conf

拡張子とハンドラーを関連付ける。
(before) #AddHandler cgi-script .cgi
(after) AddHandler cgi-script .cgi

CGI スクリプトの実行を許可する。以下の設定は、すべてのディレクトリ "/" 以下に CGI スクリプトの実行を許可している。
(before)
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
(after)
<Directory />
    Options FollowSymLinks ExecCGI
    AllowOverride None
</Directory>

Apahce 名前ベースのヴァーチャルホスト (name-based virtual hosting) で紹介した以下のヴァーチャルホストに対してのみ CGI スクリプトの実行を許可する方法を紹介する。通常は、このように必要な個所にだけ、局所的に有効することが望まれる。この場合、"/" 以下の CGI スクリプトの実行を許可の定義をしなくても動作する。

<VirtualHost 192.168.100.13>
 ServerName dot-net.masudaq.com
 DocumentRoot /www/docs/dot-net.masudaq.com
 ServerAdmin webmaster@dot-net.masudaq.com
 ErrorLog logs/host.dot-net.masudaq.com-error_log
 TransferLog logs/host.dot-net.masudaq.com-access_log
 </VirtualHost>

上記のヴァーチャルホストに対して、つまり、"/www/docs/dot-net.masudaq.com" に対してのみ CGI の実行を許可するには、以下の定義を新規に追加する。"/www/docs/dot-net.masudaq.com" がキーになっている。また、デフォルトページを優先順に、"index.htm"、"index.cgi" としている。
(new)
<Directory /www/docs/dot-net.masudaq.com>
 Options ExecCGI
 DirectoryIndex index.htm index.cgi
</Directory>

加えて、シンボリックリンク、SSI を有効にするには、Options ディレクティブ にそれぞれ、FollowSymLinks、Includes を指定する。
<Directory /www/docs/dot-net.masudaq.com>
 Options ExecCGI FollowSymLinks Includes
 DirectoryIndex index.htm index.cgi
</Directory>

以上で設定は完了。次にテストを実施する。

テスト用ページを作成。

[root@centkun ~]# mkdir /www/docs/dot-net.masudaq.com/java
[root@centkun ~]# vi /www/docs/dot-net.masudaq.com/java/index.cgi

#!/usr/bin/perl print

"Location:http://java.masudaq.com/\n\n";
exit;

上記は、"http://dot-net.masudaq.com/java" にアクセスすると、"http://java.masudaq.com/" へリダイレクトする CGI スクリプトの例。

index.cgi ファイルを保存する。

index.cgi に実行権限を付与する。

[root@centkun ~]# chmod o=rx /www/docs/dot-net.masudaq.com/java/index.cgi

httpd デーモンを再起動する。
[root@centkun ~]# /etc/rc.d/init.d/httpd restart

"http://dot-net.masudaq.com/java" にアクセスし、"http://java.masudaq.com/" へリダイレクトする事を確認する。