Currently Viewing Posts Tagged PHP

php Benchmark test on php 7.2

Now it is a php 7.2. Included in the Ubuntu 18.04.

My site is running on this platform. I also want to try to run the php Benchmark test, and see how fast it is.

 

--------------------------------------
|        PHP BENCHMARK SCRIPT        |
--------------------------------------
Start : 2018-06-07 11:38:41
Server : webexample.win@50.116.5.237
PHP version : 7.2.5-0ubuntu0.18.04.1
Platform : Linux
--------------------------------------
test_ifelse               : 0.159 sec.
test_loops                : 0.249 sec.
test_stringmanipulation   : 0.394 sec.
test_math                 : 0.284 sec.
--------------------------------------
Total time:               : 1.086 sec.

I ran five times and the average time is 1.113 second.

Compare with the previous PHP.

  • php 7.0, 2.342 seconds
  • HHVM 5.6.99,  1.4 seconds
  • php 5.5, 5.646 seconds
  • php 5.4, 5.417 seconds
  • php 5.3, 6.567 seconds

Continue reading “php Benchmark test on php 7.2”

How to connect DBF file and read data by PHP script

Afte last post, my php-fpm support the dbase database file now.  Here is an example php script.

$db = dbase_open('../db/clients.dbf',0);

if ($db){
	$record_numbers = dbase_numrecords($db);
	
	for ($i = 1; $i <= $record_numbers; $i++) {
		  $row = dbase_get_record_with_names($db, $i);
		  if ( strpos(trim($row['CLIENTNAME']), $clientname)  !== FALSE and $row['deleted'] != 1) {
			  	$clientid =  $row['CLIENTID']);
				
				echo 'Customer ID Number: ',$clientid,';
                 }
     }
}

0) The dbase file has two columns, one is CLIENTID, the other one is CLIENTNAME.

1) Open the dbase file, clients.dbf,  by relative path.

2) If the dbf file exists, get the total number of records.

3) Go through all records. If field CLIENTNAME is same as the variable, $clientname,  print client id.

Remove link of comment in WordPress

By default all links posted in the comments under your articles are turned by WordpPress into links.

Unfortunately, this features encourages spammers to post many unwanted comments in your blog.

To disable turning URLs from comments into actual links, go to your admin area and then navigate to Appearance -> Editor.

remove-clickable-link

Select the functions.php file from the right column and add the following line just above the closing php tag ( ?> ):

remove_filter('comment_text', 'make_clickable', 9);

Then, I purged all caches and reloaded one post. Then there are no hyper-link in comments. Theses links are just pure text now, not clickable.

PHP – Parse error: syntax error, unexpected end of file

FastCGI sent in stderr: “PHP message: PHP Parse error: syntax error, unexpected end of file in /your_path/yourscript.php on line 308” while reading response header

This is the error when I check the php log file. Because I met the blank page with 500 internal error, which is loaded OK on the other server.

The real reason is I use (<? ?> ) and (<?php ?>) in mix. So I faced with this parse error when running the script on different systems:

Parse error: syntax error, unexpected $end in X on line Y

where X is the file path and Y the line number, usually the last line of the file. But the shotform may not there.

To avoid this error, refrain yourself from using the short form and stick to ( <?php ?>), despite the extra 3 characters. This will make your code universally accessible and it is essential if your developing for beyond your own personal use. If you insist in using the short form (<? ?> ), and you get this error, you can enable the short form option in the php.ini file.

short_open_tag=On

Keep in mind though, that the short form is supposed to be deprecated in PHP 6. If this goes through you will have to go back and change all your source files.

 

In the furture, I wil keep using long form of it only.

HHVM 3.6 has problem with MariaDB

The same script php file, always works smoothly on HHVM 3.2 to HHVM 3.5. Until I upgrade it to HHVM 3.6.

It is a simply script which use mysqli connection and so on. Now it is shown the error as below:

Connect failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

I tried to find the answer and faild. So I upgrde the HHVM to nightly build version. It is 3.7-dev.

davidyin@fiob:/etc/hhvm$ hhvm --version
HipHop VM 3.7.0-dev (rel)
Compiler: heads/master-0-g0359d4a4bbb22c5defd7eb024194aed0af24c80b
Repo schema: 9008d5e6e7f6584d09ed3b14deceac5a34f363c1

And it did solve the error, and the script is working as expected.

But, later I found the reason why it happen.

Add

hhvm.mysql.socket = /tmp/mysql.sock

into server.ini

To specify the mysql socket and let HHVM knows how to connect mySQL server or MariaDB server.

I did not try the solution.  I found it here.

Some changes on Timthumb script

What is Timthumb?

It is a small php script for cropping, zooming and resizing web images (jpg, png, gif).

 

Here is the official place to donwload the script. https://code.google.com/p/timthumb/

Even the developers said it is not supported any more, there are still a lot of sites or plugins of wordpress and joomla.

Here is some modification I did on one of the plugin of Joomla.

1) Change default quality of image file.

The original default is 90%, it is not good enough. Especially when testing the output on webpagetest.org. So reduce the default image quality to 70. Find the following in tb.php


if(! defined('DEFAULT_Q') )                                     define ('DEFAULT_Q', 70);                                                               // Default image quality. Allows overrid in timthumb-config.php

It can also be overrid in url paraments. Such as http://your.domain.name/tb.php?src=/images/abc.jpg&q=70

2) Add interlace or Make jpeg file progressive

It is good to make jpg file to be progressive. It is similar file size but make it display on the monitor faster.

This is actually a mod on the original timthumb script.

Add following in the defaults section of timthumb script.


if(! defined('JPEG_IS_PROGRESSIVE') )    define ('JPEG_IS_PROGRESSIVE', TRUE);

Then find following script


$imgType = 'jpg';
imagejpeg($canvas, $tempfile, $quality);

And add one line in the middle. Looks like below:


                        $imgType = 'jpg';
                        imageinterlace($canvas, JPEG_IS_PROGRESSIVE);
                        imagejpeg($canvas, $tempfile, $quality);

timthumb

 

 

PHP bench test results of PHP 5.3, PHP 5.4, and PHP 5.5

It is a bench test. Testing on the testing machine I just installed in the previous post.

Including PHP 5.3.28, PHP 5.4.28, and PHP 5.5.12, installed with fastcgi running on Apache and mod_fcgid.

PHP 5.3 is 6.567 seconds.

--------------------------------------
|        PHP BENCHMARK SCRIPT        |
--------------------------------------
Start : 2014-05-24 16:27:00
Server : web3.example.com@192.168.0.122
PHP version : 5.3.28
Platform : Linux
--------------------------------------
test_math                 : 1.724 sec.
test_stringmanipulation   : 1.741 sec.
test_loops                : 1.852 sec.
test_ifelse               : 1.250 sec.
--------------------------------------
Total time:               : 6.567 sec.

PHP 5.4 is 5.417 seconds.

--------------------------------------
|        PHP BENCHMARK SCRIPT        |
--------------------------------------
Start : 2014-05-24 23:27:23
Server : web2.example.com@192.168.0.122
PHP version : 5.4.28
Platform : Linux
--------------------------------------
test_math                 : 1.556 sec.
test_stringmanipulation   : 1.664 sec.
test_loops                : 1.328 sec.
test_ifelse               : 0.869 sec.
--------------------------------------
Total time:               : 5.417 sec.

 

PHP 5.5 is 5.646 seconds.

--------------------------------------
|        PHP BENCHMARK SCRIPT        |
--------------------------------------
Start : 2014-05-24 23:27:15
Server : web1.example.com@192.168.0.122
PHP version : 5.5.12
Platform : Linux
--------------------------------------
test_math                 : 1.618 sec.
test_stringmanipulation   : 1.723 sec.
test_loops                : 1.363 sec.
test_ifelse               : 0.942 sec.
--------------------------------------
Total time:               : 5.646 sec.

 

I Repeated the test many times and in different order. The results is consistent.

PHP 5.4 is a little bit faster than PHP 5.5. Both PHP 5.5 and PHP 5.4 are almost 1 second faster than PHP 5.3.

Install multiple version of php on CentOS 6.5

This is a how-to of install php 5.3 an dphp 5.5 on one server, CentOS and use them simultaneously.

It is based on CentOS 6.5, 64bit for Apache only.

1) Enable rpmforge and epel yum repository


wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm

 

2) Install php 5.3

CentOS has php 5.3 built-in-box, simply install it with yum


yum install php php-mysql php-mbstring php-mcrypt

3) Compile and install php 5.5 from source

For php 5.5, we prefer to install it from source, the point is to install php at different location, which does not conflict with the php 5.3.

PHP 5.3 is installed as an apache module, we can only use one version of php at the same time. If we need to run different version of php at the same time, fastcgi and php-fpm works on it.

Build and install php 5.5 with fpm enabled, the latest version of php 5.5 is php 5.5.12.

3.1) Install required dev packages

yum install gcc libxml2-devel bzip2-devel zlib-devel \
	curl-devel libmcrypt-devel libjpeg-devel \
	libpng-devel gd-devel mysql-devel

3.2) Compile and install


wget http://ca2.php.net/get/php-5.5.12.tar.bz2/from/this/mirror

tar -xjf mirror

cd php-5.5.12
./configure --prefix=/usr/local/php55 \
	--with-config-file-path=/etc/php55 \
	--with-config-file-scan-dir=/etc/php55/php.d \
	--enable-fpm \
	--with-fpm-user=apache \
	--with-fpm-group=apache \
	--with-libdir=lib64 \
	--with-mysql \
	--with-mysqli \
	--enable-mbstring \
	--disable-debug \
	--disable-rpath \
	--with-bz2 \
	--with-curl \
	--with-gettext \
	--with-iconv \
	--with-openssl \
	--with-gd \
	--with-mcrypt \
	--with-pcre-regex \
	--with-zlib

make -j4 && sudo make install
mkdir /etc/php55
cp php.ini-production /etc/php55/php.ini
sed -i -e 's#php_fpm_CONF=\${prefix}/etc/php-fpm.conf#php_fpm_CONF=/etc/php55/php-fpm.conf#' \
	sapi/fpm/init.d.php-fpm
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod a+x /etc/init.d/php-fpm
/sbin/chkconfig --add php-fpm
/sbin/chkconfig php-fpm on
cp sapi/fpm/php-fpm.conf /etc/php55/

3.3) Configure php-fpm
Edit /etc/php55/php-fpm.conf, change some settings. This step is mainly to uncomment some settings.

vi /etc/php55/php-fpm.conf
pid = run/php-fpm.pid
listen = 127.0.0.1:9000
pm.max_children = 20
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15

Then, start php-fpm

/etc/init.d/php-fpm start

3.4) Install and setup mod_fastcgi

yum install libtool httpd-devel apr-devel
wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz
tar -xzf mod_fastcgi-current.tar.gz
cd mod_fastcgi-2.4.6
cp Makefile.AP2 Makefile
make top_dir=/usr/lib64/httpd/ install
sh -c "echo 'LoadModule fastcgi_module modules/mod_fastcgi.so' > /etc/httpd/conf.d/mod_fastcgi.conf"

4) Prepare hosts and add web sites settings
4.1) Add the following line to /etc/hosts

127.0.0.1 php53.example.com php55.example.com

4.2) Create web document root and drop and index.php under it to show php information.

mkdir /var/www/fcgi-bin
for i in {1..2}; do
	web_root=/var/www/web$i
	mkdir $web_root
	echo "<?php phpinfo(); ?>" > $web_root/index.php
done

4.3) Create Apache config file(append it to httpd.conf)

vi /etc/httpd/conf/httpd.conf

Modify httpd.conf Per below

NameVirtualHost *:80

# module settings
# mod_fastcgi with php-fpm

<IfModule mod_fastcgi.c>
        FastCgiExternalServer /var/www/fcgi-bin/php-fpm -host 127.0.0.1:9000
</IfModule>

# virtual hosts...

#################################################################
#1st virtual host, use mod_php, run php-5.3.3
#################################################################
<VirtualHost *:80>
        ServerName php53.example.com
        DocumentRoot "/var/www/web1"

        <ifmodule mod_php5.c>
                <FilesMatch \.php$>
                        AddHandler php5-script .php
                </FilesMatch>
        </IfModule>

        <Directory "/var/www/web1">
                DirectoryIndex index.php index.html index.htm
                Options -Indexes FollowSymLinks
                Order allow,deny
                Allow from all
        </Directory>

</VirtualHost>

#################################################################
#2nd virtual host, use mod_fastcgi + php-fpm, run php-5.5.12
#################################################################
<VirtualHost *:80>
        ServerName php55.example.com
        DocumentRoot "/var/www/web2"


        <IfModule mod_fastcgi.c>
                ScriptAlias /fcgi-bin/ /var/www/fcgi-bin/
                AddHandler php5-fastcgi .php
                Action php5-fastcgi /fcgi-bin/php-fpm
        </IfModule>

        <Directory "/var/www/web2">
                DirectoryIndex index.php index.html index.htm
                Options -Indexes FollowSymLinks +ExecCGI
                Order allow,deny
                Allow from all
        </Directory>

</VirtualHost>

4.4) Restart apache. Visit these two sites and view phpinfo page.

service httpd restart

http://php53.example.com
php53-example
http://php55.example.com
php55-example

You can use the one of the virtual host as a template to create new virtual host.

PHP 5.3, PHP5.4 and PHP5.5

phplogo

PHP, as a program language, has its only life cycle.

The main three version of PHP is 5.3, 5.4, and 5.5.

PHP5.3 will be supported until July, 2014. It is still the main default version in CentOS.

PHP5.4 will be supported until March, 2015

PHP 5.5 will be supported until June, 2016.

Each version of PHP has three years life from the release date.

Continue reading “PHP 5.3, PHP5.4 and PHP5.5”

  • Archives