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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

  <key>Label</key>

  <string>homebrew.mxcl.varnish</string>

  <key>ProgramArguments</key>

  <array>

    <string>/usr/local/opt/varnish/sbin/varnishd</string>

    <string>-n</string>

    <string>/usr/local/var/varnish</string>

    <string>-f</string>

    <string>/usr/local/etc/varnish/default.vcl</string>

    <string>-s</string>

    <string>malloc,1G</string>

    <string>-T</string>

    <string>127.0.0.1:2000</string>

    <string>-a</string>

    <string>0.0.0.0:80</string>

    <string>-u</string>

    <string>{YOUR_USER_NAME}</string>

    <string>-p</string>

    <string>http_resp_size=64k</string>

    <string>-p</string>

    <string>http_resp_hdr_len=16k</string>

    <string>-F</string>

  </array>

  <key>KeepAlive</key>

  <true/>

  <key>RunAtLoad</key>

  <true/>

  <key>WorkingDirectory</key>

  <string>/usr/local</string>

  <key>StandardErrorPath</key>

  <string>/usr/local/var/varnish/varnish.log</string>

  <key>StandardOutPath</key>

  <string>/usr/local/var/varnish/varnish.log</string>

</dict>

</plist>

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 4.8.1.1

wget http://download.icu-project.org/files/icu4c/4.8.1.1/icu4c-4_8_1_1-src.tgz
tar -xvf icu4c-4_8_1_1-src.tgz
cd icu/source/
./runConfigureICU Linux
make
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

wget http://php.net/distributions/php-5.4.11.tar.bz2
tar -xvf php-5.4.11.tar.bz2
cd php-5.4.11/ext/intl
phpize
./configure --enable-intl
make
make install

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

Edit php.ini to load extension

extension=intl.so

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.

cities_repository:
        class: MY\EntityBundle\Entity\CitiesRepository
        arguments: [MYEntityBundle:Cities]
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        calls:
            - [ 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();
            $city->setName($cityName);
            $city->setCountry($country);
            $this->_em->persist($city);
            $this->_em->flush();
        }

        return $city;
    }
}

Usage

$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 form.vars.help is defined %}
                    <span class="help-block">{{ form.vars.help }}</span>
                {% endif %}
            </div>
            {{ form_errors(form) }}
        </div>
    {% 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 %}
            </div>
        {% endif %}
    {% endspaceless %}
{% endblock form_errors %}

Usage

{{ form_widget(signup) }}

Use Memcache for Session handler on Symfony 2.1

#parameters.yml

session_memcache_host: 127.0.0.1
session_memcache_port: 11211
session_memcache_prefix: sess
session_memcache_expire: 3600

#config.yml

services:
    session.memcache:
        class: Memcache
        calls:
             - [ addServer, [ %session_memcache_host%, %session_memcache_port% ]]

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

framework:
    session:
        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/gearman.so’ – /usr/lib/php5/20100525/gearman.so: 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/
./configure
make
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

wget https://git-core.googlecode.com/files/git-1.7.11.1.tar.gz
tar -xzvf git-1.7.11.1.tar.gz 
cd git-1.7.11.1
./configure 
make
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: https://github.com/phthviet/codeigniter_hook_master_autoview

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
$this->load->view('welcome_message');
}

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…