• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SMASHING LAB

GUIDES | LINUX | GEEKY STUFF

  • Nginx
  • Linux
  • Guide
  • Ubuntu
  • WordPress
  • Drones, My Hobby

How to Install and Use Multiple Versions of PHP on Debian/Ubuntu

By Imran Yousaf

Performance of PHP is greatly increased in the version 7.0 and support for the older version will be ended in newer future. So it is now critical to update your PHP version. But at the same point, there are applications of environment that demand older version of PHP. You may need to run several versions of PHP under one apache server.

I looked at the various options and I was given the launch of the old version of PHP 5.6 via FastCGI. So, I already have the Apache server installed and PHP 7.0 installed. Let’s start with downloading and building PHP 5.6.

We need to execute the folowing necessary commands to prepare your machine to build and execute the application from source code.

apt-get -y install build-essential libfcgi-dev libfcgi0ldbl libjpeg62-dbg libmcrypt-dev libssl-dev libc-client2007e libc-client2007e-lib libxml2-dev libbz2-dev libcurl4-openssl-dev libjpeg-dev libpng12-dev libfreetype6-dev libkrb5-dev libpq-dev libxml2-dev libxslt1-dev pkg-config

Now you need to prepare directory to compile and run the project.

1
2
3
4
5
6
mkdir /opt/php-5.6.34 # create a folder in which the compiled php will be compiled
mkdir /usr/local/src/php5-build #create the source folder
cd /usr/local/src/php5-build #move to this folder
wget http://php.net/get/php-5.6.34.tar.bz2/from/a/mirror
tar jxf php-5.6.34.tar.bz2 # unpack the archive
cd php-5.6.34 / #go to the source folder

Next, configure and compile php-5.6.34.

./configure –prefix=/opt/php-5.6.30 –with-config-file-path=/opt/php-5.6.340 –with-pdo-pgsql –with-zlib-dir –with-freetype-dir –enable-mbstring –with-libxml-dir=/usr –enable-soap –enable-calendar –with-curl=/usr/bin –with-mcrypt –with-zlib –with-gd –with-pgsql –disable-rpath –enable-inline-optimization –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-jpeg-dir=/usr –with-png-dir=/usr –enable-gd-native-ttf –with-openssl –enable-ftp –with-kerberos –with-gettext –enable-cli –enable-fastcgi –enable-discard-path –enable-force-cgi-redirect –enable-cgi

compile and install

1
2
make
make install

At the end of the run, you will have the compiled version of php-5.6.34 in the /opt/php-5.6.34/ directory.

Next, you need to install the module for apache.

1
apt-get install libapache2-mod-fastcgi

and run it and another module.

1
2
a2enmod fastcgi
a2enmod actions

Next, edit the configuration file /etc/apache2/mods-enabled/fastcgi.conf (cite the contents).

1
2
3
4
  AddHandler fastcgi-script .fcgi
  #FastCgiWrapper /usr/lib/apache2/suexec
  FastCgiIpcDir /var/lib/apache2/fastcgi
  FastCgiServer /usr/lib/cgi-bin/php56-cgi -idle-timeout 240

Then we create a wrapper through which the php scripts will be executed.

1
vi /usr/lib/cgi-bin/php56-cgi

1
2
3
4
5
6
7
8
#!/bin/sh
PHPRC="/opt/php-5.6.34/"
export PHPRC
PHP_FCGI_CHILDREN=4
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /opt/php-5.6.34/bin/php-cgi

Let’s add the execution rights.

1
sudo chmod + x /usr/lib/cgi-bin/php56-cgi

Create an apache configuration file with the settings of the php script handler.

1
vi /etc/apache2/php56.conf

1
2
3
4
5
6
7
8
9
10
LogLevel warn
Options -Indexes +FollowSymLinks +ExecCGI
Require all granted
 
<FilesMatch ".php">
SetHandler application/x-httpd-php5
 
ScriptAlias /php56-cgi /usr/lib/cgi-bin/php56-cgi
Action application/x-httpd-php5 /php56-cgi
AddHandler application/x-httpd-php5 .php

It remains to create a configuration file for the virtual host and add the import php56.conf.

1
vi /etc/apache2/sites-available/test.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
        Include php56.conf
        ServerName test.local
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/test
        
        
        Options  Indexes FollowSymLinks
        AllowOverride all
        Require all granted
                      
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined        

Add this virtual host to the Apache configuration

1
a2ensite test.conf

and restart the service

1
service apache2 restart

To verify, create a file

1
vi /var/www/test/phpinfo.php

1
<!--?php phpinfo (); ?-->

Another site with the higher version of php,

Voila. You have now multiple versions of PHP on the same server.

Some more articles you might also be interested in …

  • How to Install Brackets on Debian / Ubuntu
  • LMDE 4 Debbie Released, Debian 10 Buster Based
  • Guide How to Install Apache in Ubuntu 18.04
  • Install and Configure Apache on Ubuntu Server
  • Complete Guide: How To Install Ubuntu Server 18.04
  • How to Install LAMP Stack (Linux, Apache, MySQL,…
  • How to install LAMP on CentOS 8
  • Install Docker in CentOS 7
  • How to install Nextcloud on CentOS 8
  • How to install Zabbix on openSUSE Leap 15.1
Tweet
Pin
Share
0 Shares

Filed Under: Guide

Primary Sidebar

  • Twitter
  • Facebook
  • Google+

Recent Posts

  • How to install Nextcloud on CentOS 8
  • How to install Zabbix on openSUSE Leap 15.1
  • How to Easily Configure Your Domain’s Email Account with Gmail?
  • Tails 4.4 OS Released with Tor Browser 9.0.6
  • How to Convert JPG to PDF in Linux

Subscribe to our Newsletter

Useful articles, tips and videos about creating and promoting websites to your mail



* required field


  • Contact Us
  • Privacy Policy
  • About Us

Copyright © 2021. Smashing Lab