Install Varnish on Mac OSX locally for development

Homebrew is the most package management i love on Mac. So let use it to install Varnish:

brew install varnish

The command will install Varnish 4.0.0, the newest version on brew source at this time.

After installation step, Homebrew suggests us do some stuff to load Varnish at startup time by link .plist file to ~/Library/LaunchAgents. But the big problem here is the plist file from Varnish package could not start properly. The reason is we need to be root to run Varnish if you want it listens on port 80 and can able to run C compiler during its starting (I’m not sure was it affected on lower version of Varnish?).

The sample plist file:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">

<plist version="1.0">





































Another important thing we should put .plist in /Library/LaunchDaemons instead of user library dir ~/Library/LaunchAgents because we are expecting this file will be executed by root.

Start it:

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.varnish.plist

PHP 5.4 Install php-intl extension on centos 5 with latest ICU data

When i moved my Symfony project to VPS, the problem is missing php-intl because i have installed PHP from Remi repository (php 5.4.11). I will show you how can we fix it.

Install ICU version 4+ to meet symfony’s requirements by compile it from source

I choose ICU version

tar -xvf icu4c-4_8_1_1-src.tgz
cd icu/source/
./runConfigureICU Linux
make install

If Everything is OK, you are ready to next step

Compile php-intl extension from source
Let’s choose the same php version with your self, my case is PHP 5.4.11

tar -xvf php-5.4.11.tar.bz2
cd php-5.4.11/ext/intl
./configure --enable-intl
make install

Everything is OK, we have at /usr/lib64/php/modules/

Edit php.ini to load extension

Restart php-fpm

/etc/init.d/php-fpm restart


Symfony 2: Injecting service as dependency into doctrine repository

To do that, you must declare repository as a service and inject services which you need into it.

        class: MY\EntityBundle\Entity\CitiesRepository
        arguments: [MYEntityBundle:Cities]
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
            - [ setCanonicalizer, [ @util.canonicalizer ] ]

The repository class:

namespace MY\EntityBundle\Entity;

use Doctrine\ORM\EntityRepository;
use FOS\UserBundle\Util\CanonicalizerInterface;

 * CitiesRepository
class CitiesRepository extends EntityRepository
     * @var CanonicalizerInterface $canonicalizer
    protected $canonicalizer;

    public function setCanonicalizer(CanonicalizerInterface $canonicalizer)
        $this->canonicalizer = $canonicalizer;

    public function getCanonicalizer()
        return $this->canonicalizer;

    public function saveFromString($cityName, Countries $country)
        $city = $this->findOneBy(
            array('name' => $this->canonicalizer->canonicalize($cityName))
        if (!$city) {
            $city = new Cities();

        return $city;


$city = $this->get('cities_repository')->saveFromString(
            'Ho Chi Minh',
            new Countries('VN', 'Vietnam')

Symfony 2 redefine form_row for horizontal form in bootstrap support validation style

Override form_row block in your twig template as follow:

{% form_theme signup _self %}
{% block form_row %}
    {% spaceless %}
        <div class="control-group{% if errors|length > 0 %} error{% endif %}">
            {{ form_label(form, '', { 'label_attr': {'class': 'control-label'} }) }}
            <div class="controls">
                {{ form_widget(form) }}
                {% if is defined %}
                    <span class="help-block">{{ }}</span>
                {% endif %}
            {{ form_errors(form) }}
    {% endspaceless %}
{% endblock form_row %}
{% block form_errors %}
    {% spaceless %}
        {% if errors|length > 0 %}
            <div class="controls">
                {% for error in errors %}
                    <span class="help-block">{{ error.messagePluralization is null
                    ? error.messageTemplate|trans(error.messageParameters, 'validators')
                    : error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators') }}</span>
                {% endfor %}
        {% endif %}
    {% endspaceless %}
{% endblock form_errors %}


{{ form_widget(signup) }}

Use Memcache for Session handler on Symfony 2.1


session_memcache_port: 11211
session_memcache_prefix: sess
session_memcache_expire: 3600


        class: Memcache
             - [ addServer, [ %session_memcache_host%, %session_memcache_port% ]]

        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
        arguments: [ @session.memcache, { prefix: %session_memcache_prefix%, expiretime: %session_memcache_expire% } ]

        handler_id: session.handler.memcache

[Ubuntu] PHP 5.4.x and gearman troubleshooting

When upgraded to PHP 5.4.x, you may have an error:
PHP Warning:  PHP Startup: Unable to load dynamic library ‘/usr/lib/php5/20100525/’ – /usr/lib/php5/20100525/ undefined symbol: gearman_job_error in Unknown on line 0

[Resolve] Update your gearmand server to the latest version

Remove current gearman from packages manager
sudo apt-get remove gearman gearman-job-server

Install lastest version (now gearmand-1.1.1.tar.gz )
sudo apt-get install libboost-program-options-dev libboost-all-dev libevent-dev cloog-ppl
tar -xvf gearmand-1.1.1.tar.gz && cd gearmand-1.1.1/
sudo make install

Run gearman
gearmand -d

Check version
gearadmin --server-version

Start gearman at boot up: init script

Centos 5.8 install your own git server

Install dependencies

yum -y install zlib-devel openssl-devel cpio expat-devel gettext-devel

Install from source

tar -xzvf git- 
cd git-
make install

Check install:

git --version

Create new Test repo:

useradd git
cd /home/git
mkdir test-repo
cd test-repo
git init
cd ../
git clone --bare ./test-repo/ test-repo.git

Add your public key to authorize

cd /home/git
mkdir .ssh
touch .ssh/authorized_keys
# import your public key into authorized_keys
chown -R git.git ./*

Now you can clone your test-repo and then pull, push

git link: git@server_ip:test-repo.git

Codeigniter hook: combine master page and auto load view

Get source at:

How to use?

When use this hook. you don’t need to load view manually by hand, just doing what are in your controller codes

Old way:
public function index()
// your code

New way:
public function index()
// your code

The view of action will auto located at path /application/views/{controller}/{method}.php if exists

Note: the view only will be locate if there is no output from controller ($this->output->set_output() won’t be call in controller codes)

will be continue…