define( 'WPCACHEHOME', '/srv/www/' ); //Added by WP-Cache Manager php | Ian Chilton


Posts Tagged ‘php’

Book Review: Sphinx Search Beginner’s Guide

April 5th, 2011 No comments

Packtpub were kind enough to send me a copy of their new book, Sphinx Search Beginner’s Guide to review.

The book is written by Abbas Ali who is currently working as Chief Operating Officer and Technical Manager at SANIsoft Technologies Private Limited, Nagpur, India. The company specializes in development of large, high performance, and scalable PHP applications.

Sphinx is well described by it’s website as follows:

Sphinx is an open source full text search server, designed from the ground up with performance, relevance (aka search quality), and integration simplicity in mind. It’s written in C++ and works on Linux (RedHat, Ubuntu, etc), Windows, MacOS, Solaris, FreeBSD, and a few other systems.

Sphinx lets you either batch index and search data stored in an SQL database, NoSQL storage, or just files quickly and easily — or index and search data on the fly, working with Sphinx pretty much as with a database server.

The book covers everything from the installation and setup of Sphinx to simple and advanced use in PHP.

Here is a full outline of what’s covered:

  • Chapter 1, Setting Up Sphinx is an introduction to Sphinx. It guides the reader through the installation process for Sphinx on all major operating systems.
  • Chapter 2, Getting Started demonstrates some basic usage of Sphinx in order to test its installation. It also discusses full-text search and gives the reader an overview of Sphinx.
  • Chapter 3, Indexing teaches the reader how to create indexes. It introduces and explains the different types of datasources, and also discusses different types of attributes that can comprise an index.
  • Chapter 4, Searching teaches the reader how to use the Sphinx Client API to search indexes from within PHP applications. It shows the reader how to use the PHP implementation of the Sphinx Client API.
  • Chapter 5, Feed Search creates an application that fetches feed items and creates a Sphinx index. This index is then searched from a PHP application. It also introduces delta indexes and live index merging.
  • Chapter 6, Property Search creates a real world real estate portal where the user can add a property listing and specify different attributes for it so that you can search for properties based on specific criteria. Some advanced search techniques using a client API are discussed in this chapter.
  • Chapter 7, Sphinx Configuration discusses all commonly used configuration settings for
    Sphinx. It teaches the reader how to configure Sphinx in a distributed environment where
    indexes are kept on multiple machines.
  • Chapter 8, What Next? discusses some new features introduced in the recent Sphinx release.
    It also shows the reader how a Sphinx index can be searched using a MySQL client library.
  • Lastly, it discusses the scenarios where Sphinx can be used and mentions some of the
    popular Web applications that are powered by a Sphinx search engine.

At first, the style of the book seemed a bit strange to me – it’s split up into small chunks which are often followed by a “What just happened” section which gives a summary or broken down explanation of the concept just explained. Once I got used to it though, this actually improved the clarity and aided understanding.

The book is a very informative read for both beginners to either search or Sphinx and existing users and i’d highly recommend it to anyone interested in either search or the Sphinx product.

Anyone willing to find out more about or purchase the book can do so on the Packetpub website.

Categories: Reviews Tags: , , , ,

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'));
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');


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:

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:

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_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: 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_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: , ,