PHP custom session handler changes in PHP7

If you are using a custom session handler probably you noticed this kind of issue when you upgraded to PHP7, else keep this information in mind since in the PHP documentation it is not really clear at the moment of writing this article.

Since PHP 5 this changes affected custom session handler implementation:

  • The read method should return an empty string if session was not started yet
  • The write method should return either true or false

How to enable SSL certificate on Nginx

First of all you have to check if your webserver version supports ssl. If ssl is not enabled in your server read more here

If SSL module is installed in your nginx instance then you have more few simple steps to follow.

  1. Generate Certificate Signing Request (CSR)
    • Generate private key
    • Login into the server by ssh
      openssl genrsa -out 2048
    • Generate the CSR
    • openssl req -new -key -out
  2. Create a backup of private key in a secure place it will be useful
  3. Request Certificate – with the CSR generated above you can go to a provider to sign your certificate.
  4. Install the certificate
    • You have to receive to keys from your SSL certificate provider: your primary certificate and the intermediate certificate.
    • create one file with both files
    • Upload to the server in a safe place with read access for root user
    • Edit your host configuration on nginx
    • server {
              listen 443 default ssl;
              ssl_certificate      /etc/ssl/;
              ssl_certificate_key  /etc/ssl/;
       keepalive_timeout    70;
              # other configuration
    • Test your nginx configuration and restart the service if everything is fine.
  5. Done

Recompile Nginx on production environement

In case you don’t have an Nginx cluster or you are not powered in a cloud base infrastructure then recompiling the webserver may be a tricky job. If you are in one of the described situation you can setup all instances one by one having a stable platform.

Even so of course you can recompile the webserver without downtime.

  1. Create backups for all nginx files.
  2. "cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old"
    "cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.old"
  3. Download all sources and dependencies for nginx
  4. #You can check previous configuration parameters by 
    nginx -V
    #Download all needed sources
    wget nginx-headers.tar.gz
    #Extract them 
    tar -zxvf file.tar.gz
  5. Configure the compilation with custom parameters. You can even specify an other binary location for the new compiled software. In this way all the installed software will sill exist in the original files. In my case I decided to overwrite the existing binary of nginx.
    “/usr/local/nginx/sbin/nginx -V”
  6. ./configure  --add-module=/home/ec2-user/headers-more-nginx-module-0.28 --add-module=/home/ec2-user/ngx_pagespeed- --with-http_ssl_module
  7. Compile and install
  8. sudo make
    sudo make test
    #run: "make install clean" This will install the new binary in place of the old one and  makes a backup of the old one.
    sudo make install
  9. Sometimes this step is a little bit risky because some files may be opened and could not be overwritten. In my case everything worked fine.  Even if the service was running in this time, the binary was already replaced.
  10. If you decided to go on a safer way and installed the binary in other place now all you have to change is defining your daemon to use this binary instead of old one, test the nginx configuration and restart the service.

A much more complex aprouch may be found here


Upgrade MySql 5.5 to 5.6 on Ubuntu 14.04

If you are already using MySql 5.5 and some new applications is requiring a newer version you can proceed a upgrade with the following steps

  1. Check if there is available a binary version for your operating system
  2. If you are using Ubuntu14.04 there is available a Mysql 5.6 version. If not, you have to register other repository
  3. Create a backup just in case somethings will go wrong
    mysqldump --lock-all-tables -u root -p --all-databases > dump.sql
  4. Upgrade
  5. sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install mysql-server-5.6
  6. If something was wrong until now you can remove previous version and separately install the new one.
  7. You can restore the information by
    mysql -u root -p < dump.sql

Install PHP 7 and xdebug for development environement

PHP 7 was released

Compile PHP 7 from source for development environement

git clone && cd php-src


./configure \
--with-freetype=/usr/local \
--with-config-file-path=/opt/php7/config.d \
--with-config-file-scan-dir=/opt/php7/conf.d \
--prefix=/opt/php7 \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-soap \
--enable-calendar \
--with-curl \
--with-mcrypt \
--with-zlib \
--with-gd \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-mysql \
--with-pdo-mysql \
--with-mysqli \
--with-png-dir=/usr \
--enable-gd-native-ttf \
--with-openssl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-libdir=lib \
--enable-ftp \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-gettext \
--with-gd \
--with-xpm-dir=/usr \
--with-ldap=/usr \
--with-jpeg-dir=/usr/lib/ \
--enable-fpm \
--with-fpm-user=www-data \
--with-fpm-group=www-data \

make test
make install

Install xdebug from sources, since there is no available binaries compatible with PHP7

  1. Download xdebug source archive
  2. Unzip the source code
  3. Check the PHP version /opt/php7/bin/phpize
  4. configure the compile process with this php version ./configure –with-php-config=/opt/php7/bin/php-config
  5. make
  6. make install
  7. Add the extension to ini file
  8. Configure xdebug
  9. xdebug.remote_enable=true
    xdebug.remote_port="<the port for XDebug to listen to>" (the default port is 9000)
    xdebug.remote_host=<the host where PhpStorm is running (e.g. localhost)>
    xdebug.profiler_output_dir="<AMP home\tmp>"


E-commerce platforms battle, benchmark and quality report

This article is focused on open-source online commerce platforms, most of them form PHP world.

Famous e-commerce platform vs new technologies

If you are here probably you want to start an online store and you are not 100% decided what will be the best platform you can use.  We are talking here just about open-source platforms most of them developed using PHP.

Why you should use an open-source platform? Because open-source is an interesting movement, and it was proved by Linux that making good software it is possible in this way.


Cloud-based Infrastructure for Black Friday

Earn more money during sales campaigns using cloud-based solution

During the largest online sales events of the year all retailers are very excited to sell as much as they can sell. But what if the online platform is not able to support such amount of traffic. It is your website ready for Black Friday?

Few advantages of using cloud computing for this purpose:

    • you have as much resources as you may want
    • you don’t need to be afraid of hardware issues
    • up-time is guaranteed for some services
    • you pay as you use
    • you can easily build a very stable/professional application
    • overall cost

You are not the only one

You can read a successful story about Black Friday campaign, in Azure cloud service, of one of the biggest retailer from Romania.

“Being able to quickly upscale or downscale as the number of customers changed, and only pay for the resources we used, gave us a huge competitive advantage,” says Cristina Martinas Online Sales Director at Flanco.

Compile Nginx from source

Probably you are setting a virtual host on one of cloud providers. Read here few references on cloud computing provides

Configure nginx with extra modules

First step in order to obtain needed features you have to think about which modules of nginx you will be using. Read Nginx documentation and download all dependencies.

For example I will download ngx_cache_purge from git and some few others nginx modules
git clone
git clone
git clone

Depending on which modules you will use probably you will need to install some libraries (depending on your operating sistem) as shown below.

apt-get install libxml2-dev
apt-get install libxslt-dev
apt-get install libgd2-xpm libgd2-xpm-dev

You can find the libraries or nginx modules you have to download or install by trying to configure the build. Some parameters may be removed in your case. You cand define each module in the folowing command and point to the source code of the module so build proces can link to it.

./configure --sbin-path=/usr/sbin/nginx --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/ --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --with-openssl=/usr/local/src/openssl-1.0.1j --add-module=/usr/local/src/passenger_apt_automation/debian_specs/nginx/modules/nginx-auth-pam --add-module=/usr/local/src/passenger_apt_automation/debian_specs/nginx/modules/nginx-echo --add-module=/usr/local/src/passenger_apt_automation/debian_specs/nginx/modules/nginx-upstream-fair --add-module=/usr/local/src/passenger_apt_automation/debian_specs/nginx/modules/nginx-dav-ext-module --add-module=/usr/local/src/ngx_cache_purge

The output of this command may be similar with:

Configuration summary
+ using system PCRE library
+ using OpenSSL library: /usr/local/src/openssl-1.0.1j
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
nginx http scgi temporary files: "/var/lib/nginx/scgi"

Build nginx and install

If the configure step was successfully finished then you can try to build and install nginx by following commands.

make install

Cloud computing providers overview and nice cloud features

When it comes to scale your application (PHP or no mater what languages or technologies) of course you are looking to optimize software part, but of course the hardware it is important.

Even if you are a start-up or a big business  you can optimize your monthly cost or performance using the right solution.


  • Digital ocean is a nice service but therea are few week points:
    1. you can not attache additional storage for virtual machines
    2. there are no other services you can use, virtual machine will be everything you have
  • Amazon AWS
    1. there are a lot of interesting features you can use and managed services, this will be a big plus
    2. all services I used until now worked perfect
    3. flexible management tools
  • Azure
    1. a lot of features and services
    2. interesging managed services
    3. personally, I encountered a lot of problems with virtual machines performances on Azure
    4. few of problems were resolved


  • Digital ocean it is ceaper and offer a very good performance
  • AWS I think it is best features/price I tested until now
  • Azure – I think I neet to test it more until I will show my opinion.

In conclusion, I will recommend next cloud computing providers in few use cases:

  • Digital Ocean, if you are developing small project and you are not needing advanced cloud computing features. It is not flexible on storage service
  • Amazon AWS, best for ambitious projects, full feature cloud computing service

Performance benchmark PHP 5 , PHP 5 + Opcache, PHP 5 + APC, HHVM

Is PHP good for any application?

Since php is most popular language on web based applications with a huge amount of opensource components/reusable code and powered by a big community. I think it is important to understand  it’s architecture first and then talk about scaling the application.

From my point of view PHP it is one of the best language for a start-up or low budget project because just because of the description above. Maybe I can add more reasons here as: depoyment flow or development efficiency or others.

What if the business grows very fast?

So, what? There are a lot of tricks you can use.


Since PHP is a interpreted language some use-cases can prove PHP lazy.

HHVM is a opensource project powered by Facebook to add more power to PHP projects. In this way there are few improvements of classical interpreter. HHVM is a just in time compiler for PHP meaning, that the code is not interpreted every time but the generated byte code is saved and reused. HHVM try to resolve few week points as: variable type missing, functions – variables – constants lookup time and others.

The result of my benchmark maybe will surprize you.

The benchmark is made on Magento 1.9, the most popular e-commerce platform on PHP world.

Compare PHP with HHVM

I used basically configured tools and as you can see in the table above just using HHVM you can obtain a 3.5X performance gain then using php 5.6X. But the differcence is not so big if on the standard PHP interpreter you are using APC cache or OPCACHE.


Requests/min / Concurent users

See the performance gain in the following graph


The average response time is much more stable using hhvm because of all optimizations made as you can see bellow.


In my test I used form 15 concurent users up to 250 concurent users, below we can see the availability state of the service during the test.


Configure Zend Opcache extension for PHP

Zend Opcache extension installation

If you want to speedup your PHP application maybe you are looking for a PHP accelerator such APC cache.

Since PHP 5.5 there is another option for caching Opcodes (details): Zend OpCache extension

If you are using PHP 5.5.0 and later Zend Opcache extension may be compiled with PHP, you have only to configure it.

If you are using a older version of PHP you can compile the extension following few simple steps:

  • Download latest version here
  • Configure the compilation with your php config
  • ./configure \
  • Build the package
  • Install will copy the binary file to your extension directory (or you can copy by your own)
     make install

After the compilation was successfully finished you can declare your new extension into php.ini file

 zend_extension=/...full path.../

 Zend Opcache configuration

The full parameters list are listed on Zend Opcache documentation

I will just mention few of the parameters you have to consider in your configuration

  •  revalidate_freq – configure how often the file time stamp is checked to detect file changes. 0 – means check every time. You have to configure revalidate_freq 0 on your development machine. On production I am using a large number such as 3600
  • validate_timestamps – When disabled, you must reset the OPcache manually or restart the
    webserver for changes to the filesystem to take effect. I advice you to use it on production server and create a hook to clear Opcache after release
  • max_accelerated_files  – them maximum number of scripts stored in opcached memory. you can count your code-base using find . -type f -print | grep php | wc -l
  • memory_consumption – the amount of memory to store pre-compiled PHP files

You can read here a performance benchmark report here on the same subject.

Install PHP APC extension from sources

How to compile the PHP APC extension for your php version

Used operating system: Ubuntu 14.04

There are few steps to follow until you will have your APC extension up and running for your particular PHP version:

  • Download the latest version of APC
  • wget
  • Extract files and identify the default values for following parameters PHP Extension,
    Zend Extension using phpize
  • tar -xvf APC
    cd APC-X.X.X (replace with your downloaded version)
  • If you are running more then one version on the same machine you will need to configure the compilation with the target version of php
  • In my case I used this command to configure the compilation
  • ./configure --with-php-config=/opt/php-5.3/bin/php-config --enable-apc --enable-apc-debug
  • if everything worked ok then you can compile your extension make && make install
  • The last step is to enable and configure your extension in the php.ini file (echo “” > /etc/php.d/apc.ini)
  • Restart php service

You can read here a performance benchmark report here on the same subject.

Find in files – translations texts

Use case

Follow this article if you want to locate multiple occurrences of a string in a folder using a regular expression pattern.

 Find all occurrence of pattern

Using following code in linux command line you will copy all occurrence of the given pattern in a temporary file

 find ./ -name '*.*' -exec grep -o -E "language->get(.*?)" {} > output.txt  \;

OR you can use this 

find ./ -name '*.*' -exec grep -o -E -Hn "language->get(.*?)" {} > output.txt2  \;

Process the found parts

Using a simple text editor compatible with regular expression you will be able to process the temporary file into a wanted format.

See the attached screenshot.


In my case I had to find all $this->get(‘Any text here’) in my php files and create a translation file for Opencart platform in the following format:

$_['Any text here'] = 'Any text here';
$_['Any text here'] = 'Any text here';


Manage RDS instances through AWS CLI


Why do you need to use RDS CLI?

There are some actions that you may be want to execute on your instance but you are not allowed to make from web interface:

  • change the parameters group

Install RDS CLI tools

To be able to use RDS CLI tool you have to install Java, download RDS CLI tool and configure few environment variables.

All configuration steps are listed here:

As shown in the related article if the command

rds --help

is working then the CLI tools was successfully installed.

RDS CLI usage

As you already completed the configuration steps (very important: don’t forget to set the path to your credentials to be able to use any further commands).


Read more

 rds-modify-db-instance DBInstanceIdentifier --db-parameter-group-name fp-param-group --region eu-west-1

DBInstanceIdentifier – This is the unique key that identifies an DB instance. Stored as a lowercase string. This is provided at instance creation time, or modified later, and can be found at the beginning of instance Endpoint.

Very important:  If you are not providing the –region parameter the CLI will use the default region, and can return a wrong result.


Use more then one PHP version in Nginx

In many cases probably you need to have different PHP version on the same machine and using it on different domains.

Install PHP

You can install PHP versions by downloading already compiled package or you can compile your own (with personalized configuration – quick php compile & install guide).

Personally, I prefer for development machines to compile php by myself.  You can read how I prefer to install php in linked before article.


In you php-fpm coffiguration file there is a option to specify the port used to listen:

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   ''    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen =


Next step in your configuration is to define the upstream  in nginx configuration to be able to pass the request to PHP.  Next you will have few examples:

# This will define upstream to local PHP-FPM server; for tools like 
upstream local-phpfpm {
upstream phpfpm {
	#server unix:/var/run/php5-fpm.sock;
upstream php53fpm {
upstream php56fpm {

Next time when you will configure a virtual host on your machine you will be able to use different php version on the same machine:

    location ~ .php$ { ## Execute PHP scripts
        fastcgi_pass    php53fpm;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params


    location ~ .php$ { ## Execute PHP scripts
        fastcgi_pass    php56fpm;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params


Install PHP 5.x from repository on Ubuntu

Install build tool

Here are detailed steps for installing compile tool for ubuntu.

Install dependencies

In most cases quite a lot of dependencies will be missing. You can install most of them using next command

sudo apt-get install libxml2-dev libpcre3-dev libbz2-dev libcurl4-openssl-dev libdb4.8-dev libjpeg-dev libpng12-dev libxpm-dev libfreetype6-dev libmysqlclient-dev postgresql-server-dev-9.1 libt1-dev libgd2-xpm-dev libgmp-dev libsasl2-dev libmhash-dev unixodbc-dev freetds-dev libpspell-dev libsnmp-dev libtidy-dev libxslt1-dev libmcrypt-dev

Compile and install PHP

Clone the PHP repository

sudo mkdir /opt/source && cd /opt/source
git clone && cd php-src

Choose the version of php you want to compile and install

  PHP 5.3:  git checkout PHP-5.3
  PHP 5.4:  git checkout PHP-5.4
  PHP 5.6:  git checkout PHP-5.6
  PHP HEAD: git checkout master

For building php from git repository, first you have to run sudo ./buildconf .

./buildconf generates the configuration script. This may take several minutes.

 Create the folder where you want to install the php:


sudo mkdir -p /opt/php-5.6
sudo mkdir -p /opt/php-5.5

 Configure build

Once the ./configure script is generated via the step above, you can use it to customize your PHP build. You can list all supported options using –help:

 ./configure \
--with-freetype=/usr/local \
--with-config-file-path=/opt/php-5.6/config.d \
--with-config-file-scan-dir=/opt/php-5.6/conf.d \
--prefix=/opt/php-5.6 \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-soap \
--enable-calendar \
--with-curl \
--with-mcrypt \
--with-zlib \
--with-gd \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-mysql \
--with-pdo-mysql \
--with-mysqli \
--with-png-dir=/usr \
--enable-gd-native-ttf \
--with-openssl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-libdir=lib \
--enable-ftp \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-gettext \
--with-gd \
--with-xpm-dir=/usr \
--with-ldap=/usr \
--with-jpeg-dir=/usr/lib/ \
--enable-fpm \
--with-fpm-user=www-data \
--with-fpm-group=www-data \

Compile PHP

Now, you can use make to perform the actual compilation:


 Install PHP

Now you can run “make install” to install PHP into /usr/local (default) or other directories by using the --prefixconfiguration. In this case it’s /opt/php-5.6

sudo make install

Please note that make install will not create an ini file.

/opt/php-5.6/bin/php --ini
Configuration File (php.ini) Path: /opt/php-5.6/lib
Loaded Configuration File:         (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)


Copy config files

sudo cp /opt/source/php-src/php-fpm.conf.default /opt/php-5.6/etc/php-fpm.conf
sudo cp /opt/source/php-src/php.ini-production /opt/php-5.6/lib/php.ini

Open /opt/php-5.6/etc/php-fpm.conf and adjust the settings in the listen line. You must change to an unused port (e.g. 9001)

Init script setup

You will probably want to create an init script for your new php-fpm. Luckily, PHP 5.3+ already provides it for you, simply copy the init script to your directory and change permissions:

sudo cp /opt/source/php-src/sapi/fpm/init.d.php-fpm /etc/init.d/php5.6-fpm
sudo chmod 755 /etc/init.d/php5.6-fpm

Your init script is ready. Now, you are able to start, stop and reload php-fpm:

sudo /etc/init.d/php5.6-fpm start
sudo /etc/init.d/php5.6-fpm stop
sudo /etc/init.d/php5.6-fpm reload

Create alias for every command:

alias p56start='sudo /etc/init.d/php5.6-fpm start'
alias p56stop='sudo /etc/init.d/php5.6-fpm stop'
alias p56r='sudo /etc/init.d/php5.6-fpm restart'

alias p53start='sudo /etc/init.d/php5.3-fpm start'
alias p53stop='sudo /etc/init.d/php5.3-fpm stop'
alias p53r='sudo /etc/init.d/php5.3-fpm restart'

alias nginxr='sudo service nginx restart'



Magento – BNR currency import module for RON

Automatically currency rate import for RON currency in your Magento e-commerce

You can download here:

Or you can create your own currency import module following next instructions, it is very simple:

Build your own Magento ecommerce – currency rate import extension

  1. declare your currency import model in etc/config.xml file
                            <name>BNR RON</name>
  2. Next, in the Bb_Bnr_Model_Currency_Import_Bnr class definition extend the class  Mage_Directory_Model_Currency_Import_Abstract 
  3. This class have to implement the function  protected function _convert($currencyFrom, $currencyTo, $retry=0). This function have to return the currency rate between currencyFrom and currencyTo as float.