Dodano: 05.09.2011
maslen on #kohana (freenode) asked about guide on preparing Kohana application to be deployed on production server. As there's no official guide, I've decided to write few pointers of my own.
Here's a list:
- Set your Kohana::$environment to Kohana::PRODUCTION. It's not really used in core (yet), but it's very useful to add your own debugging stuff in more complex applications that can't be seen in production. The best way to do so is modify your bootstrap.php adding something like this:
if (isset($_SERVER['KOHANA_ENV']))
{
Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
}
else
{
Kohana::$environment = ($_SERVER['REMOTE_ADDR'] == '127.0.0.1' ? Kohana::DEVELOPMENT : Kohana::PRODUCTION);
}
- Create your own 500 errors in place of Kohana's. The simplest way of doing so is creating your custom application/views/kohana/error.php with some nice, user friendly error in place. The better way would be creating customised Kohana_Exception (extending Kohana_Kohana_Exception - yes, I know it sounds silly) and setting your own public static $error_view. If you don't have any other solution in place, you can also create customised Log_Writer that sends you info about critical errors to e-mail (if you're lazy just hack something in Kohana_Kohana_Exception, but it's not me who's going to coder hell)
- Remember about disabling profiling and enabling caching on production machine. Once again it can be done using Kohana::$environment like this:
Kohana::init(array(
'base_url' => '/',
'index_file' => '',
'profile' => (Kohana::$environment !== Kohana::PRODUCTION),
'caching' => (Kohana::$environment === Kohana::PRODUCTION)
));
There's also a profiling switch in Database configuration, don't forget about it. It may be a good idea to turn on caching for database too, depending on the project you're working on.
- Create your own, custom 403 and 404 error pages. It's very important and most often forgotten.
- It's a good practice to keep your framework + application and index with assets code in different directories with the former in some directory not accesible from web browser. I for example most often use directory structuring where application/modules/system are in core directory and index.php with uploads and assets in public_html. Unfortunately most of the cheap webhosting's shared accounts don't allow such configuration.
- (Edit: 7.09) I forgot about the most important part: always enable some kind of opcode cacher (XCache, APC). It'll give you a significant speed boost, sometimes up to 200-300% with properly written code. Results may vary though.
Of course it's not everything you can do to prepare your application for deployment. Don't forget about caching, use Fragment where possible and most importantly - don't be a cheapskate and don't try saving money on half-assed server.
2 komentarze
pim
Your entries about Kohana are really, really useful for me. Thanks a lot, mate :)