Celavi | blog
The Weblog of Ales Loncar
The Weblog of Ales Loncar
Aug 16th
Well, as you can tell, it’s been about a month since I’ve posted on this blog, but I’ve been busy1 elsewhere. In a week or two I’d share with you a few of the projects I’ve been working on recently and show you some peeks at what I will be doing for this Autumn and beyond.
What else is new?
I was approached about two weeks ago by Packt Publishing to write a review of a new book on Nginx. I’ve never done it before, but it will be a new challenge for me. I’ll publish my review on Celavi|Blog so you should check back from time to time :) They found me by Google Search. Especially they noticed valuable and informative post about setting up Nginx on Ubuntu. I have to say, it sure does make all the time spent blogging feel a little more worthwhile!
Nginx HTTP Server is a new book from Packt, written by Clement Nedelcu. This book is a detailed guide to setting up Nginx in different production situations: as standalone server, as reverse proxy, interacting with applications via FastCGI, and more. Using this book readers will be able to explore possible interactions between Nginx and Apache to get the best of both worlds.
Using this book, readers will be able to download and build Nginx from source along with its dependencies. In addition to this, they will learn the basics of Nginx configuration, process of replacing Apache by Nginx and troubleshooting. Users will also be able to configure Nginx to work as frontend for their existing HTTP server and establish advanced rewrite rules with the Nginx Rewrite module.
To read more about the book, please visit Packt Publishing Technical & IT Book Store.

Jun 26th
I won’t give you a lecture about using Nginx for a perfect web server. But the fact is that Apache is mostly a memory hog, so we are sometimes forced to look for alternative ways to host our websites. I will show you a step to step guide to install all the necessary software to run any sort of PHP application on your local machine. Read on to see how that can be achieved…
First step is updating your Ubuntu Installation
sudo aptitude update sudo aptitude safe-upgrade
This will update the apt package database and install all the latest updates (if there are any). If you see that a new kernel gets installed as part of the updates, you should reboot the system afterwards.
Installing Nginx is the next step. This can be easily done by following the next step (assuming there is no other web server up and running). Everything will be installed by default.
sudo aptitude install nginx
Start nginx afterwards:
sudo /etc/init.d/nginx start
After typing your web server’s hostname into a browser1, you should see the Nginx welcome page.
To make Nginx start at boot time, run
sudo update-rc.d nginx defaults
Next, we need FastCGI deamon to run PHP5 in fcgi mode. First off, Nginx doesn’t have its own internal FastCGI process manager, so we must run the FastCGI service separately. Usually the preferred method was to use spawn-fcgi program provided by the lighttpd web server. That’s not necessary any more, because from version 1.6.0 ahead spawn-fcgi was separated from lighttpd project. To install spawn-fcgi, run the following command.
sudo aptitude install spawn-fcgi
For now, we’re going to leave everything as it is and we’ll move forward with installing MySQL and PHP5.
Let’s install MySQL server and client binaries. The process is quick and easy.
sudo aptitude install mysql-server mysql-client
You will be asked to provide a password for the MySQL root user. After we specify a MySQL root password, the installation will finish. We can try the MySQL client program via command line tool:
mysql -u root -p
Installing PHP5. Now it’s time to install PHP5 via FastCGI. Ubuntu provides a FastCGI-enabled PHP5 package. We’ll install only core packages and add others when needed.
sudo aptitude install php5 php5-cli php5-common php5-suhosin php5-cgi php-pear php5-mysql
We’ve installed everything for running PHP on our computer, so let’s move on to necessary configuration. First, let’s write a script which will spawn FastCGI PHP processes on a unix domain socket, so there is no need for tcp/ip on localhost. Let’s create file /usr/sbin/fastcgi-php
sudo touch /usr/sbin/fastcgi-php
#!/bin/sh
FCGIPHP_BIN_PATH="/usr/bin/php5-cgi"
FCGIPHP_USER="www-data"
FCGIPHP_GROUP="www-data"
FCGIPHP_CHILDREN="2"
FCGIPHP_MAX_REQUEST="1000"
FCGIPHP_SERVER_ADDR="127.0.0.1"
FCGIPHP_SOCKET="/tmp/php-fastcgi.sock"
PIDFILE="/var/run/php5-fcgi.pid"
SPAWN_FCGI="/usr/bin/spawn-fcgi"
FCGIPHP_ENV="SHELL PATH USER"
COMMAND_ARGS="$SPAWN_FCGI -C $FCGIPHP_CHILDREN -s $FCGIPHP_SOCKET -f $FCGIPHP_BIN_PATH -u $FCGIPHP_USER -g $FCGIPHP_GROUP -P $PIDFILE"
export FCGIPHP_MAX_REQUEST
export FCGIPHP_SERVER_ADDR
ALLOWED_ENV="$FCGIPHP_ENV FCGIPHP_MAX_REQUEST FCGIPHP_SERVER_ADDR"
E=""
for i in $ALLOWED_ENV; do
eval "x=\$$i"
E="$E $i=$x"
done
start(){
echo "STARTING PHP-FCGI"
if [ -f $PIDFILE ];then
echo "PHP-FCGI is running already.";
exit 1;
fi
echo $COMMAND_ARGS
env - $E $COMMAND_ARGS
}
stop(){
echo "STOPING PHP-FCGI"
if test -f $PIDFILE;then
killall `basename $FCGIPHP_BIN_PATH`
rm -f $PIDFILE
else
echo "PHP-FCGI is not running"
fi
}
#Main################
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
*)
echo "usage: $0 {start|stop|restart}"
;;
esac
exit 1
and give it execute permissions. After that let’s start the process.
sudo chmod a+x /usr/sbin/fastcgi-php sudo /usr/sbin/fastcgi-php start
Final step, Nginx configuration. To get the web server up and running properly, we need to edit the file /etc/nginx/nginx.conf. We’ll pass all PHP calls to an upstream server called php5 so edit your configuration file to look like this:
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
sendfile on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
upstream php5 {
server unix:/tmp/php-fastcgi.sock;
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Any further configuration files needed for our applications will be located in /etc/nginx/sites-available and linked to /etc/nginx/sites-enabled. Let’s add new site which will link to our home public folder ~/Public and we will call it my.public.local. Now create my.public.local.conf in /etc/nginx/sites-available
server {
listen 80;
reset_timedout_connection on;
server_name my.public.local;
root /home/celavi/Public;
client_max_body_size 500m;
autoindex on;
location / {
index index.php;
}
# Handle Static Content Here
location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
access_log off;
gzip off;
expires 30d;
}
location ~* ^.+\.(css|js)$ {
access_log off;
expires 1d;
}
location ~* ^.+\.(pdf|gz|bz2|exe|rar|zip|7z)$ {
gzip off;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass php5;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
and create a symbolic link to /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-availible/my.public.local.conf /etc/nginx/sites-enabled/my.public.local.conf
We need to tell the operating system to map my.public.local to local IP address. Open file /etc/hosts and add this line:
#local maping 127.0.0.1 my.public.local
Let’s restart Nginx
sudo /etc/init.d/nginx restart
In order to test if the execution of PHP is working, create an index.php file in ~/Public folder with the following content:
<?php echo phpinfo();
Open browser and type http://my.public.local. If you did not see the phpinfo, there might be something wrong. In order to track down what went wrong, you can check the Nginx error log:
sudo tail /var/log/nginx/error.log
Remember, if you did change your php.ini you have to restart /usr/sbin/fastcgi-php. Restarting nginx isn’t’ necessary.
Well, that’s it. Now you can start developing your PHP apps.
Screenshots:
And remember, contributions earn you karma. ;)

Jun 22nd
I’ve received several emails and comments about KjeSeRoka and what the appropriate translation would be in English. I will try to explain it’s meaning for those whose first language is not Slovene.
The most appropriate translation1 would be: “Where’s The Party?”, or in this particular case “Where I can find upcoming rock concerts in Slovenia?”.
Rokat / rokaj
Word rokat/rokaj has common origin in South Slavic languages (Serbian, Croatian, Bosnian) and can have a lot of different meanings.
Let’s look at these examples:


I hope these examples where enough illustrated. :D
And remember, contributions earn you karma. ;)

Jul 6th
This tutorial is deprecated. With Zend Framework 1.9 plural support to Gettext adapter has been implemented.
One of the most popular discussions on forums, mailing lists and blogs regarding Zend Framework and its component Zend_Translate are about plural support in the gettext adapter. The Gettext Adapter is the Adapter which is used most frequently. Gettext is a translation source format which was introduced by GNU, and is now used worldwide. It is not human readable, but there are several freeware tools (for instance, POEdit), which can be very helpful. Zend_Translate does not use PHP’s gettext extension because it is not multi-thread aware. Adapter reads the mo files directly, therefore it does not need PHP’s workaround. Because Zend has focused on a simple API which covers many sources (array, scv, gettext, ini, tbx, tmx, qt, xliff, xmltm) , implementation of plurals (which are native to translation in general, not unique to gettext) would be a hard task in early deployment of Zend Framework.
in implementing plural support to Gettext Adapter in Zend Framework is understanding how gettext handles plural forms. I won’t get into details here, you can read more about this at GNU / gettext . For us the crucial information is about the plural form selections, which has to be stored in the header of entry of the PO file (the one with the empty msgid string). The plural form information looks like this:
Plural-Forms: nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);
Here you can find the list of plural forms, as used by Gettext PO, that are appropriate to each language.
in achieving our goal would be extracting that information from PO file and storing it somewhere safe for later use. I’ve decided to store it as a translation keyword “Plural-Forms” and then use it as necessary. The supplemented code which is added to Zend_Translate_Adapter_Gettext is:
//store PluralForm
if ($this->_adapterInfo[$filename] != 'No adapter information available') {
$this->_data[$locale]['Plural-Forms'] = $this->_getPluralForms($this->_adapterInfo[$filename]);
}
/**
* Retrieve Plural-Forms string from MO file
*
* @param array $meta
* @return string
*/
protected function _getPluralForms($meta)
{
$pluralForms = 'nplurals=2; plural=(n != 1);';
$array = array();
foreach (explode("\n", $meta) as $info) {
if ($info = trim($info)) {
list($key, $value) = explode(':', $info, 2);
$array[trim($key)] = trim($value);
}
}
if (array_key_exists('Plural-Forms', $array)) {
$pluralForms = $array['Plural-Forms'];
}
return $pluralForms;
}
is to write a function which would return translated plural form for the provided text, plural version of text, count and optional parameters.
This is just a piece of code from the whole translate class which holds Zend_Translate object (Zend_Translate_Adapter_Gettext):
/**
* Translate Plural message
*
* @param string $text simple text
* @param string $plural plural version of text
* @param int $count count
* @param string/array $params
* @return string
*/
public static function translatePlural($text, $plural, $count, $params = array())
{
//set Plural Forms String
self::_setPluralForms();
if(!is_string($text) || strlen($text) == 0)
return '';
// find out the appropriate form
$select = self::_selectString($count);
// this should contains all strings separated by NULLs
$key = $text.chr(0).$plural;
// verify if translation Exists
self::_verifyMessage($key);
if (!self::getInstance()->_translate->isTranslated($key)) {
$translated = ($count != 1) ? $plural : $text;
} else {
$result = self::getInstance()->_translate->translate($key);
$list = explode(chr(0), $result);
$translated = $list[$select];
}
// return formated translation
return vsprintf($translated, $params);
}
within action controller
//Change Locale to Slovene
ZendX_T::setLocale('sl_SI');
$threeSeconds = ZendX_T::translatePlural("%d second","%d seconds", 3, 3);
/**
$threeSeconds will have value '3 sekunde'
*/
The Zend Framework way of implementing this thing right would be writing a View_Helper. :) I’ll leave that task to you.
Download a zip archive of the project
zf_gettext_plural.zip (4.3 MiB, 245 hits)
And remember, contributions earn you karma. ;)

Jul 3rd
Mozilla Firefox 3.5 was released a few days ago. This is great news for us web developers, but not all add-ons have been updated to be compatible with the new version yet, YSlow being one of them. You can force an incompatible extension to work with Firefox 3.5 by disabling the compatibly check that Firefox performs before each install.
NOTE: This might lead to unexpected behavior or even a crash. Make sure that you properly backup your add-ons before editing.
And remember, contributions earn you karma. ;)

Jun 28th
Hello World! :P
Well, today is the day. I’ve finally managed to set up a blog in English. Some of you may have read my blog in Slovene (Internet Solutions) – and I must note that this blog will contain similar, if not the same, content. I registered this domain in January 2008, and since then this has been just a dumping ground. I’ve made my final decision about this step after I returned from Netherlands, where I was attending the Dutch PHP Conference 2009 (DPC).
Considering the fact that PHP is distributed under Open Source license, there was much debating why developers should contribute code to an Open Source Software projects. Especially one discussion was completely devoted to contribution. Software Architect for Zend Framework Matthew Weier O’Phinney was talking about various ways we can contribute to open source projects and earn our “karma”. Developers who are using the work of others, should take the responsibility and contribute back, even if this just means to translate a few pages of Project Documentation.
Hopefully I will be expecting some useful articles / contributions here in the nearby future.
And remember, contributions earn you karma ;)