Virtualmin – upgrate PHP 5.3 to PHP 5.4 / PHP 5.5 or higher version from Yum

Upgrade PHP 5.3 default version in Virtualmin / webmin to PHP 5.3 / php 5.5 in Centos:

Virtualmin / webmin comes with default php5.3 version while higher version is only available in pro version. This article helps how to update php versions  to higher version like php5.5.

 

Step 1:  Install PHP version:

For centos 6:

rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm

For centos 5:

rpm -Uvh http://mirror.webtatic.com/yum/el5/latest.rpm

Now install php by:

To install php 5.4 version:

yum install php54w php54w-common php54w-mysql php54-mbstring

To install php 5.5 version

yum install php55w php55w-opcache php55w-common php55w-mysql php55-mbstring

More module can be added by simple apending the module name at the end of yum or can be installed sepereatly.

List of modules are:

Package Provides
php54w mod_php, php54w-zts
php54w-bcmath
php54w-cli php-cgi, php-pcntl, php-readline
php54w-common php-api, php-bz2, php-calendar, php-ctype, php-curl, php-date, php-exif, php-fileinfo, php-ftp, php-gettext, php-gmp, php-hash, php-iconv, php-json, php-libxml, php-openssl, php-pcre, php-pecl-Fileinfo, php-pecl-phar, php-pecl-zip, php-reflection, php-session, php-shmop, php-simplexml, php-sockets, php-spl, php-tokenizer, php-zend-abi, php-zip, php-zlib
php54w-dba
php54w-devel
php54w-embedded php-embedded-devel
php54w-enchant
php54w-fpm
php54w-gd
php54w-imap
php54w-interbase php_database, php-firebird
php54w-intl
php54w-ldap
php54w-mbstring
php54w-mcrypt
php54w-mssql
php54w-mysql php-mysqli, php_database
php54w-mysqlnd php-mysqli, php_database
php54w-odbc php-pdo_odbc, php_database
php54w-pdo
php54w-pecl-apc
php54w-pecl-memcache
php54w-pecl-zendopcache
php54w-pecl-xdebug
php54w-pgsql php-pdo_pgsql, php_database
php54w-process php-posix, php-sysvmsg, php-sysvsem, php-sysvshm
php54w-pspell
php54w-recode
php54w-snmp
php54w-soap
php54w-tidy
php54w-xml php-dom, php-domxml, php-wddx, php-xsl
php54w-xmlrpc

Each module can be configured with ini files which are inside /etc/php.d/<module name>.ini

Here we are installing php 5.5 so in above module we replace php54w with php55w . example , php54w-mysql will be php55w-mysql in php 5.5

Step 2: setup PHP 5.5 version in apache

a) Create cgi file < if using php-cgi)

Go to your domain name:

/home/<username>/cgi-bin/

Copy existing cgi file lets say 5.4.cgi to new version cgi, in this case 5.5.cgi

cd /home/<username>/cgi-bin/
cp php5.4.cgi php5.5.cgi

edit cgi file

vi php.5.5.cgi

and replace with

#!/bin/bash
 PHPRC=$DOCUMENT_ROOT/../etc/php5 # your php version
 export PHPRC
 umask 022
 if [ "$REDIRECT_URL" != "" ]; then
 SCRIPT_NAME=$REDIRECT_URL
 export SCRIPT_NAME
 fi
 SCRIPT_FILENAME=$PATH_TRANSLATED
 export SCRIPT_FILENAME
 exec /usr/bin/php-cgi  # path of php-cgi

 

Give proper permission to your cgi files by:

chown <username>:<username> php5.5.cgi
chmod +x php5.5.cgi

b) create new PHP 5.5 fcgi file ( if using fcgi php)

go to

cd /home/<username>/fcgi-bin/

copy existing fcgi file to new php5.5 fcgi

cp php5.fcgi php5.5.fcgi

vi php5.5.fcgi

replace with following

#!/bin/bash
 PHPRC=$PWD/../etc/php5
 export PHPRC
 umask 022
 export PHP_FCGI_CHILDREN
 PHP_FCGI_MAX_REQUESTS=99999
 export PHP_FCGI_MAX_REQUESTS
 SCRIPT_FILENAME=$PATH_TRANSLATED
 export SCRIPT_FILENAME
 exec /usr/bin/php-cgi

 

Give proper permission to your cgi files by:

chown <username>:<username> php5.5.fcgi
chmod +x php5.5fcgi

 

c) Change path toPHP 5.5 cgi in apache httd.

Now final step is to edit apache httpd file to point to new version of php.

vi /etc/httpd/conf/httpd.conf

go to your domain virtual hosting settings.

i) For fcgi based domain

Remove current FCGIWrapper and replace with following :

FCGIWrapper /home/<username>/fcgi-bin/php5.5.fcgi .php

 

ii) For cgi based domain

Remove existing Action and AddType and replace with follows:

Action application/x-httpd-php5.5 /cgi-bin/php5.5.cgi
 AddType application/x-httpd-php5.5 .php

 

3) Step 3:  Restart apache server:

service httpd restart.

Understanding Generators in PHP

What is PHP Generators ?

As per the php wiki “A generator function looks just like a normal function, except that instead of returning a value, a generator yields as many values as it needs to.”

so what that means?
In general,  generators  functions allow you to declare a function that behaves like an iterator by yielding value and saving its state so that when its called again; iteration can continue. This is very helpful to save memory  when iterating large data set.

Example:

Lets take a example of simple range iteration:

function my_range($start,$end)  {
    $out = [];
    for($i=$start; $i<=$end; $i++) {
        $out[] = $i;
    }
    return $out;
}

foreach( y_range(0,1000) as $j) {
//logic here
}

 In this example, my_range() function will store every output in array, since the array is big we need more memory to store. So if the range set is too high, most probably we will run out of memory. 

Lets do same thing  with Generators:

function my_range($start,$end)  {
       for($i=$start; $i<=$end; $i++) {
           yield $i;
       }
}

foreach( y_range(0,1000) as $j) {
//logic here
}

In above example, each time foreach is run only one value is return, and state (instance) is saved in “yield” so next time it will call next() and continue. This way each time we just need integer memory space each time that will save lots of memory and also results in performance.

Injection:
Apart from getting value we can also pass value to generators

Eample:

function my_range($start,$end)  {
       for($i=$start; $i<=$end; $i++) {
       $command = (yield $i); 
       if($command =='terminate')
           return;
       }
}
$iterator = y_range(0,1000);
foreach($iterator as $j) { //logic here
    if(//condition) {
        $iterator->send('terminate');

}
}

send function is use to send command back to generator.

Additional functions:

Since generators implements iterator interface class we can use additional functions:

current ( void )
 scalar key ( void )
 void next ( void )
 void rewind ( void )
 boolean valid