CakePHP2をLinuxサーバーにインストールする方法についてです。
CakePHPインストールの概要
CentOS:7.3
CakePHP:2.6.7
Apacheで以下のようなバーチャルホストが作られていることを前提にします。
http://www.example.com/test
http://www.example.com/sample
それぞれのドキュメントルートは
/var/www/html/test
/var/www/html/sample
となっています。
CakePHP2をダウンロード→解凍してそのままドキュメントルートにアップロードして使う方法もありますが、セキュリティ上良いとは言えません。今回はCakePHPのコアファイルを別の場所において、それぞれのバーチャルドメインのルートからアクセスして使う方法を記載します。
公式ページCakeBOokの応用インストールのページの一番下「複数のアプリケーションで CakePHP を共有する」のやり方です。
CakePHP 2x Cookbook 応用インストール
下記のように3つの部分に分かれます。
A:すべてのバーチャルホストで使うCakePHPのコアファイル(共通)
B:バーチャルドメインごとのCakePHPアプリケーションファイル
C:バーチャルドメインごとのCakePHP公開ファイル
Aの作業はCakePHPをインストールするときに1回だけ行えばOKです。BとCの作業はバーチャルドメインを増やすごとに発生します。
CakePHPの基本的な動きは
ブラウザで「http://www.example.com/sample」を入力→公開ディレクトリの index.phpからCakePHPのコアファイルとアプリケーションファイルにアクセス。
となっています。公開ディレクトリにあるのは 「index.php」「cssフォルダ」 「jsフォルダ」 程度になります。
CakePHP Coreファイルの準備
CakePHPをダウンロードして解凍して開くと以下のようなディレクトリ・ファイル構成になっています。
lib というフォルダを開くと Cake というフォルダが出てきます。
Cakeの中身がCakePHPのCoreファイルになっています。下記のような構成です。これをこれから作るCakePHPのCore用のディレクトリにアップロードします。
# mkdir /usr/lib/Cake # chmod 777 /usr/lib/Cake/ # chown -R ユーザー名:グループ名 /usr/lib/Cake/
作ったディレクトリ「/usr/lib/Cake/」にファイルをアップロードします。
FTPでアップロードしたファイルを見ると以下のようになっています。
Coreファイルの準備はこれだけです。
アプリケーションファイルの準備
CakePHPのアプリケーションを置くディレクトリを「/home/me/」とします。
まず「/home/me/」のディレクトリを作成します。バーチャルドメインごとにこの中にアプリケーションディレクトリを作っていきます。
# mkdir /home/me # chmod 777 /home/me/ # chown -R ユーザー名:グループ名 /home/me/
今回は「http://www.example.com/sample」で実験を行うので、/home/me/の中に「sample」というアプリケーション用のディレクトリを作成します。
※ディレクトリの名前は任意で構いません。後に公開ディレクトリの index.php でこのディレクトリの名前を設定します。
# mkdir /home/me/sample # chmod 777 /home/me/sample/ # chown -R ユーザー名:グループ名 /home/me/sample/
CakePHPの「app」の中身を「/home/me/sample/」にFTPでアップロードします。
「app」の中身は以下のようになっています。
これでアプリケーションの準備が終わりました。
公開ディレクトリの準備
CakePHPの「app」フォルダの中に「webroot」というフォルダがあります。これが公開用ファイルになっています。
これを公開ディレクトリ「/var/www/html/sample」にFTPでアップロードします。(公開ディレクトリは自分のパスに合わせてください)
これで公開ディレクトリの準備が終わりました。
まだ、設定をしていないので、ブラウザで見ても何も表示されないか、エラーになります。
設定
公開フォルダにある「.htaccess」の中身を消去し、下記のものに入れ替えます。
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] </IfModule>
公開フォルダにある「index.php」を変更します。
/** * Use the DS to separate the directories in other defines */ if (!defined('DS')) { define('DS', DIRECTORY_SEPARATOR); } /** * These defines should only be edited if you have CakePHP installed in * a directory layout other than the way it is distributed. * When using custom settings be sure to use the DS and do not add a trailing DS. */ /** * The full path to the directory which holds "app", WITHOUT a trailing DS. * */ if (!defined('ROOT')) { define('ROOT', dirname(dirname(dirname(__FILE__)))); } /** * The actual directory name for the "app". * */ if (!defined('APP_DIR')) { define('APP_DIR', basename(dirname(dirname(__FILE__)))); } /** * The absolute path to the "cake" directory, WITHOUT a trailing DS. * * Un-comment this line to specify a fixed path to CakePHP. * This should point at the directory containing `Cake`. * * For ease of development CakePHP uses PHP's include_path. If you * cannot modify your include_path set this value. * * Leaving this constant undefined will result in it being defined in Cake/bootstrap.php * * The following line differs from its sibling * /lib/Cake/Console/Templates/skel/webroot/index.php */ //define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'lib');
変更点は3か所になります。
[注意点]
5行目:CakePHPのindex.phpファイルではディレクトリの区切りに「/」ではなく「DS」を使うという設定です。
パスを書くときは「/home/me/sample」ではなく「DS.’home’.DS.’me’.DS.’sample’」になります。
[変更点1]
19行目:アプリケーションのルートディレクトリの設定です。
今回は「/home/me」としたので、「 define(‘ROOT’, DS . ‘home’ . DS . ‘me’);」に変更します。
[変更点2]
27行目:アプリケーションディレクトリの設定です。
今回は「home/me/sample」としたので、「define(‘APP_DIR’, ‘sample’);」に変更します。
「/home/me」は変更点1で設定してあるので、ここでは「sample」というディレクトリだけを指定します。
[変更点3]
44行目:CakePHPのコアファイルのパスを指定。
先頭の「//」を外し、以下のように変更します。
「define(‘CAKE_CORE_INCLUDE_PATH’, DS . ‘usr’. DS . ‘lib’);」
※デフォルトでは「define(‘CAKE_CORE_INCLUDE_PATH’, ROOT . DS . ‘lib’);」とROOTとい文字が入っています。これは公開ディレクトリにコアファイルを置いた時の設定になるので、ROOTを「usr」に変えるようにしましょう。
/** * The full path to the directory which holds "app", WITHOUT a trailing DS. * */ if (!defined('ROOT')) { define('ROOT', DS . 'home' . DS . 'me'); } /** * The actual directory name for the "app". * */ if (!defined('APP_DIR')) { define('APP_DIR', 'sample'); } /** * The absolute path to the "cake" directory, WITHOUT a trailing DS. * * Un-comment this line to specify a fixed path to CakePHP. * This should point at the directory containing `Cake`. * * For ease of development CakePHP uses PHP's include_path. If you * cannot modify your include_path set this value. * * Leaving this constant undefined will result in it being defined in Cake/bootstrap.php * * The following line differs from its sibling * /lib/Cake/Console/Templates/skel/webroot/index.php */ define('CAKE_CORE_INCLUDE_PATH', DS . 'usr'. DS . 'lib');
詳細設定
キャッシュディレクトリのパーミッション設定
上記の状態でブラウザでアクセスしてみて次のようなメッセージがでたら、順調に設定が進んでいます。
このメッセージはキャッシュで使うディレクトリのパーミッションを変更してくださいというものです。
Warning: _cake_core_ cache was unable to write 'cake_dev_ja' to File cache in /usr/lib/Cake/Cache/Cache.php on line 328 Warning: /home/me/sample/tmp/cache/persistent/ is not writable in /usr/lib/Cake/Cache/Engine/FileEngine.php on line 385 Fatal error: Uncaught exception 'CacheException' with message 'Cache engine "_cake_core_" is not properly configured. Ensure required extensions are installed, and credentials/permissions are correct' in /usr/lib/Cake/Cache/Cache.php:186 Stack trace: #0 /usr/lib/Cake/Cache/Cache.php(151): Cache::_buildEngine('_cake_core_') #1 /home/me/sample/Config/core.php(374): Cache::config('_cake_core_', Array) #2 /usr/lib/Cake/Core/Configure.php(72): include('/home/me/sample...') #3 /usr/lib/Cake/bootstrap.php(175): Configure::bootstrap(true) #4 /var/www/html/sample/index.php(104): include('/usr/lib/Cake/b...') #5 {main} thrown in /usr/lib/Cake/Cache/Cache.php on line 186
ターミナルから以下コマンドを実行します。
# chmod 777 -R /home/me/sample
再度ブラウザでアクセスしてみてください。以下のような画面が表示されます。
赤と黄色の帯の部分を対処して緑色に変えていきます。(設定がうまくいくと緑色になります)
データベースの設定
アプリケーションフォルダの中のConfigディレクトリ内に「database.php.default」というファイルがあります。パスは「/home/me/sample/Config/database.php.default」です。
↓アプリケーションのConfigフォルダ内の構成
これをダウンロードして内容を編集します。
class DATABASE_CONFIG { public $default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'user', 'password' => 'password', 'database' => 'database_name', 'prefix' => '', //'encoding' => 'utf8', ); public $test = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'user', 'password' => 'password', 'database' => 'test_database_name', 'prefix' => '', //'encoding' => 'utf8', );
ファイルの下のほうに上記のような記載があります。ここの public $default 部分がデータベースの設定になります。ウェブサーバーと出たベースサーバーが同じ場合は下記の3か所の設定をすればOKです。
「login」「password」「database」
この3つを変更したらファイル名を「database.php」に変更してアプリケーションのConfigフォルダにアップロードしましょう。
ブラウザでページを見たときに先ほどまで黄色い表示だった
Your database configuration file is NOT present.
Rename APP/Config/database.php.default to APP/Config/database.php
が緑に変更されていればデータベースの設定はOKです。
Securityの設定
後は赤い2つのNoticeが消えるようにします。ページの一番上に2つ出ています。
Notice (1024): Please change the value of ‘Security.salt’ in APP/Config/core.php to a salt value specific to your application.
これらは、アプリケーションのConfigディレクトリ内にあるcore.phpファイルを編集します。
/** * A random string used in security hashing methods. */ Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi'); /** * A random numeric string (digits only) used to encrypt/decrypt strings. */ Configure::write('Security.cipherSeed', '76859309657453542496749683645')
2か所変更点があります。
4行目:「DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi」を半角ローマ字・半角数字を含む任意の文字列に変更
9行目:「76859309657453542496749683645」を任意の半角数字に変更。
変更後アプリケーションのCOnfigディレクトリ内にアップロードします。
デバッグツールの「DebugKit」は使わない人もいるので、使うときに設定すればよいと思います。
エラー対処
ブラウザで開いたときにエラーが出る場合、何も表示されない場合などはapachのログを見てみます。
ターミナルから下記コマンドを実行
# less /var/log/httpd/error_log
「Ctrl + G」で最終行を見ると、最新のアクセスに対するエラーが出ています。
「.htaccess」が使えないとCakePHPが動かないので、.htaccessが有効かどうかベーシック認証などを行ってみて確認します。