define( 'WPCACHEHOME', '/srv/www/www.ichilton.co.uk/html/blog/wp-content/plugins/wp-super-cache/' ); //Added by WP-Cache Manager kohana | Ian Chilton

Archive

Posts Tagged ‘kohana’

Logging to Syslog with Kohana 3

March 16th, 2011 No comments

By default, the Kohana PHP Framework logs errors to files in the application/logs directory, separated into directories and files for each year, month and day (eg: application/logs/2011/03/16.php).

To change this behaviour to use the system log daemon, you change the following line in bootstrap.php:
Kohana::$log->attach(new Kohana_Log_File(APPPATH.'logs'));
to:
Kohana::$log->attach(new Kohana_Log_Syslog('site_identifier', LOG_LOCAL1));

By default, these will probably appear in your /var/log/syslog file (on Debian/Ubuntu anyway – some other distributions use /var/log/messages).

You can configure syslog to put these in their own file by adding this to your syslog configuration file (on Debian/Ubuntu it’s done by adding a file into /etc/rsyslog.d):
local1.* /var/log/local1.log

If something else is already logging to local1, you can change that and LOG_LOCAL1 above to local2 and LOG_LOCAL2.

If you would like to write your own messages to the log, you can do so with:
Kohana::$log->add(Log::ERROR, 'error text');

Instead of ERROR, you can also use EMERG, ALERT, CRITICAL, WARNING, NOTICE, INFO or DEBUG.

If you would like to pull out different types of errors into different files, you can use this in your syslog configuration:
local1.=alert /var/log/local1.strace.log

Note that the errors are not actually written until the request has completed. You can force them to be written with the following:
Kohana::$log->write();

You can also force this behaviour by setting Log::$write_on_add = TRUE; in bootstrap.php, but be aware there is an overhead to doing that.

If you would like to ensure that logging is setup before writing to the log, you can do the following:

if (is_object(Kohana::$log))
{
// Add this exception to the log:
Kohana::$log->add(Log::ERROR, $error);

// Make sure the logs are written:
Kohana::$log->write();
}

Categories: development Tags: ,

Kohana 3 (KO3) PHP Framework with Nginx (and PHP-FPM)

March 7th, 2011 No comments

The Kohana manual (or for 3.0) gives the following configuration for doing clean url rewriting with Nginx:

location / {
index index.php index.html index.htm;
try_files $uri index.php;
}

location = index.php {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}

I hit two problems with this.

Firstly, I had to change it to location = /index.php to get it to work.

Once I had done that it seemed to work fine.

However, I then realised that query string parameters were not working correctly. For example, if I had a URL of: http://mydomain.com/controller/param1/param2?myvar=123. Under Apache, that would work fine and $_GET['myvar'] would be set to 123. Under Nginx however, $_GET['myvar'] wouldn’t be set.

I fixed this by using: try_files $uri /index.php?$query_string;

My final, working config is therefore as follows:

location / {
try_files $uri /index.php?$query_string;
}

location = /index.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}

This seems to be working fine on an Ubuntu 10.10 box with: nginx-full 0.8.54-4ppa14~maverick and php5-fpm 5.3.3-1ubuntu9.3.

It’s worth noting that the above won’t let you put any other .php files in your web root. If you want to be able to do that, you’ll want to use:
location ~ \.php$ {
instead of:
location = /index.php {

Note though that there is a bit of a security problem to be aware of with doing \.php$ – i’ll blog about this soon.

Categories: Web Tags: , ,