How to have two versions of PHP installed and switch easily between them?

180,613

Solution 1

You can use a php version manager to achieve this. Different version managers are available like:

My favorite is phpbrew. Hope this helps.

Solution 2

You can run 2 different PHP versions at once, but it's not as easy as just apt-getting them. You need to run one seperately installed version and serve it up according to the settings in your apache config.

You can do this using fastcgi for example: basically what you are looking for is the config you see on this page. You add a different handler in your config based on the situation/port/domain you need. The trick, after installing both versions, is this step:

===from that page==

  1. The last step was to create virtual hosts. In the end I have three files in /etc/apache2/sites-enabled: 000-default, php5.3.17 and php5.4.7 With the following contents

default:

    <VirtualHost *:80>
      ServerName localhost
      DocumentRoot /var/www
      <Directory "/var/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler php-cgi .php
        Action php-cgi /php-fcgi/php5317.fcgi
      </Directory>
    </VirtualHost>

php5.3.17:

    <VirtualHost *:80>
      ServerName 5317.localhost
      DocumentRoot /var/www
      <Directory "/var/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler php-cgi .php
        Action php-cgi /php-fcgi/php5317.fcgi
      </Directory>
    </VirtualHost>

php5.4.7:

    <VirtualHost *:80>
      ServerName 547.localhost
      DocumentRoot /var/www
      <Directory "/var/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler php-cgi .php
        Action php-cgi /php-fcgi/php547.fcgi
      </Directory>
    </VirtualHost>

See for the complete installation the linked question. Don't forget to look at the two links in the header as well, they look like nice tutorials (but less compressed). This and That

(don't be put of by the fact that the linked question is a big one with no accepted answer. The method should (and does) work fine, but the user forgot to use <?php and used <? with shorttags off, see the comments)

Solution 3

I installed php 5.6 and 7.0 but besides all tips it kept running 5.6, so this command saves the day (in my case i chosse option 1 and restart apache):

sudo update-alternatives --config php

Solution 4

This one works for me: https://lornajane.net/posts/2016/php-7-0-and-5-6-on-ubuntu

Same as the script from "Growling Flea", but using new versions.

Add the PPA

The PHP 5.6 and PHP 7.0 packages are from a third party PPA, not provided by the official Ubuntu repositories from Canonical. The PPAs I'm recommending here are from Ondřej Surý who packages PHP for Debian (which is then used by Ubuntu) so while it's not an official repository, he's not exactly random! The PPA itself is here

To add the PPA to your setup:

sudo add-apt-repository ppa:ondrej/php  

Then we'll also want to grab information about what is on offer from this new PPA, so then run:

sudo apt-get update   

Install new PHP versions

I already had some of the php5 packages installed, but I didn't uninstall anything, I just let apt work out what it wanted to do when I asked it to install the new versions:

sudo apt-get install php5.6 php7.0

This resulted in a lot of complaining from apt and lots of conflicts. The first suggested resolution was to remove all the stock php5 packages so that PHP 5.6 could be installed - so I just accepted the first suggestion.

I use apache so this setup gave me apache with both php5.6 and php7.0 modules available, and the php5.6 module actually loaded.

As well as just the PHP itself, all the extensions and other tools you'd expect with PHP are there for both versions of PHP so it's very easy to add in the modules that you need. I was very, very impressed with how nicely this is done.

Configuring and switching versions

Now you have two completely separate versions of PHP installed on your system, so let's have a look at where all the pieces went!

The config files are all in /etc/php/5.6 and /etc/php/7.0 respectively - inside here is where you can configure which extensions are loaded, set the ini settings, and everything else for each version in isolation.

I'm an apache user, and as I mentioned both modules are available. So to switch from one to the other I need to do:

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

For nginx users, the changes are almost as easy, Digital Ocean have good documentation on this (they do have great docs!) so check out their guide: https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-ubuntu-14-04 as it includes a section on reconfiguring nginx to use another version of PHP.

From the command-line, I have both php5.6 and php7.0 available as commands. I also still have a php command - look in /etc/alternatives to see that it symlinks to a particular version of PHP cli*. You can also quickly check which yours is using by running php -v.

&ast; more specifically, run which php to see which version of PHP is being used - but this will probably point to /usr/bin/php, which for me is itself a symlink to the /etc/alternatives/php command.

Working with extensions

This PPA comes with the usual php-pear package which offers the pecl command for both versions of PHP, so any extensions that are available via PECL can be installed in the usual way. You will also need the relevant headers so either php5.6-dev or php7.0-dev should be installed.

When the pecl installation completes, you'll get a note to add the *.so file to your php.ini; in fact the best thing to do here is to look at what's in /etc/php/mods-available. There will be some modules already here, each in its own file named after the extension and ending in .ini. You can copy one to use as a template or create your own and put all the relevant configuration for the extension in it (as a minimum, you need extension=[extensionName].so).

Once the new extension is in mods available, enable and then check it by doing:

sudo phpenmod extension  
php -m  

This will create the symlinks in the right places for your current version of PHP to load this module, and you should see it in the list of modules output by the php -m. Pro tip: if you don't see it in the output, scroll all the way to the top of the output and see if there are any useful error messages there.

Solution 5

From this post, I just put in order the commands and explain in order as I was served (Ubuntu 12.04)

This method give you:

  1. script with menu, that permit switch between php versions installed (not both at same time)

  2. various PHP versions installed on your server

  3. separated conf files

Install all versions that you need (i have two)

Install dependencies:

sudo apt-get install flex apache2-threaded-dev libxml2-dev apache2 apache2-mpm-prefork apache2-threaded-dev apache2-utils apache2.2-bin apache2.2-common

Install PHP 5.3 first time. Download php sources

md5sum Downloads/php-5.3.10.tar.bz2
mkdir ~/Sources
cd ~Sources/
cp -Rf ../Downloads/php-5.3.10.tar.bz2 .
tar xjf php-5.3.10.tar.bz2
cd php-5.3.10/
sudo mkdir /usr/local/php/php_5.3.10

Install PHP5.6 first time. Download php sources

md5sum Downloads/php-5.6.11.tar.bz2
mkdir ~/Sources
cd ~Sources/
cp -Rf ../Downloads/php-5.6.11.tar.bz2 .
tar xjf php-5.6.11.tar.bz2
cd php-5.6.11/
sudo mkdir /usr/local/php/php-5.6.11

Script manager versions PHP: Create a file called php.sh and put it in /bin/:

#!/bin/bash
opcion=0
cat << CABECERAMENU
Opciones del menu
1 => PHP 5.3.10
2 => PHP 5.6.11
CABECERAMENU
echo -n "Ingrese su eleccion: "
read opcion
echo
case $opcion in
    "1")
        rm /etc/apache2/php.conf
        ln -s /usr/local/php/php_5.3.10.conf /etc/apache2/php.conf
        /etc/init.d/apache2 restart
    ;;
    "2")
        rm /etc/apache2/php.conf
        ln -s /usr/local/php/php_5.6.11.conf /etc/apache2/php.conf
        /etc/init.d/apache2 restart
    ;;
    *)
        echo "Opcion no valida"
    ;;
esac

Compile and install php 5.3:

cd ~/Sources/php-5.3.10/
sudo ./configure --prefix=/usr/local/php/php_5.3.10 --with-config-file-path=/usr/local/php/php_5.3.10/lib --with-mysql --with-libdir=/lib/x86_64-linux-gnu --with-apxs2=/usr/bin/apxs2 --enable-zip --with-gd --with-curl --with-xmlrpc --with-freetype-dir=/usr/lib/x86_64-linux-gnu  --with-jpeg-dir=/usr/lib/x86_64-linux-gnu --with-pdo-mysql --with-pdo-pgsql --enable-soap
sudo make clean
sudo make
sudo ls -lhart /usr/lib/apache2/modules/libphp5.*
sudo rm -rf /usr/lib/apache2/modules/libphp5.*
sudo make install
sudo ls -lhart /usr/lib/apache2/modules/libphp5.*
sudo rm -rf /usr/local/php/php-5.3.10/modules/libphp5.so
sudo mv /usr/lib/apache2/modules/libphp5.so /usr/local/php/php_5.3.10/modules/
ls -lhart /usr/local/php/php_5.3.10/modules/
sudo a2dismod php5
sudo service apache2 restart

Compile and install php 5.6

cd ~/Sources/php-5.6.11
sudo ./configure --prefix=/usr/local/php/php_5.6.11 --with-config-file-path=/usr/local/php/php_5.6.11/lib --with-mysql --with-libdir=/lib/x86_64-linux-gnu --with-apxs2=/usr/bin/apxs2 --enable-zip --with-gd --with-curl --with-xmlrpc --enable-calendar --enable-sockets --with-freetype-dir=/usr/lib/x86_64-linux-gnu  --with-jpeg-dir=/usr/lib/x86_64-linux-gnu --with-pdo-mysql  --with-pdo-pgsql --enable-soap
sudo make clean
sudo make
sudo ls -lhart /usr/lib/apache2/modules/libphp5.*
sudo rm -rf /usr/lib/apache2/modules/libphp5.*
sudo make install
sudo ls -lhart /usr/lib/apache2/modules/libphp5.*
sudo rm -rf /usr/local/php/php_5.6.11/modules/libphp5.so
sudo mv /usr/lib/apache2/modules/libphp5.so /usr/local/php/php_5.6.11/modules
ls -lhart /usr/local/php/php_5.6.11/modules
sudo a2dismod php5
sudo service apache2 restart

Let's to use them:

$ sudo php.sh
Opciones del menu
1 => PHP 5.3.10
2 => PHP 5.6.11
Ingrese su eleccion: 1

 * Restarting web server apache2                                                                                                                                                                                                                                        apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
 ... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
Share:
180,613

Related videos on Youtube

Debiprasad
Author by

Debiprasad

Updated on September 18, 2022

Comments

  • Debiprasad
    Debiprasad over 1 year

    I want to have both PHP 5.2.17 and PHP 5.3.5 installed on my Ubuntu machine and switch as per my necessity. How can I achieve this?

    • Michael Gundlach
      Michael Gundlach almost 13 years
      You can do this with suPHP, not sure the details
    • Lazzaro
      Lazzaro about 12 years
      is there somthing like python virtualenv to have clean isolated deployments of PHP ?
    • Simon Hoare
      Simon Hoare over 11 years
      Have you considered setting up virtual Ubuntu servers using virtualisation software that you use in a headless fashion? I'd be surprised if what you are wanting to do is not well served (no pun intended) by such a set-up.
    • cprn
      cprn over 11 years
      Define what it means to "switch easily". I used this for work: my webserver was using different PHP for .php and .php5 files. You can also configure your WWW server to use different PHP versions for different ports (like http://localhost:80 and http://localhost:8080) or different domains (like http://localphp52 and http://localphp53) leaving all the remaining structure of folders intact so you can easily compare running one web application in two environments by changing the current URL.
    • ThorSummoner
      ThorSummoner over 9 years
      I would like to see a containerization-based solution.
  • Debiprasad
    Debiprasad almost 13 years
    I already have PHP 5.3.5 installed. How can I install PHP 5.2.17? Could you please tell me where and what I have to change to switch between in these two versions? The httpd.conf file is empty in my installation.
  • Lazzaro
    Lazzaro almost 12 years
    there is no prepackaged script to compile these multiple parallele php instance ? Just to do it manually and patch paths ? SO this means that all hosting company develop there custom scripts to run these parallel version and never share their effort.
  • Nanne
    Nanne over 11 years
    A single apache instance can run 2 php versions at once.
  • user98085
    user98085 over 11 years
    Welcome to Ask Ubuntu! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
  • aneeshep
    aneeshep over 11 years
    Just check the links they have really good documentation on their sites. Do you really want me to copy-paste things from their website?.
  • Henrique Fernandes
    Henrique Fernandes over 11 years
    I did not have time enough to do as you said. I guess, i only wanted to show a way. Stil need to learn how to use the formating stuff. Thanks anyway!
  • MirroredFate
    MirroredFate almost 11 years
    Ironically, one of the reasons not to just post links is linkrot, from which this post now suffers.
  • Matthieu
    Matthieu about 6 years
    @aneeshep, not copy-paste their documentation, but relevant excerpts and a few lines about what is a "php version manager" and how to use/configure it for that specific problem would really be welcome.
  • goo
    goo almost 6 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
  • Shivdhwaj Pandey
    Shivdhwaj Pandey almost 6 years
    @waltinator Thanks for the review, sure will take care of this
  • Adi Prasetyo
    Adi Prasetyo over 4 years
    it's useful to remember use phpinfo() and php -v to crosscheck since both of them can have a different value.