Yiiで作ったサイトを公開する際にしたことをまとめておきます。
- Yiiのディレクトリ構造を変更する
- 設定ファイルを分割する
- 開発環境と公開環境で違う設定ファイルを利用する
Yiiのディレクトリ構造を変更する
Yiiのフレームワークだとか、protected以下のファイルは公開ディレクトリ以下に設置したくないので、以下のようにディレクトリ構造を変更します。
- 公開ディレクトリ(/var/www/html)には、protectedディレクトリ以外のディレクトリやファイルを設置する。
- /var/www/yii/example以下にprotectedディレクトリを設置する。
- /var/www/yii/framework以下にはYiiのフレームワークを設置する。
修正するファイルは、index.php、index-test.phpとprotected/yiic.phpの3つ。
//index.phpの修正部分 $yii=dirname(__FILE__).'/../yii/framework/yii.php'; $config=dirname(__FILE__).'/../yii/example/protected/config/main.php'; //index-text.phpの修正部分 $yii=dirname(__FILE__).'/../yii/framework/yii.php'; $config=dirname(__FILE__).'/../yii/example/protected/config/test.php'; //protected/yiic.phpの修正部分 $yiic=dirname(__FILE__).'/../../framework/yiic.php'; $config=dirname(__FILE__).'/config/console.php';
設定ファイルを分割する
protected/config/main.phpのparamで設定している項目については、他の設定ファイルでも利用するため、paramの項目については、別ファイルで管理し、このファイルを設定ファイルから読み込むようにします。
protected/config/main.phpやprotected/config/console.phpなどは以下のように修正します。
// application-level parameters that can be accessed // using Yii::app()->params['paramName'] 'params'=>require(dirname(__FILE__).'/params.php'),
protected/config/params.phpを用意し、以下のようにすれば完了です。
return array( // this is used in mail from 'adminEmail'=>'staff@example.jp', );
開発環境と公開環境で違う設定ファイルを利用する
index.php、index-test.php、protected/yiic.phpで、読み込んでいる設定ファイルをサーバの環境変数によって切り替えるようにします。
例えば、index.phpであれば以下のような感じです。
HTTP_HOSTがlocalhostかどうかで判断しています。また、読み込む設定ファイルだけでなく、公開環境では、デバッグモードを無効にします。
if($_SERVER['HTTP_HOST']=='localhost') { // change the following paths if necessary $yii=dirname(__FILE__).'/../../yii/framework/yii.php'; $config=dirname(__FILE__).'/protected/config/development.php'; // remove the following lines when in production mode defined('YII_DEBUG') or define('YII_DEBUG',true); // specify how many levels of call stack should be shown in each log message defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3); } else { // change the following paths if necessary $yii=dirname(__FILE__).'/../yii/framework/yii.php'; $config=dirname(__FILE__).'/../yii/example/protected/config/production.php'; } require_once($yii); Yii::createWebApplication($config)->run();
設定ファイルのprotected/config/main.phpですが、このファイルには開発環境、公開環境の両方で利用する設定を書くようにし、protected/config/development.phpには開発環境でのみ利用する設定、protected/config/production.phpには公開環境でのみ利用する設定を書きます。
protected/config/main.phpは以下のような感じです。
return array( 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', //this site name 'name'=>'テストサイト', 'language'=>'ja', // preloading 'log' component 'preload'=>array( 'log', ), // autoloading model and component classes 'import'=>array( 'application.models.*', 'application.components.*', ), // application components 'components'=>array( 'user'=>array( 'class' => 'WebUser', // enable cookie-based authentication 'allowAutoLogin'=>true, ), // uncomment the following to enable URLs in path-format 'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName' => false, 'rules'=>array( '<controller:\w+>/<id:\d+>'=>'/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'/', '<controller:\w+>/<action:\w+>'=>'/', ), ), 'errorHandler'=>array( // use 'site/error' action to display errors 'errorAction'=>'site/error', ), ), // application-level parameters that can be accessed // using Yii::app()->params['paramName'] 'params'=>require(dirname(__FILE__).'/params.php'), );
protected/config/development.phpは以下のような感じです。
return CMap::mergeArray( require (dirname(__FILE__).'/main.php'), array( 'modules'=>array( // uncomment the following to enable the Gii tool 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'hogehoge', // If removed, Gii defaults to localhost only. Edit carefully to taste. 'ipFilters'=>array('127.0.0.1','::1'), //bootstrap 'generatorPaths'=>array('bootstrap.gii') ), ), // application components 'components'=>array( 'session'=>array( 'sessionName' => 'Session', 'class'=>'CCacheHttpSession', 'cacheID'=>'sessionCache', 'cookieMode'=>'only', 'timeout'=>86400, ), 'sessionCache'=>array( 'class'=>'CApcCache', ), 'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=hoge', 'emulatePrepare' => true, 'username' => 'hoge', 'password' => '4uDH', 'charset' => 'utf8', ), 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CWebLogRoute', 'levels'=>CLogger::LEVEL_PROFILE, 'showInFireBug'=>true, 'ignoreAjaxInFireBug'=>true, ), array( 'class'=>'CWebLogRoute', ), ), ), ), ) );
protected/config/production.phpは以下のような感じです。
return CMap::mergeArray( require (dirname(__FILE__).'/main.php'), array( // application components 'components'=>array( 'cache'=>array( 'class'=>'system.caching.CApcCache', ), 'session'=>array( 'sessionName' => 'Session', 'class'=>'CCacheHttpSession', 'cacheID'=>'sessionCache', 'cookieMode'=>'only', 'timeout'=>600, ), 'sessionCache'=>array( 'class'=>'CApcCache', ), 'db'=>array( 'connectionString' => 'mysql:host=db.exmple.jp;dbname=hoge', 'emulatePrepare' => true, 'username' => 'hoge', 'password' => 'dfuDH', 'charset' => 'utf8', //cache schema 'schemaCachingDuration' => 3600, //sec ), 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>CLogger::LEVEL_ERROR, 'logFile'=>'level_error.txt', 'filter'=>'CLogFilter', ), ), ), ) ) );
公開環境では、キャッシュを有効にしています。
また、ActiveRecordを利用しているので、
'schemaCachingDuration' => 3600, //sec
で、データベーススキーマをキャッシュするようにし、スキーマの解析時間を減らします。こうすることで、スキーマを解析するために発行されていた以下のようなSQLの発行を制限できます。
SHOW FULL COLUMNS FROM `tbl_event`; SHOW CREATE TABLE `tbl_event`;
ただし、テーブル構造の変更があった場合、実際のテーブル構造とキャッシュに違いが出るため問題になります。この場合、キャッシュを削除する必要があります。
キャッシュにCApcCacheを利用しているのであれば、httpdなどのウェッブサーバを再起動したらキャッシュは削除されます。