the journal & notebook of

Eric L. Barnes

Page 10 of 10

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;


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 {
    root /sites/mysite/www;
    include /sites/nginx/ci_vhost;
server {
    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_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.

CodeIgniter Minify

The goal of this project is to provide a simple way to minify and combine js and css files inside a CodeIgniter application. Currently other systems exists but I wanted the compression to be part of my build process. So on deployments I compress and minify all the js and css. Then push off to s3 but this could also be useful to write them to a single file.

Visit the CodeIgniter Minify repository.


Below is an overview of different usages:

Minify JS file

$file = 'test/js/test1.js';
echo $this->minify->js->min($file);

Minify CSS file

$file = 'test/css/test1.css';
echo $this->minify->css->min($file);

Minify String

$content = 'body{ padding:0; margin: 0}';
echo $this->minify->css->min($content);

Minify and combine an array of files. Useful if you need files to be in a certain order.

$files = array('test/css/test2.css', 'test/css/test1.css');
echo $this->minify->combine_files($files, [optionalParams]);

Minify and save a physical file

$file = 'test/css/test1.css';
$contents = $this->minify->css->min($file);
$this->minify->save_file($contents, 'test/css/all.css');

Minify an entire directory. The second param is an array of ignored files.

echo $this->minify->combine_directory('test/css/, array('all.css'), [optionalParams]);

Optional Params

combine_files($files, [type], [compact], [css_charset]);
combine_directory($directory, [ignore], [type], [compact], [css_charset]);


[type]: string ('css' or 'js')
[compact] : bool (TRUE, FALSE). TRUE Compact/compress output, FALSE doesn't compress output (only aggregation)
[css_charset] : string (default 'utf-8'). If CSS you can force a starting single charset declaration (when aggregate files)
               due to the charset pre-removal (for stantdars compliance and Webkit bugfix prevention)
               set to null or leave empty if JS.

Combine dir:

[ignore] : array with files to ignore

Pear, & 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
sudo pear upgrade pear
pear -V
PEAR Version: 1.9.4
PHP Version: 5.3.6

5. Install phpUnit

sudo pear channel-discover
sudo pear channel-discover
sudo pear channel-discover
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
sudo pear install pat/vfsStream-alpha

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


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

CodeIgniter CSRF Protection With Ajax

A common issue I have seen lately is people using the new CSRF protection inside CodeIgniter 2.0.  This is a great new feature but it affects all post data no matter if it comes from a form or from an ajax call.  The ajax part is what seems to be tripping people up.  So I decided to write a short overview on how to accomplish sending jQuery ajax posts with CSRF enabled.

In this tutorial I will be using the jQuery cookie plugin for simplicity.

Here is the whole javascript to handle the ajax:

$("#selector").click(function () {
    var form_data = {
        cat_name: $('#something').val(),
        csrf_token_name: $.cookie("csrf_cookie_name")

        type: "POST",
        url: SITE_URL + "controller",
        data: form_data,
        success: function(data) {
            alert('it worked');

As you can see the only thing I had to add was the following line:

csrf_token_name: $.cookie("csrf_cookie_name")

Hopefully this will help some of you folks out.

Page 10 of 10

© 2011 - 2015 Eric L. Barnes & Theme by Anders Norén