Speed up your CakePHP application by using Memcached

In anticipation of heavier traffic and also just for performance reasons, I’ve started looking into ways of caching data for Nouchi.Mobi. Since this is a CakePHP application, I naturally looked into PHP cache based solutions and you’d be hard pressed not be recommended Memcached(Used by Facebook, Twitter, Youtube, Flicker amongst others). Why would you want database caching? If you’re using a framework to build your application or just from building any type of decently functional custom coded dynamic application nowadays, chances are you will make numerous calls to retrieve the data you want to display (the average Drupal site issues 300/400 queries!). Some pages do actually make more SQL queries than others and caching the result of these queries would give your application a significant performance boost. Memcached is a high performance in-memory data caching system that works by storing data as key-value pairs, meaning :

  • Store the value V with the key K
  • Retrieve the Value V identified by the key K

In the context of CakePHP, follow these instructions to get Memcached up and running in your local development environment. The trickiest steps in this set up will probably enabling the memcache support in your PHP configuration (getting and enabling the php_memcache.so extension in Linux). Keep in mind that for your production environment, unless you have your own Virtual Private Server, you’d be hard pressed to find any  host that will let you run Memcached in a shared hosting environment, but this is a scaling issue, and a problem you want to have, meaning your application is getting popular. Next you’d need to enable it in CakePHP in your app/core.php

Cache::config('default', array('engine' => 'Memcache'));

Once it’s up and running what you have to remember about using caching is that it is not magic. You have to code with caching in mind, and caching works best for queries that return the same data or that don’t change often. Teknoid has a good article on explaining what I mean by that with an example. Hope that’s enough to get you started and you can also take a look at this excellent Nettuts tutorial on the matter.

Installing SSH2 manually on Debian Linux

Phew. I’ve just succeeded in installing SSH2 on my DreamHost hosting account, which happens to run on Debian. I am more of a Windows guy which explains why it took me so long but I am none the wiser for the experience. Let me show you how I did it. I used these two excellent articles as my points of reference:

Installing SSH2

Make SSH Connections with PHP

First of all, let it be said that I am running on PHP 5.3. So if you are using DreamHost as your hosting provider, follow this guide here to update your account:

PHP Upgrade Guide

If you are already running 5.3 you might want to skip a few of these steps down and read ahead:

Upgrading to PHP 5.3

PHP 5.3 supports several new configuration hooks which make custom php.ini configuration super-easy! DreamHost recommends that all users needing custom PHP setup use PHP 5.3 or later, as the configuration changes required to set it up are much simpler than for PHP 5.2.
Log into the panel at https://panel.dreamhost.com/index.cgi?tree=domain.manage& and change your domain to use php5.3 (probably fast cgi)
Create a directory under your user called .php, with a subdirectory called 5.3. Files under this directory will be used by all domains under that user which are set to use PHP 5.3.
Example for the terminal
mkdir ~/.php
mkdir ~/.php/5.3
Once this directory exists, there are two files you can create in it:
To add custom directives to PHP, create a file under .php/5.3/ called “phprc” and add configuration directives to it. You do not need to copy the default php.ini to this file, as it is used in addition to the system php.ini; if a directive appears in both files, the one in this file will take precedence.
nano phprc
and then type in the commands you need.
Sample directive which turns off error reporting level Notice and Deprecated
error_reporting = E_ALL & ^(E_DEPRECATED|E_NOTICE)
Replace the system php.ini entirely
To replace the system php.ini entirely, create a file under .php/5.3/ called “phpini”. If this file exists, PHP will not read the system php.ini at all, so you should probably start by copying /etc/php53/php.ini here as a starting point. In most cases, this file is not necessary — please use a phprc if at all possible.
You may need to force PHP to reload its configuration file for changes to take effect. You can do this by running “killall php53.cgi” from a shell, or by waiting several minutes.
Loading PHP 5.3 extensions
Our build of PHP 5.3 contains a number of additional extensions which are not loaded by default (for instance: soap). You can load them by simply adding the following line to a file at /home/username/.php/5.3/phprc:
extension = soap.so

That last paragraph about loading php5.3 extensions is important because that’s exactly what we will do once we build and compile the ssh2 extension. For the mean time, make sure you are running PHP 5.3 by typing the command “which php” which should give you something like /usr/local/etc/php53. Your mileage might vary. If it still says /usr/local/etc/php5 on Dreamhost, you are still running 5.2.

Libraries Needed

Once you are running 5.3, the next step would be to download the necessary files you will need to compile and install. Download the archives for LibSSH and SSH2. I have used:

Once downloaded, I uploaded them manually to a “src” folder on my server and unzipped them. You could use the wget command to download the files, you know, pack it all up nice  in a shell script(Try this script on your machine, make sure you edit the values for PHPDIR and PREFIXDIR, specially for PHPDIR, make sure it points to your PHP 5.3 installation) but for some reason, it did not work for me. I just ran the following commands manually:

#Keep in mind I am using the following values for the {} variables you are seeing below
SRCDIR=${HOME}/src
PHPDIR=usr/local/etc/php53
PREFIXDIR=${HOME}/sshprefix
LIBSSH2="libssh2-1.8"
LIBSSH2FEATURES="--prefix=${PREFIXDIR}"
SSH2="ssh2-0.11.2"
SSH2FEATURES="--prefix=${PREFIXDIR} --with-php-config=${PHPDIR}/bin/php-config --with-ssh2=${PREFIXDIR}"
# Push the install dir's bin directory into the path. Important step not miss export PATH=${PREFIXDIR}/bin:$PATH export PHP_PREFIX=${PHPDIR}/bin

# Extract the src files into the src directory.
cd ${SRCDIR}

tar xzf ${SRCDIR}/${LIBSSH2}.tar.gz 

tar xzf ${SRCDIR}/${SSH2}.tgz 

## Compile

#AUTOCONF
cd ${SRCDIR}/${AUTOCONF}
./configure --prefix=${PREFIXDIR}
make install

#LIBSSH2
cd ${SRCDIR}/${LIBSSH2}
./configure ${LIBSSH2FEATURES}
make all install

#SSH2
cd ${SRCDIR}/${SSH2}
$PHP_PREFIX/phpize
./configure
make

Once this is done, your ssh2.so extension file should be avaible in the {SRCDIR}/ssh2-0.11.2/modules directory. What is important to remember for installing on DreamHost shared account is that since you don’t have access to the system’s regular folder, you need to specify a prefix directory to append to the path to support all the libssh and ssh2 related commands. That’s a very important step to complete. Copy the file to the .php/5.3 folder you created earlier and edit your phprc file to :

cp ~/src/ssh2-0.11.2/modules/ssh2.so !/.php/5.3
Edit your phprc file also to load the ssh2.so extension

extension=ssh2.so

Verify that the ssh2 extension was loaded by entering in the command line:

php -m

You should see the ssh2 extension loaded in the list. That’s it. You are free now to return to your code and use SSH2 connections to your liking!

Hope It Helps!

Programming mistakes and how to avoid them

For a good write up on some common programming mistakes and how to properly avoid or mitigate them, check out this excellent article by Paul Tero. Not only you get an explanation of the mistake, but he also gives you some tools and techniques to use alongside. What i really appreciated as well about the article is that Paul is not shy about admitting to past mistakes, no matter how serious, which helps give perspective about their repercussions and is without a doubt a sign of experience and maturity. Insightful to say the least, I have made some of these mistakes and picked up a few best practices from him:

My Favorite Programming Mistakes.

 

Download phpssh2.dll extension for PHP 5.3 compatible with WAMP Server 2.1

A project that I am working on now requires shell access basically to dynamically create cron jobs. This took me a minute and will be the topic of an upcoming post but for now, in my local WAMP based dev environment, I need SSH2 set up and good to go. I quickly found out that the version downloaded from the official PHP site needs to be compiled in a DLL.  No time and not set up with MS Visual Studio for that. I was able to procure a version that worked with my local set up of WAMPServer V2.1 on Windows XP:

You can download it here.

Hope it helps!

Zend Framework: mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication

Setting a new project that uses Zend Framework in Zend Studio I ran into an issue trying to launch the app. I got an this error;


Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'SQLSTATE[HY000] [2000] mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password'). This will store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag from your my.cnf file' in C:\wamp\frameworks\ZendFramework-1.7.8\library\Zend\Db\Adapter\Pdo\Abstract.php on line<em>143</em>

It turns out, the version of Zend I am running is 1.7, which is coded against an earlier version of PHP and when i installed WAMP i installed the latest version which came with PHP 5.3 which uses a new “mysqlnd” driver, thus the error. I guess at this point the choice is either to install an older version of WAMP or to reset the password for the database user as suggested in the bug description.