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

  • 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?

    in ubuntu there is apache.conf in /etc/apache2, i thought about this too, maybe i will in the future make some package it would be very useful for web developers

    You can do this with suPHP, not sure the details

    is there somthing like python virtualenv to have clean isolated deployments of PHP ?

    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.

    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.

    I would like to see a containerization-based solution.

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

    My favorite is phpbrew. Hope this helps.

    Thanks,Can you please include instructions? (onnu vishadheekarikkamo?)

    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.

    Just check the links they have really good documentation on their sites. Do you really want me to copy-paste things from their website?.

    Ironically, one of the reasons not to just post links is linkrot, from which this post now suffers.

    @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.

  • 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)

  • 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
    

    it's useful to remember use `phpinfo()` and `php -v` to crosscheck since both of them can have a different value.

  • 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.

    * 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.

  • 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
    
  • Problems:

    1. Single apache instance can not run two different PHP versions simultaneously (at least as far as I know it).

    2. Two different PHP versions can't be installed from packages as those will conflict (and try overwriting the same directories).

    Solution:

    • for b) Compile „the other” PHP version from source, manually (or if you prefer, grab source of appropriate package and modify it to avoid conflicts and to modify install paths)

    • for a) Create separate set of configs for apache with different module paths and separate startup script. And of course run it on separate port.

    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.

    A single apache instance _can_ run 2 php versions at once.

  • Instead of making multiple VirtualHost I just add the below code in my .htaccess file and surprisingly it worked,

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
    </FilesMatch>
    

    Node: I had already installed php7.2-fpm before using the code.

  • I wrote a bash script that allows me to easily switch between installed php versions on my box. I run several virtual hosts because I have several web projects with customers that have old and new programs. Some of these projects require older versions of php and other projects require php7.0.

    Read through my comments and make sure you understand what is going on before using this.

    #!/bin/bash
    # This file is for switching php versions.  
    # To run this file you must use bash, not sh
    # 
    # OS: Ubuntu 14.04 but should work on any linux
    # Example: bash phpswitch.sh 7.0
    # Written by Daniel Pflieger
    # growlingflea at g mail dot com
    
    NEWVERSION=$1  #this is the git directory target
    
    #get the active php enabled mod by getting the array of files and store
    #it to a variable
    VAR=$(ls /etc/apache2/mods-enabled/php*)
    
    #parse the returned variables and get the version of php that is active.
    IFS=' ' read -r -a array <<< "$VAR"
    array[0]=${array[0]#*php}
    array[0]=${array[0]%.conf}
    
    
    #confirm that the newversion veriable isn't empty.. if it is tell user 
    #current version and exit
    if [ "$NEWVERSION" = "" ]; then
    echo current version is ${array[0]}.  To change version please use argument
    exit 1
    fi 
    
    OLDVERSION=${array[0]}
    #confirm to the user this is what they want to do
    echo "Update php"  ${OLDVERSION} to ${NEWVERSION}
    
    
    #give the user the opportunity to use CTRL-C to exit ot just hit return
    read x
    
    #call a2dismod function: this deactivate the current php version
    sudo a2dismod php${OLDVERSION}
    
    #call the a2enmod version.  This enables the new mode
    sudo a2enmod php${NEWVERSION} 
    
    echo "Restart service??"
    read x
    
    #restart apache
    sudo service apache2 restart
    
  • I had the same problem when I was handling multiple projects. I referred this article But it was like every time I need to switch the versions if need but this gives the flexibility for cli and web separately.

    Moving From php5.6 to php7.0

    For Apache, we can use the command as:

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

    For CLI, we can use the command as:

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

    Moving From php7.0 to php5.6

    For Apache, we can use the command as:

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

    For CLI, we can use the command as:

    sudo update-alternatives --set php /usr/bin/php5.6
    

    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

    @waltinator Thanks for the review, sure will take care of this

  • It can be done by disabling and enable the new version of PHP-

    • switch to older version -
      a2dismod php7.0
      service apache2 restart
    • switch to newer version
      a2enmod php7.0
      service apache2 restart

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM