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
    • Generate the CSR
  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
    • 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. Download all sources and dependencies for nginx
  3. 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”
  4. Compile and install
  5. 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.
  6. 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
  4. Upgrade

  5. If something was wrong until now you can remove previous version and separately install the new one.
  6. You can restore the information by

Install PHP 7 and xdebug for development environement

PHP 7 was released

Compile PHP 7 from source for development environement

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

 

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.

https://customers.microsoft.com/Pages/CustomerStory.aspx?recid=20822

“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 https://github.com/phusion/passenger_apt_automation.git
git clone https://github.com/FRiCKLE/ngx_cache_purge.git
git clone https://github.com/openssl/openssl.git

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/nginx.pid --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
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.

Features

  • 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

Priceing

  • 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.

PHP vs HHVM

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.

grafic-tranzactiii-php-vs-hhvm

Requests/min / Concurent users

See the performance gain in the following graph

performance-gain-PHP-HHVM

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

response-time-hhvm-php

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.

availability

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 https://github.com/zendtech/ZendOptimizerPlus
  • Configure the compilation with your php config
  • Build the package
  • Install will copy the binary file to your extension directory (or you can copy by your own)

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

 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
  • Extract files and identify the default values for following parameters PHP Extension,
    Zend Extension using phpize
  • 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
  • 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 “extension=apc.so” > /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

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.

notepad-search-and-replace

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:

 

Manage RDS instances through AWS CLI

Introduction

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

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).

rds-modify-db-instance

Read more

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.

PHP FPM

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

Nginx

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:

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:

 

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

Compile and install PHP

Clone the PHP repository

Choose the version of php you want to compile and install

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:

 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:

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

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

 

Copy config files

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:

1
2
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:

1
2
3
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:

 

 

Magento – BNR currency import module for RON

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

You can download here: https://github.com/georgebabarus/mage-bnr-currency-import

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
  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.