Initial setup

  1. Download and extract the Thrift tarball
  2. Download and extract the Cassandra tarball

Building the PHP client

  1. Change to the thrift directory
  2. Install build dependencies.
  3. Configure and build Thrift:
    ./configure
    make
    
  4. Build the PHP Thrift interface for Cassandra:
    ./compiler/cpp/thrift -gen php ../PATH-TO-CASSANDRA/interface/cassandra.thrift
    
  5. Copy the include files to a useful place (per the Cassandra "Getting Started" guide):
    sudo mkdir -p /usr/share/php/Thrift
    sudo cp -R gen-php/ /usr/share/php/Thrift/packages/
    sudo cp -R lib/php/src/* /usr/share/php/Thrift/
    

Building and installing the native PHP extension

Installing and using the native PHP extension provides approximately a ten-fold performance boost on writing data.

  1. Change to PATH-TO-THRIFT/lib/php/src/ext/thrift_protocol.
  2. Install support for building PHP extensions:
  3. Build the extension:
    phpize
    ./configure --enable-thrift_protocol
    make
    
  4. Copy the extension's .so to the extensions directory.
  5. Enable the module.
  6. Verify installation:
    php -i | grep -v "PWD" | grep "thrift_protocol"
    
  7. Restart Apache.
  8. Ensure that the PHP is using TBinaryProtocolAccelerated and not TBinaryProtocol as the protocol.

Starting the server for development use

  1. Change to the Cassandra directory.
  2. Install the Sun Java runtime.
  3. Give Cassandra the permissions it needs:
    WHOAMI=`whoami`
    sudo mkdir /var/log/cassandra/
    sudo chown $WHOAMI /var/log/cassandra/
    sudo mkdir /var/lib/cassandra
    sudo chown $WHOAMI /var/lib/cassandra/
    
  4. Start Cassandra in the foreground:
    ./bin/cassandra -f
    

Checking the connection

  1. Copy in the PHP from the Cassandra client examples page to /var/www/cassandra-test.php.
  2. Visit http://localhost/cassandra-test.php in the browser.
  3. On the first load, the array output should be empty. On the second, there should be content.

Consider using higher-level clients

While it's possible to use the low-level Thrift API, it's hard to learn and very inefficient as you will end up writing lots of code for very simple tasks such as fetching and reading data, it's advised to use some high-level client.

Good candidates include: