Kohana の application ディクレクトリと system ディレクトリ を Apache の DocumentRoot から外す

この作業を行うのは、セキュリティ上の理由から実施する。
Apache の DocumentRoot に Kohana の application や system ディレクトリがあるということは、不特定多数の人がそれらのディレクトリにアクセスできることを表す。このことは、例えばデータベースへの接続設定が思わぬところから外部へ流出する可能性があることを意味する。
そのため、Kohana で作成したアプリケーションを公開するときは、application や system ディレクトリを移動するべきである。

application や system ディレクトリを移動する

  • Apache の DocumentRoot を /var/www/public_html とする。
  • Kohana の初期設置場所は、/var/www/public_html/myapplication とする。
  • Kohana の application や system ディレクトリの移動先を /home/littlebuddha/kohana とする。

詳細な内容は、Kohana のドキュメントを参照。
ディレクトリは、適宜、自分の環境と置き換える。

% cd /var/www/public_html/myapplication
% ls -l
total 8
drwxr-xr-x   11 littlebuddha  littlebuddha   374  5  7 23:19 application
-rwxr-xr-x    1 littlebuddha  littlebuddha  2762  2  6 17:35 index.php
drwxr-xr-x    5 littlebuddha  littlebuddha   170  2  6 17:35 modules
drwxr-xr-x   10 littlebuddha  littlebuddha   340  5  8 01:14 system

application や system ディレクトリを /home/littlebuddha/kohana に移動する。

% cd /home/littlebuddha/kohana
% ls -l
total 0
drwxr-xr-x   11 littlebuddha  littlebuddha  374  5  7 23:19 application
drwxr-xr-x   10 littlebuddha  littlebuddha  340  5  8 01:14 system

index.php に application や system ディレクトリの場所を記述する

/var/www/public_html/myapplication/index.php に下記の設定を記述する。

変更前
/**
 * Kohana website application directory. This directory should contain your
 * application configuration, controllers, models, views, and other resources.
 *
 * This path can be absolute or relative to this file.
 */
$kohana_application = 'application';
変更後
/**
 * Kohana website application directory. This directory should contain your
 * application configuration, controllers, models, views, and other resources.
 *
 * This path can be absolute or relative to this file.
 */
$kohana_application = '/home/littlebuddha/kohana/application'; // 設置パスを追加する
変更前
/**
 * Kohana package files. This directory should contain the core/ directory, and
 * the resources you included in your download of Kohana.
 *
 * This path can be absolute or relative to this file.
 */
$kohana_system = 'system';
変更後
/**
 * Kohana package files. This directory should contain the core/ directory, and
 * the resources you included in your download of Kohana.
 *
 * This path can be absolute or relative to this file.
 */
$kohana_system = '/home/littlebuddha/kohana/system'; // 設置パスを追加する

設定変更後に、http://localhost/myapplication/ にアクセスをして、正常に表示されるか確認をする。

modules ディレクトリについて

application や system ディレクトリを移動したが、modules ディレクトリはどうするのか?

a% ls -l /var/www/public_html/myapplication
total 8
-rwxr-xr-x   1 littlebuddha  littlebuddha  2806  5 10 12:32 index.php
drwxr-xr-x   5 littlebuddha  littlebuddha   170  2  6 17:35 modules

modules ディレクトリも設置場所の変更が実は必要とされている(参照: 3. Move Kohana core directories outside of the document root)。
しかし、index.php には、

$kohana_modules = 'modules';

の記述が見つからない。
調べてみたところ、現在、開発中の Kohana に modules ディレクトリの設定がある。

/**
 * Kohana modules directory. This directory should contain all the modules used
 * by your application. Modules are enabled and disablehttp://d.hatena.ne.jp/images/admin/markup_url.gif[http://d.hatena.ne.jp/littlebuddha/20080510/1210390498:title]d by the application
 * configuration file.
 *
 * This path can be absolute or relative to this file.
 */
$kohana_modules = 'modules';

なので、現在は新規に上記の記述を行っても正しく Kohana が動くのかどうか未確認だ。確認後、結果を更新する。