You need to switch between PHP 5.6 and 7.1 on your Ubuntu 16.x, without any docker/vm bs? Here is one of several ways tpo do it.

  • Edit July 2017: Here’s how to do this with Ubuntu 14.x

Use case: an existing development server for lxHive Note: This is not recommended for production environments.

  • Ubuntu 16.04
  • run PHP 5.6 along with default PHP 7.1
  • being able to switch back to PHP 7.1
  • run legacy Mongo driver on PHP 5.6 (ext-mongo)
  • enable current Mongo support for PHP 5.6 (ext-mongodb)
# stop apache service
sudo service apache2 stop

# Add the main PPA for PHP (5.6, 7.0, 7.1) with many PECL extensions
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

sudo apt-get install php5.6 php5.6-dev php5.6-mbstring php5.6-dom php5.6-cli php5.6-json php5.6-curl php5.6-gd php5.6-mcrypt
sudo apt-get install php5.6-mysql
sudo apt-get install php5.6-mongodb
sudo apt-get install php5.6-mongo
sudo nano /etc/php/5.6/apache2/php.ini

## add to file:
# extension=mongo.so
# extension=mongodb.so

# finally
sudo service apache2 start

Now you can switch PHP versions (apache and cli)

# check version

#switch to php 7.1
sudo a2dismod php5.6 && sudo a2enmod php7.1 && sudo update-alternatives --set php /usr/bin/php7.1 && sudo service apache2 restart

#switch to php 5.x
sudo a2dismod php7.1 && sudo a2enmod php5.6 && sudo update-alternatives --set php /usr/bin/php5.6 && sudo service apache2 restart
  • Edit july 2017: I added a little helper script for above switch commands:

Links

  • https://launchpad.net/~ondrej/+archive/ubuntu/php
  • MongoDB driver
  • Legacy Mongo driver
  • http://tecadmin.net/install-php5-on-ubuntu/
  • https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-ubuntu-14-04
  • https://askubuntu.com/a/762161