[Ubuntu] Switch PHP version from PHP 7 to PHP 5.6

If you have an old version of PHP and you need to switch it in your apache2 server, or even if you are using PHP 7 and you need to switch to an older one, you don’t need to uninstall the older version.

There are a few steps to switch you php version.

First of all let’s update the php repository.

Once you get everything properly installed and updated, let’s switch the version from PHP 7 to 5.6

  • From PHP 5.6 to php7.0 :
    • Apache:
      sudo a2dismod php5.6 && sudo a2enmod php7.0 && sudo service apache2 restart
    • CLI:
      sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php
  • from php7.0 to php5.6:
    • Apache:
      sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart
    • CLI:
      sudo ln -sfn /usr/bin/php5.6 /etc/alternatives/php

 

2 Comments

  • ihakoz
    Posted July 22, 2017 6:32 pm 7Likes

    You can create a script to switch from versions: sudo nano switch_php
    then type this:

    #!/bin/sh
    #!/bin/bash
    echo “Bascule vers PHP$1 en cours…”
    case $1 in
    “7”)
    sudo a2dismod php5.6
    sudo a2enmod php7.0
    sudo service apache2 restart
    sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php;;
    “5.6”)
    sudo a2dismod php7.0
    sudo a2enmod php5.6
    sudo service apache2 restart
    sudo ln -sfn /usr/bin/php5.6 /etc/alternatives/php;;
    esac
    echo “Version actuelle: $( php -v | head -n 1 | cut -c-7 )”

    exit and save
    make t executable: sudo chmod +x switch_php

    To execute the script just type ./switch_php [VERSION_NUMBER] where the parameter is 7 or 5.6
    That’s it you can now easily switch form PHP7 to PHP 5.6!

    • fspencer
      Posted July 26, 2017 12:54 pm 0Likes

      That is a great alternative! Thank you for sharing!

Leave a comment