How to Install Multiple PHP version (PHP 5.2, PHP 5.5, PHP 5.6 and PHP 7) in single VPS with Ubuntu 16.04


Limited Time Promo, Get Free $20 credit from Vultr, click the banner to get it.

I need to test the effect of new plugin on new development stage while still preserve production setting. Not in high traffic domain and I also want to learn new framework called phalcon php. Since I want to support newest ubuntu server I came with my requirement list :

  1. Must be able to run on newest Ubuntu (I’m aware about 16.04 LTS)
  2. Must be able to run old PHP version (the latest of 5.2 release is 5.2.17). Even this version is no longer supported by php.net but a few products still rely on this version.
  3. I want to learn Phalcon PHP version 2.0.x .
  4. I also want to compare it with latest version of Phalcon PHP version 2.1.x
  5. Newest PHP version 7 also needed

The list translated into plan :

5.2 -> 5.2.17
5.5 -> 5.5.32 -> phalcon 2.0.10 (latest)
5.6 -> 5.6.18 -> phalcon 2.1 (latest)
7.0 -> 7.0.3

Well, I use Vultr since this vps working well for me for long time and also support iso (yes, I use Ubuntu 16.04 server daily build). (EU) Frankfurt, DE chosen as vps location for this server. 64 bit OS (Xenial Server).

ubuntu16

I also pointing A record for these sub domain.

ubuntu162

Lets get start!

Install Nginx and friends on Ubuntu 16.04

$ sudo apt-get install -y nginx git autoconf lynx wget build-essential libxml2-dev libssl-dev libbz2-dev libcurl4-openssl-dev  libpng12-dev libfreetype6-dev libxpm-dev libmcrypt-dev libmhash-dev libmysqlclient-dev libjpeg62-dev freetds-dev libjson-c-dev re2c php5 php5-json php5-dev nano

Setting Local Environment on Ubuntu 16.04

$ sudo nano /etc/environment

Add these lines :

LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8

save.

$ sudo reboot

Download all PHP source

$ sudo mkdir /opt/source
$ cd /opt/source
$ sudo wget -c http://museum.php.net/php5/php-5.2.17.tar.bz2
$ sudo wget -c http://au1.php.net/get/php-5.5.32.tar.bz2/from/this/mirror -O php-5.5.32.tar.bz2
$ sudo wget -c http://ar2.php.net/get/php-5.6.18.tar.bz2/from/this/mirror -O php-5.6.18.tar.bz2
$ sudo wget -c http://at2.php.net/get/php-7.0.3.tar.bz2/from/this/mirror -O php-7.0.3.tar.bz2

$ ls -l
total 49700
-rw-r–r– 1 root root 9092312 Jan 7 2011 php-5.2.17.tar.bz2
-rw-r–r– 1 root root 13685561 Feb 4 10:30 php-5.5.32.tar.bz2
-rw-r–r– 1 root root 14094993 Feb 3 12:10 php-5.6.18.tar.bz2
-rw-r–r– 1 root root 14011153 Feb 2 17:50 php-7.0.3.tar.bz2

Extract PHP source

$ sudo tar xvjf php-5.2.17.tar.bz2
$ sudo tar xvjf php-5.5.32.tar.bz2
$ sudo tar xvjf php-5.6.18.tar.bz2
$ sudo tar xvjf php-7.0.3.tar.bz2

Special patch for PHP 5.2.17

$ cd /opt/source/php-5.2.17

SimpleXML patch for PHP 5.2.17

$ sudo curl -o php-5.2.17.patch https://mail.gnome.org/archives/xml/2012-August/txtbgxGXAvz4N.txt
$ sudo patch -p0 -b < php-5.2.17.patch

PHP-FPM patch for PHP 5.2.17

$ sudo wget -c http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
$ sudo gunzip php-5.2.17-fpm-0.5.14.diff.gz
$ sudo patch -p1 < php-5.2.17-fpm-0.5.14.diff

OpenSSL Patch for PHP 5.2.17

$ sudo wget -c -t 3 -O ./debian_patches_disable_SSLv2_for_openssl_1_0_0.patch https://bugs.php.net/patch-display.php\?bug_id\=54736\&patch\=debian_patches_disable_SSLv2_for_openssl_1_0_0.patch\&revision=1305414559\&download\=1
$ sudo patch -p1 -b < debian_patches_disable_SSLv2_for_openssl_1_0_0.patch

Freetype support for PHP 5.2.17

$ sudo mkdir -pv /usr/include/freetype2/freetype
$ sudo ln -sf /usr/include/freetype2/freetype.h /usr/include/freetype2/freetype/freetype.h

Compile and Install PHP 5.2.17 on Ubuntu 16.04

$ cd /opt/source/php-5.2.17

$ sudo ./configure –enable-cli \
–prefix=/opt/source/php-5.2.17 \
–with-pear \
–without-iconv \
–with-curl \
–with-mysqli \
–with-mysql –with-libdir=/lib/x86_64-linux-gnu \
–enable-mbstring \
–enable-exif \
–with-gd \
–with-zlib \
–with-zlib-dir \
–with-gettext \
–with-mhash \
–enable-ftp \
–enable-bcmath \
–with-mime-magic \
–with-pdo-mysql \
–enable-sockets \
–enable-soap \
–enable-calendar \
–enable-fastcgi \
–enable-force-cgi-redirect \
–enable-fpm

$ sudo make
$ sudo make install

Copy php.ini

$ sudo cp /opt/source/php-5.2.17/php.ini-dist /opt/source/php-5.2.17/lib/php.ini

Check it.

$ /opt/source/php-5.2.17/bin/php-cgi -v
PHP 5.2.17 (cgi-fcgi) (built: Feb 10 2016 11:27:38)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies

Compile and Install PHP 5.5.32 on Ubuntu 16.04

$ cd /opt/source/php-5.5.32

$ sudo ./configure –enable-cli \
–prefix=/opt/source/php-5.5.32 \
–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/x86_64-linux-gnu \
–enable-ftp \
–with-gettext \
–with-gd \
–with-jpeg-dir=/usr/lib/ \
–enable-fpm

$ sudo make
$ sudo make install

Copy php.ini

$ sudo cp /opt/source/php-5.5.32/php.ini-production /opt/source/php-5.5.32/lib/php.ini

Check it

$ /opt/source/php-5.5.32/bin/php-cgi -v
PHP 5.5.32 (cgi-fcgi) (built: Feb 10 2016 11:41:55)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

Compile and Install PHP 5.6.18 on Ubuntu 16.04

$ cd /opt/source/php-5.6.18

$ sudo ./configure –enable-cli \
–prefix=/opt/source/php-5.6.18 \
–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/x86_64-linux-gnu \
–enable-ftp \
–with-gettext \
–with-gd \
–with-jpeg-dir=/usr/lib/ \
–enable-fpm

$ sudo make
$ sudo make install

Copy php.ini

$ sudo cp /opt/source/php-5.6.18/php.ini-production /opt/source/php-5.6.18/lib/php.ini

Check it

$ /opt/source/php-5.6.18/bin/php-cgi -v
PHP 5.6.18 (cgi-fcgi) (built: Feb 11 2016 04:50:26)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

Compile and Install PHP 7.0.3 on Ubuntu 16.04

No mysql support so I remove mysql from options above.

$ cd /opt/source/php-7.0.3

$ sudo ./configure –enable-cli \
–prefix=/opt/source/php-7.0.3 \
–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 \
–enable-mysqlnd \
–with-mysqli=/usr/bin/mysql_config \
–with-pdo-mysql=mysqlnd \
–with-png-dir=/usr \
–enable-gd-native-ttf \
–with-openssl \
–with-fpm-user=nginx \
–with-fpm-group=nginx \
–with-libdir=/lib/x86_64-linux-gnu \
–enable-ftp \
–with-gettext \
–with-gd \
–with-jpeg-dir=/usr/lib/ \
–enable-fpm

$ sudo make
$ sudo make install

Copy php.ini

$ sudo cp /opt/source/php-7.0.3/php.ini-production /opt/source/php-7.0.3/lib/php.ini

Check it

$ /opt/source/php-7.0.3/bin/php-cgi -v
PHP 7.0.3 (cgi-fcgi) (built: Feb 15 2016 15:40:51)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

Building Phalcon PHP Framework Extension On Ubuntu 16.04

Current version of Phalcon

At the moment Phalcon PHP support for PHP 5.5.x/5.6.x and available in 3 branch :
1. Legacy version : 1.3.xx
2. Stable version : 2.0.xx -> supporting PHP 5.3 (>= 5.3.21)
3. Stable version : 2.1.xx (still in beta) -> introduces new features some of them incompatible with previous released versions.

My plan

I’ll compile Phalcon on these version :

1. Phalcon 2.0.10 on PHP 5.5.32
2. Phalcon 2.1.0.RC1 on PHP 5.6.18

Install Zephir On Ubuntu 16.04

$ cd /opt
$ sudo git clone https://github.com/phalcon/zephir
$ cd zephir
$ sudo ./install -c

Test zephir version

$ zephir

_____ __ _
/__ / ___ ____ / /_ (_)____
/ / / _ \/ __ \/ __ \/ / ___/
/ /__/ __/ /_/ / / / / / /
/____/\___/ .___/_/ /_/_/_/
/_/

Zephir version 0.9.2a-dev

Lets compile Phalcon

Compile Phalcon 2.0.10 for PHP 5.5.32 on Ubuntu server 16.04

$ cd /opt
$ sudo git clone http://github.com/phalcon/cphalcon
$ cd cphalcon
$ sudo git checkout 2.0.x
$ cd ext
$ sudo /opt/source/php-5.5.32/bin/phpize
$ sudo ./configure –with-php-config=/opt/source/php-5.5.32/bin/php-config
$ sudo make
$ sudo make install
$ ls /opt/source/php-5.5.32/lib/php/extensions/no-debug-non-zts-20121212/
opcache.a opcache.so phalcon.so
$ sudo su
# echo “extension=phalcon.so” >> /opt/source/php-5.5.32/lib/php.ini
$ exit

Check if extension included :

$ /opt/source/php-5.5.32/bin/php -me | grep phalcon
phalcon

Compile Phalcon 2.1.RC1 for PHP 5.6.18 on Ubuntu server 16.04

$ cd /opt/cphalcon
$ sudo git checkout 2.1.x
$ cd ext
$ sudo make clean
$ sudo /opt/source/php-5.6.18/bin/phpize
$ sudo ./configure –with-php-config=/opt/source/php-5.6.18/bin/php-config
$ sudo make
$ sudo make install
$ ls /opt/source/php-5.6.18/lib/php/extensions/no-debug-non-zts-20131226/
opcache.a opcache.so phalcon.so
$ sudo su
# echo “extension=phalcon.so” >> /opt/source/php-5.6.18/lib/php.ini
$ exit

Check if extension included :

$ /opt/source/php-5.6.18/bin/php -me | grep phalcon
phalcon

Setting php-fpm for php 5.2.17

$ sudo cp /opt/source/php-5.2.17/sapi/cgi/fpm/init.d/php-fpm.in /etc/init.d/php-fpm5217

$ sudo chmod +x /etc/init.d/php-fpm5217

Change setting php-fpm5217

$ sudo nano /etc/init.d/php-fpm5217

….
php_fpm_BIN=/opt/source/php-5.2.17/bin/php-cgi
php_fpm_CONF=/opt/source/php-5.2.17/etc/php-fpm.conf
php_fpm_PID=/opt/source/php-5.2.17/php-fpm.pid

save.

for configuration of php-fpm.conf

$ sudo nano /opt/source/php-5.2.17/etc/php-fpm.conf

<value name=”listen_address”>127.0.0.1:9001</value>
<value name=”owner”>www-data</value>
<value name=”group”>www-data</value>
Unix user of processes
<value name=”user”>www-data</value>
Unix group of processes
<value name=”group”>www-data</value>

save.

Set it for boot ready.

$ sudo update-rc.d php-fpm5217 defaults

Run the process

$ sudo service php-fpm5217 start

Check it :

$ ps ax | grep php
1597 ? Ss 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
1599 ? S 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
1600 ? S 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
1601 ? S 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
1602 ? S 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
1603 ? S 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf

Setting php-fpm for php 5.5.32

$ sudo cp /opt/source/php-5.5.32/etc/php-fpm.conf.default /opt/source/php-5.5.32/etc/php-fpm.conf
$ sudo nano /opt/source/php-5.5.32/etc/php-fpm.conf

Change port from 9000 to 9002

listen = 127.0.0.1:9002

Change user and group to www-data

user = www-data
group = www-data

save.

$ sudo cp /opt/source/php-5.5.32/sapi/fpm/init.d.php-fpm.in /etc/init.d/php-fpm5532
$ sudo chmod +x /etc/init.d/php-fpm5532

$ sudo nano /etc/init.d/php-fpm5532

Change the content into ::

prefix=/usr/local
exec_prefix=/usr/local/bin

php_fpm_BIN=/opt/source/php-5.5.32/sbin/php-fpm
php_fpm_CONF=/opt/source/php-5.5.32/etc/php-fpm.conf
php_fpm_PID=/opt/source/php-5.5.32/var/run/php-fpm.pid

save.

$ sudo update-rc.d php-fpm5532 defaults

Setting php-fpm for php 5.6.18

$ sudo cp /opt/source/php-5.6.18/etc/php-fpm.conf.default /opt/source/php-5.6.18/etc/php-fpm.conf
$ sudo nano /opt/source/php-5.6.18/etc/php-fpm.conf

Change port from 9000 to 9003

listen = 127.0.0.1:9003

Change user and  group to www-data

user = www-data
group = www-data

save.

$ sudo cp /opt/source/php-5.6.18/sapi/fpm/init.d.php-fpm.in /etc/init.d/php-fpm5618
$ sudo chmod +x /etc/init.d/php-fpm5618

$ sudo nano /etc/init.d/php-fpm5618

Change the content ::

prefix=/usr/local
exec_prefix=/usr/local/bin

php_fpm_BIN=/opt/source/php-5.6.18/sbin/php-fpm
php_fpm_CONF=/opt/source/php-5.6.18/etc/php-fpm.conf
php_fpm_PID=/opt/source/php-5.6.18/var/run/php-fpm.pid

save.

$ sudo update-rc.d php-fpm5618 defaults

Setting php-fpm for php 7.0.3

$ sudo cp /opt/source/php-7.0.3/etc/php-fpm.conf.default /opt/source/php-7.0.3/etc/php-fpm.conf
$ sudo nano /opt/source/php-7.0.3/etc/php-fpm.conf

pid = run/php-fpm.pid

remove ; sign

save.

$ sudo cp /opt/source/php-7.0.3/etc/php-fpm.d/www.conf.default /opt/source/php-7.0.3/etc/php-fpm.d/www.conf
$ sudo nano /opt/source/php-7.0.3/etc/php-fpm.d/www.conf

Change port from 9000 to 9004

listen = 127.0.0.1:9004

change user and group to www-data

user = www-data
group = www-data

save.

$ sudo cp /opt/source/php-7.0.3/sapi/fpm/init.d.php-fpm.in /etc/init.d/php-fpm703
$ sudo chmod +x /etc/init.d/php-fpm703

$ sudo nano /etc/init.d/php-fpm703

change the content into ::

prefix=/usr/local
exec_prefix=/usr/local/bin

php_fpm_BIN=/opt/source/php-7.0.3/sbin/php-fpm
php_fpm_CONF=/opt/source/php-7.0.3/etc/php-fpm.conf
php_fpm_PID=/opt/source/php-7.0.3/var/run/php-fpm.pid

save.

$ sudo update-rc.d php-fpm703 defaults

Setting nginx configuration for virtualhost

$ sudo nano /etc/nginx/sites-enabled/default

Add these directives :

#php 5.2.17

server{
listen 80;
server_name goku.hostingformula.net;

root /home/alamsyah/public_html/php52;

location / {
index index.php;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9001; #(You can change it to your private IP in the future)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

# eof php 5.2.17
#php 5.5.32

server{
listen 80;
server_name goten.hostingformula.net;

root /home/alamsyah/public_html/php55;

location / {
index index.php;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9002; #(You can change it to your private IP in the future)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

# eof php 5.5.32
#php 5.6.18

server{
listen 80;
server_name gohan.hostingformula.net;

root /home/alamsyah/public_html/php56;

location / {
index index.php;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9003; #(You can change it to your private IP in the future)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

# eof php 5.6.18

#php 7.0.3

server{
listen 80;
server_name saitama.hostingformula.net;

root /home/alamsyah/public_html/php70;

location / {
index index.php;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9004; #(You can change it to your private IP in the future)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# eof php 7.0.3

save.

Create folder

$ cd
$ sudo mkdir public_html
$ sudo mkdir public_html/php52
$ sudo mkdir public_html/php55
$ sudo mkdir public_html/php56
$ sudo mkdir public_html/php70

$ sudo chown -R www-data:www-data public_html/

Inject phpinfo content

# sudo echo ‘<?php phpinfo();?>’ > public_html/php52/info.php
# sudo echo ‘<?php phpinfo();?>’ > public_html/php55/info.php
# sudo echo ‘<?php phpinfo();?>’ > public_html/php56/info.php
# sudo echo ‘<?php phpinfo();?>’ > public_html/php70/info.php

Restart nginx

$ sudo service nginx restart

Checking process :

$ ps ax | grep php
566 ? Ss 0:00 php-fpm: master process (/opt/source/php-5.5.32/etc/php-fpm.conf)
567 ? S 0:00 php-fpm: pool www
568 ? S 0:00 php-fpm: pool www
574 ? Ss 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
575 ? S 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
576 ? S 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
577 ? S 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
578 ? S 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
579 ? S 0:00 /opt/source/php-5.2.17/bin/php-cgi –fpm –fpm-config /opt/source/php-5.2.17/etc/php-fpm.conf
783 ? Ss 0:00 php-fpm: master process (/opt/source/php-5.6.18/etc/php-fpm.conf)
784 ? S 0:00 php-fpm: pool www
785 ? S 0:00 php-fpm: pool www
819 ? Ss 0:00 php-fpm: master process (/opt/source/php-7.0.3/etc/php-fpm.conf)
820 ? S 0:00 php-fpm: pool www
821 ? S 0:00 php-fpm: pool www

Testing from browser

PHP 5.2.17 on Ubuntu 16.04

ubuntu163

PHP 5.5.32 on Ubuntu 16.04

ubuntu164

PHP 5.6.18 on Ubuntu 16.04

ubuntu165

PHP 7.0.3 on Ubuntu 16.04

ubuntu166

Great. All those PHP version sit well in my vps.

Always watch for next issue at :

$ tail -f /var/log/nginx/error.log

$ free -m
total used free shared buff/cache available
Mem: 740 63 535 2 141 652
Swap: 764 0 764

Reference :


Limited Time Promo, Get Free $20 credit from Vultr, click the banner to get it.

One Comment

  1. Pingback: How to enable phalcon php in cpanel shared hosting

Comments are closed.