in Linux, PHP

Using multiple php inside one Ubuntu machine

programmer plus gamerAnother challenge came. One of our application finally updated. The application development use php 5.3 + and already pass with php 5.5+ version. Time for migration schedule and the requirement is obvious : the machine able to run 2 different php (php 5.2.17 is a must and other php version is at least 5.3+). The machine I use is Ubuntu 12.04 32 bit ( I prefer to use old supported version due to lower memory consumption than 14.04).

Quick google give me reference to phpfarm and other resources. As I said in previous post, php 5.2.17 successfully installed after applied a patch (installed on Ubuntu 14.04). For reference purpose I share my experience here. Target php extensions are standard like support for mysql, pdo and gd extension.

Note : if you want to to try / apply it using vps then You’ll need memory minimal 1GB. My box show this memory utilization (32 bit). Click here for hourly cheap vps.

# free -m
total       used       free     shared    buffers     cached
Mem:           873        786         87          0         78        569
-/+ buffers/cache:        138        734
Swap:          890          0        890

Disable php5 module (optional)

In case you already install php5 as apache module you must disable it first.

#a2dismod php5

Fixing locale

Locale error are pop up everywhere first time I install. I need to get rid from this error.

# locale

locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE=”en_US.UTF-8″
LC_NUMERIC=id_ID.UTF-8
LC_TIME=id_ID.UTF-8
LC_COLLATE=”en_US.UTF-8″
LC_MONETARY=id_ID.UTF-8
LC_MESSAGES=”en_US.UTF-8″
LC_PAPER=id_ID.UTF-8
LC_NAME=id_ID.UTF-8
LC_ADDRESS=id_ID.UTF-8
LC_TELEPHONE=id_ID.UTF-8
LC_MEASUREMENT=id_ID.UTF-8
LC_IDENTIFICATION=id_ID.UTF-8
LC_ALL=

# nano /etc/environment

add these path ::

PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games”
LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8

save en restart.

reboot en retest the command ::

# locale

LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE=”en_US.UTF-8″
LC_NUMERIC=”en_US.UTF-8″
LC_TIME=”en_US.UTF-8″
LC_COLLATE=”en_US.UTF-8″
LC_MONETARY=”en_US.UTF-8″
LC_MESSAGES=”en_US.UTF-8″
LC_PAPER=”en_US.UTF-8″
LC_NAME=”en_US.UTF-8″
LC_ADDRESS=”en_US.UTF-8″
LC_TELEPHONE=”en_US.UTF-8″
LC_MEASUREMENT=”en_US.UTF-8″
LC_IDENTIFICATION=”en_US.UTF-8″
LC_ALL=en_US.UTF-8

great, no more error warning. Lets continue.

Update en upgrade.

# apt-get update && sudo apt-get upgrade

install lamp-server

# apt-get install lamp-server^

Install essential and friends

# apt-get install build-essential git apache2-mpm-worker libapache2-mod-fastcgi php5-fpm libmcrypt-dev libgd2-xpm-dev

php5 dependency

#apt-get build-dep php5

Cloning phpfarm source

# git clone https://github.com/cweiske/phpfarm.git /opt/phpfarm

Cloning into ‘/opt/phpfarm’…
remote: Counting objects: 175, done.
remote: Total 175 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (175/175), 35.16 KiB | 31 KiB/s, done.
Resolving deltas: 100% (64/64), done.

# cd /opt/phpfarm/src

Creating custom option file :

#nano custom-options-5.2.17.sh

Fill these lines :

configoptions=”
–enable-cli \
–with-pear \
–with-iconv \
–with-curl \
–with-mysqli \
–with-mysql –with-libdir=/lib/i386-linux-gnu \
–enable-mbstring \
–enable-exif \
–with-gd \
–enable-gd \
–with-zlib \
–with-zlib-dir \
–with-gettext \
–with-mhash \
–enable-ftp \
–with-pspell \
–enable-bcmath \
–with-mime-magic \
–with-pdo-mysql \
–enable-sockets \
–enable-soap \
–enable-calendar \
–enable-fastcgi \
–enable-force-cgi-redirect \

save the file.

#chmod +x custom-options-5.2.17.sh

A few notes around custom options :

– enable-fastcgi will give fcgi version. Without the line will give cgi version.

Compile

# ./compile.sh 5.2.17

compiling process will work till give this result :

Build complete.
Don’t forget to run ‘make test’.

Installing PHP SAPI module:       cgi
Installing PHP CGI binary: /opt/phpfarm/inst/php-5.2.17/bin/
Installing PHP CLI binary:        /opt/phpfarm/inst/php-5.2.17/bin/
Installing PHP CLI man page:      /opt/phpfarm/inst/php-5.2.17/man/man1/
Installing build environment:     /opt/phpfarm/inst/php-5.2.17/lib/php/build/
Installing header files:          /opt/phpfarm/inst/php-5.2.17/include/php/
Installing helper programs:       /opt/phpfarm/inst/php-5.2.17/bin/
program: phpize
program: php-config
Installing man pages:             /opt/phpfarm/inst/php-5.2.17/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment:      /opt/phpfarm/inst/php-5.2.17/lib/php/
[PEAR] Archive_Tar    – installed: 1.3.7
[PEAR] Console_Getopt – installed: 1.2.3
[PEAR] Structures_Graph- installed: 1.0.3
[PEAR] XML_Util       – installed: 1.2.1
[PEAR] PEAR           – installed: 1.9.1
Wrote PEAR system config file at: /opt/phpfarm/inst/php-5.2.17/etc/pear.conf
You may want to add: /opt/phpfarm/inst/php-5.2.17/lib/php to your php.ini include_path
Installing PDO headers:          /opt/phpfarm/inst/php-5.2.17/include/php/ext/pdo/

In case you get this error :

/opt/phpfarm/src/php-5.2.17/ext/dom/node.c: In function ‘zim_domnode_getNodePath’:
/opt/phpfarm/src/php-5.2.17/ext/dom/node.c:2002:8: warning: pointer targets in assignment differ in signedness [-Wpointer-sign]
value = xmlGetNodePath(nodep);
^
make: *** [ext/dom/node.lo] Error 1
make failed.

You may refer to my previous post here.

Checking php version and cgi / fcgi mode :

# /opt/phpfarm/inst/php-5.2.17/bin/php-cgi -v

PHP 5.2.17 (cgi-fcgi) (built: Oct  7 2014 05:36:03) (DEBUG)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies

Compile another version (5.3.29) using same options :

# cp custom-options-5.2.17.sh custom-options-5.3.29.sh

# ./compile.sh 5.3.29

Installing PHP SAPI module:       cgi
Installing PHP CGI binary: /opt/phpfarm/inst/php-5.3.29/bin/
Installing PHP CLI binary:        /opt/phpfarm/inst/php-5.3.29/bin/
Installing PHP CLI man page:      /opt/phpfarm/inst/php-5.3.29/man/man1/
Installing build environment:     /opt/phpfarm/inst/php-5.3.29/lib/php/build/
Installing header files:          /opt/phpfarm/inst/php-5.3.29/include/php/
Installing helper programs:       /opt/phpfarm/inst/php-5.3.29/bin/
program: phpize
program: php-config
Installing man pages:             /opt/phpfarm/inst/php-5.3.29/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment:      /opt/phpfarm/inst/php-5.3.29/lib/php/
[PEAR] Archive_Tar    – installed: 1.3.12
[PEAR] Console_Getopt – installed: 1.3.1
[PEAR] Structures_Graph- installed: 1.0.4
[PEAR] XML_Util       – installed: 1.2.3
[PEAR] PEAR           – installed: 1.9.5
Wrote PEAR system config file at: /opt/phpfarm/inst/php-5.3.29/etc/pear.conf
You may want to add: /opt/phpfarm/inst/php-5.3.29/lib/php to your php.ini include_path
/opt/phpfarm/src/php-5.3.29/build/shtool install -c ext/phar/phar.phar /opt/phpfarm/inst/php-5.3.29/bin
ln -s -f /opt/phpfarm/inst/php-5.3.29/bin/phar.phar /opt/phpfarm/inst/php-5.3.29/bin/phar
Installing PDO headers:          /opt/phpfarm/inst/php-5.3.29/include/php/ext/pdo/
–2014-10-07 06:09:53–  http://pear2.php.net/pyrus.phar
Resolving pear2.php.net (pear2.php.net)… 5.77.39.20
Connecting to pear2.php.net (pear2.php.net)|5.77.39.20|:80… connected.

HTTP request sent, awaiting response… 200 OK
Length: 10574182 (10M) [application/octet-stream]
Saving to: `/opt/phpfarm/src/bzips/pyrus.phar’

100%[==============================================================================================================================>] 10,574,182   124K/s   in 89s

2014-10-07 06:11:25 (116 KB/s) – `/opt/phpfarm/src/bzips/pyrus.phar’ saved [10574182/10574182]

Pyrus version 2.0.0a4 SHA-1: 72271D92C3AA1FA96DF9606CD538868544609A52
Using PEAR installation found at /opt/phpfarm/inst/php-5.3.29/pear
Setting php_prefix in system paths.

done.

At this time I have version 5.2.17, 5.3.29 compiled by phpfarm.

# /opt/phpfarm/inst/php-5.3.29/bin/php-cgi -v

PHP 5.3.29 (cgi-fcgi) (built: Oct  7 2014 06:09:32) (DEBUG)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies

Enable apache module.

# a2enmod actions fastcgi alias

Enabling module actions.
Module fastcgi already enabled
Module alias already enabled
To activate the new configuration, you need to run:
service apache2 restart

FastCgi Server

# nano /etc/apache2/conf.d/php-cgisetup.conf

Fill these lines :

FastCgiServer /var/www/cgi-bin/php-cgi-5.2.17
FastCgiServer /var/www/cgi-bin/php-cgi-5.3.29
ScriptAlias /cgi-bin-php/ /var/www/cgi-bin/

save.

Create cgi-bin

# mkdir /var/www/cgi-bin

Creating each fastcgi configuration file (for php 5.2.17 and php 4.3.29)

# nano /var/www/cgi-bin/php-cgi-5.2.17

fill these lines :

#!/bin/sh
PHP_FCGI_CHILDREN=3
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /opt/phpfarm/inst/bin/php-cgi-5.2.17

save.

# nano /var/www/cgi-bin/php-cgi-5.3.29

fill these lines :

#!/bin/sh
PHP_FCGI_CHILDREN=3
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /opt/phpfarm/inst/bin/php-cgi-5.3.29

save.

Note : for Ubuntu 14.04 location for cgi configuration located in /etc/apache2/conf-available/ . To enable the configuration simply use command :

# a2enconf php-cgisetup.conf

Change ownership

# chown -R www-data:www-data /var/www/cgi-bin

Set permission

# chmod -R 0744 /var/www/cgi-bin

Testing

Create entry on hosts

# nano /etc/hosts

Add these lines :

127.0.0.1 php5217
127.0.0.1 php5329

save.

Edit virtualhost file

# nano /etc/apache2/sites-available/default

Add these lines :

<VirtualHost *:80>
ServerName php5217
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All
AddHandler php-cgi .php
Action php-cgi /cgi-bin-php/php-cgi-5.2.17
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>

<VirtualHost *:80>
ServerName php5329
DocumentRoot /var/www
<Directory />virtual host
Options FollowSymLinks
AllowOverride All
AddHandler php-cgi .php
Action php-cgi /cgi-bin-php/php-cgi-5.3.29
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>

save.

# cd /var/www/
# nano info.php

fill these lines :

<?php
phpinfo();
?>

# service apache2 restart
* Restarting web server apache2                                                                                                                                         … waiting .                                                                                                                                                   [ OK ]
#

View it from lynx (non graphic browser)

# lynx php5217/info.php

PHP Logo

PHP Version 5.2.17

System Linux banpustaka 3.8.0-29-generic #42~precise1-Ubuntu SMP Wed Aug 14 15:31:16 UTC 2013 i686
Build Date Oct 7 2014 05:35:37
Configure Command ‘./configure’ ‘–prefix=/opt/phpfarm/inst/php-5.2.17’ ‘–exec-prefix=/opt/phpfarm/inst/php-5.2.17’ ‘–enable-debug’ ‘–disable-short-tags’
‘–without-pear’ ‘–enable-cli’ ‘–with-pear’ ‘–with-iconv’ ‘–with-curl’ ‘–with-mysqli’ ‘–with-mysql’ ‘–with-libdir=/lib/i386-linux-gnu’
‘–enable-mbstring’ ‘–enable-exif’ ‘–with-gd’ ‘–enable-gd’ ‘–with-zlib’ ‘–with-zlib-dir’ ‘–with-gettext’ ‘–with-mhash’ ‘–enable-ftp’ ‘–with-pspell’
‘–enable-bcmath’ ‘–with-mime-magic’ ‘–with-pdo-mysql’ ‘–enable-sockets’ ‘–enable-soap’ ‘–enable-calendar’ ‘–enable-fastcgi’
‘–enable-force-cgi-redirect’
Server API CGI/FastCGI
Virtual Directory Support disabled
Configuration File (php.ini) Path /opt/phpfarm/inst/php-5.2.17/lib
Loaded Configuration File /opt/phpfarm/inst/php-5.2.17/lib/php.ini
Scan this dir for additional .ini files (none)
additional .ini files parsed (none)
PHP API 20041225
PHP Extension 20060613
Zend Extension 220060519
Debug Build yes
Thread Safety disabled
Zend Memory Manager enabled
IPv6 Support enabled
Registered PHP Streams compress.zlib, php, file, data, http, ftp
Registered Stream Socket Transports tcp, udp, unix, udg
Registered Stream Filters zlib.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed
Zend logo This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies

another php (5.3.29) :

# lynx php5217/info.php

PHP Logo

PHP Version 5.3.29

System Linux banpustaka 3.8.0-29-generic #42~precise1-Ubuntu SMP Wed Aug 14 15:31:16 UTC 2013 i686
Build Date Oct 7 2014 06:09:00
Configure Command ‘./configure’ ‘–prefix=/opt/phpfarm/inst/php-5.3.29’ ‘–exec-prefix=/opt/phpfarm/inst/php-5.3.29’ ‘–enable-debug’ ‘–disable-short-tags’
‘–without-pear’ ‘–enable-cli’ ‘–with-pear’ ‘–with-iconv’ ‘–with-curl’ ‘–with-mysqli’ ‘–with-mysql’ ‘–with-libdir=/lib/i386-linux-gnu’
‘–enable-mbstring’ ‘–enable-exif’ ‘–with-gd’ ‘–enable-gd’ ‘–with-zlib’ ‘–with-zlib-dir’ ‘–with-gettext’ ‘–with-mhash’ ‘–enable-ftp’ ‘–with-pspell’
‘–enable-bcmath’ ‘–with-mime-magic’ ‘–with-pdo-mysql’ ‘–enable-sockets’ ‘–enable-soap’ ‘–enable-calendar’ ‘–enable-fastcgi’
‘–enable-force-cgi-redirect’
Server API CGI/FastCGI
Virtual Directory Support disabled
Configuration File (php.ini) Path /opt/phpfarm/inst/php-5.3.29/lib
Loaded Configuration File /opt/phpfarm/inst/php-5.3.29/lib/php.ini
Scan this dir for additional .ini files (none)
Additional .ini files parsed (none)
PHP API 20090626
PHP Extension 20090626
Zend Extension 220090626
Zend Extension Build API220090626,NTS,debug
PHP Extension Build API20090626,NTS,debug
Debug Build yes
Thread Safety disabled
Zend Memory Manager enabled
Zend Multibyte Support disabled
IPv6 Support enabled
Registered PHP Streams compress.zlib, php, file, glob, data, http, ftp, phar
Registered Stream Socket Transports tcp, udp, unix, udg
Registered Stream Filters zlib.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk
Zend logo This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies

Great. Looks like everything work well. Next work is configure dns server to start migration process.

Reference :

http://www.distrogeeks.com/install-multiple-php-versions-in-ubuntu-lamp-server/
http://www.metod.si/multiple-php-versions-with-apache-2-fastcgi-phpfarm-on-ubuntu/
http://zgadzaj.com/how-to-install-php-53-and-52-together-on-ubuntu-1204