Open Laboratory for Technocrats

Grab the developer role, learn concepts & prepare with senior software engineers to get solutions for problems in a software job. Coding and Programming Solutions crafted for you.

Use Multiple Version of PHP on Ubuntu


USE MULTIPLE PHP VERSIONS ON LINUX
USE MULTIPLE PHP VERSIONS ON LINUX
While working on different projects on the same machine and not using any package manager in PHP can put you on hold for a while.

The same thing happened to us while we need to upgrade one of our projects from PHP version 5.6 to version 7.0

Challenge begins that we need to keep up both the application while migrating the project.

We figured out the solution with the alternative keyword which we have used a while back in Java for development purpose.

Now lets jump and see how we achieved the switch over to other versions while keeping both the version of PHP on our development machine.

We will start with installing PHP first and then we will go through the steps as well.

Open Terminal on the development machine and followed the commands.

  • sudo add-apt-repository ppa:ondrej/php
  • sudo apt-get update
Now after adding the apt-repo we will install first the PHP version 5.6


  • sudo apt-get install php5.6 php5.6-mysql php5.6-mbstring libapache2-mod-php5.6
This command will install some of the common libraries as well for our use. After that we will put this version on hold for now with the following command:
  • sudo a2dismod php5.6
Now we will start installing the PHP version 7.0
  • sudo apt-get install php7.0 php7.0-mysql php7.0-mbstring libapache2-mod-php7.0 php7.0-fpm
This command will install some of the common libraries as well for our use. 
Now we will enable the version 7.0 for our use with the command:
  • sudo a2enmod php7.0
After that, we need to install some dependency library to work within our case of switching so that things should work.
  • sudo apt-get install php-gettext php-xdebug
Once its done do restart server:

  • sudo /etc/init.d/apache2 restart
Now comes the main part of switching PHP versions:

From php5.6 to php7.0


USE MULTIPLE PHP VERSIONS ON LINUX
USE MULTIPLE PHP VERSIONS ON LINUX

For Apache:


We need to run following commands:

  • sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart

For CLI:

We need to run following commands:

  • sudo update-alternatives --set php /usr/bin/php7.0

From php7.0 to php5.6

USE MULTIPLE PHP VERSIONS ON LINUX
USE MULTIPLE PHP VERSIONS ON LINUX

For Apache:

We need to run following commands:
  • sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart

For CLI:

We need to run following commands:

  • sudo update-alternatives --set php /usr/bin/php5.6
For results, you can check the version of the PHP is in the use.
These steps simplified our life for version management without using any other tools or package/ version management library.

Keep Learning and Keep Coding :)

Top #3 Articles