Eric Barnes

CodeIgniter Performance Optimizations

I am currently building a pretty large CMS system on top of CodeIgniter and I wanted to share some of the best performance optimizations I have found. Although CodeIgniter is optimized out of the box when you start building large scale applications with large controllers it is easy to miss things that can add up to your overall performance.

Do Not Save Queries

$this->db->save_queries = FALSE;

By default CodeIgniter keeps an array of all the queries1 ran so that they can be used in the profiler for debugging. This is great during development but in most production cases that is not needed and just wasted memory. I recommend disabling this in production mode and can be conditionally setup with a simple if statement:

if (ENVIRONMENT == 'production')
{
    $this->db->save_queries = FALSE;
}

Output Cache

CodeIgniter supports several levels of caching and the most extreme is caching is the entire output. That is designed to cache the entire parsed page then just display it the html for the amount of minutes you designate.

By default CodeIgniter writes this cache to files which increases your i/o and it might suit you better to use apc2 (example) or memcache for the storage.

The downside to this is if you are needing some form of dynamic data like user information. Then it will not work. If that is a requirement for you then consider caching queries or other areas where your site is slow. Or you can do like getsparks and use ajax calls to dynamically populate the user information.

Consider Nginx + php-fpm + apc

I know everybody uses Apache and it has tons of tutorials and an all around good server. But in my tests straight out of the box Nginx out performs Apache and I am not the only one

I had a standard AWS medium instance running about 25 sites that was averaging 30% CPU usage during the day with Apache + APC. After just switching to Nginx and php-fpm it is now averaging around 10%. That is a huge difference!

Autoloading Can Be Dangerous

CodeIgniter comes with an “Auto-load” feature that permits libraries, helpers, and models to be initialized automatically every time the system runs.

Lets face it autoloading is awesome, but does come with some drawbacks. The biggest is that everything autoloaded is loaded on every request. This means for every page and every ajax call all those items are being loaded. So unless you are, in fact, going to need it for everything then I wouldn’t recommend auto loading. Instead consider base controllers or loading only when needed.

Only Load What You Need

Touching on autoloading is another common issue I have seen. Loading when you don’t need too. Ideally you do not want to load anything unless you are going to use it in the code that is running.

class Welcome extends CI_Controller {
    public function __construct()
    {
        $this->load->library('email');
        $this->load->library('ftp');
    }
    public function index()
    {
        $this->load->view('home_page');
    }
}

As you can see in the example above we are loading two libraries that will never be used when viewing the home page so by including them you are just wasting resources.

Have your say…

Did I miss any optimizations that you use? Or have any recommendations that are not well known? If so please share in the comments.


  1. This feature has been around since v1.6.0 but is not documented very well. 

  2. This idea came from getsparks.org and I linked to a gist in case getsparks code changes. 

Nginx PHP Gotchas

Here are some changes I needed to make to my Nginx conf that didn’t seem obvious to me and will hopefully be of help to others:

Random 502 gateway errors

fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;

413 Request Entity Too Large

This is caused because by default file uploads are limited to 1mb.

server {
    client_max_body_size 20M;

Resources:

CodeIgniter Nginx Virtual Host

Currently I am in the process of switching a server from Apache to Nginx and here is a quick tip for those using CodeIgniter or who want to host a bunch of very similar domains. For each server line I do this: 

server {
    server_name .mysite.com;
    root /sites/mysite/www;
    include /sites/nginx/ci_vhost;
}
server {
    server_name .mysecondsite.com;
    root /sites/secondpath/www;
    include /sites/nginx/ci_vhost;
}

Then the ci_vhost includes the common settings: 

index index.html index.php index.htm;

# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
    expires max;
    log_not_found off;
}

location / {
    # Check if a file exists, or route it to index.php.
    try_files $uri $uri/ /index.php;
}

location ~* \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

This allows you to have all the shared settings in one location and will make future changes much easier. Also this works perfectly for me with CodeIgniter’s default setup. Removing index.php and leaving uri_protocol to AUTO.

PHP, Pear, and phpUnit with MAMP Pro

Now updated for MAMP 2.0.1

I like using MAMP Pro on my mac because it is so easy to setup and the included virtual hosts are very nice. Plus it is easy to install for designers and those that don’t like mucking in the file system.

One gotcha though is installing php, pear, and then phpunit so it will run through terminal.  I know that there are tons of tutorials on this but from my recent research finding the exact steps hasn’t been very simple. (Plus some of them are just strange ways of doing it imo) So I decided I should write out a post describing all the steps to get this setup and have this for future reference After you have mamp pro installed open terminal and follow these steps…

1. Backup old files. (just incase)

sudo mv /usr/bin/php /usr/bin/php_bak
sudo mv /usr/bin/pear /usr/bin/pear_bak
sudo mv /usr/bin/phpunit /usr/bin/phpunit_bak

2. Symlink the mamp “php” and “pear” file

sudo ln -s /Applications/MAMP/bin/php/php5.3.6/bin/php /usr/bin/php
sudo ln -s /Applications/MAMP/bin/php/php5.3.6/bin/pear /usr/bin/pear

3. Update pear info

sudo pear config-set  doc_dir /Applications/MAMP/bin/php/php5.3.6/lib/php/doc
sudo pear config-set  ext_dir /Applications/MAMP/bin/php/php5.3.6/lib/php/extensions
sudo pear config-set  php_dir /Applications/MAMP/bin/php/php5.3.6/lib/php/
sudo pear config-set  php_bin /Applications/MAMP/bin/php/php5.3.6/bin/php
sudo pear config-set  php_ini /Applications/MAMP/bin/php/php5.3.6/conf/php.ini
sudo pear config-set  sig_keydir /Applications/MAMP/bin/php/php5.3.6/pearkeys
# This fixes a bug in pear
sudo rm /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf 

4. Upgrade pear

sudo pear channel-update pear.php.net
sudo pear upgrade pear
pear -V
PEAR Version: 1.9.4
PHP Version: 5.3.6

5. Install phpUnit

sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover components.ez.no
sudo pear channel-discover pear.symfony-project.com
sudo pear install --alldeps phpunit/PHPUnit

6. Move “phpunit” to /usr/bin

sudo ln -s  /Applications/MAMP/bin/php/php5.3.6/bin/phpunit /usr/bin/phpunit</pre>

7. Optionally Install vfStream:

sudo pear channel-discover pear.php-tools.net
sudo pear install pat/vfsStream-alpha

That should be it. At this point php, pear, and phpunit should all work via terminal.

Errors

If you get this error: Notice: unserialize(): Error at offset 276 of 1133 bytes in Config.php on line 1050 ERROR: The default config file is not a valid config file or is corrupted.

Try removing this file:

sudo rm /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf