Sometimes you can’t just upgrade Ubuntu to >=16 which ships a LAMP with PHP 7 by default . As a temporary solution you can upgrade your old LAMP to PHP 7 whilst keeping PHP 5.x available.

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

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

  • Ubuntu 14.04
  • upgrade LAMP on PHP 5.6 to PHP 7.1
  • being able to switch back to PHP 5.6
  • enable Mongo support for PHP 7 (ext-mongodb)
  • keep legacy ext-mongo for PHP 5.6 (ext-mongo)
  1. Stop Apache
# stop apache service
sudo service apache2 stop
  1. Upgrade PHP

# 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

# Install base php and extensions
sudo apt-get install php7.1 php7.1-dev php7.1-mbstring php7.1-dom php7.1-cli php7.1-json php7.1-curl php7.1-gd php7.1-mcrypt
  1. Upgrade database drivers

# mysql extension
sudo apt-get install php7.1-mysql

# monodb extension (php7 only)
sudo apt-get install php7.1-mongodb
  1. register Monodb extension in php.ini
sudo nano /etc/php/7.1/apache2/php.ini #grab loaded ini from phpinfo()
# add
  1. Start Apache
sudo service apache2 start

Now you can switch PHP versions (apache and cli)

# check version
php -v

#switch to php 7.1
sudo a2dismod php5 && 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 && sudo update-alternatives --set php /usr/bin/php5.6 && sudo service apache2 restart


  • MongoDB driver
  • Legacy Mongo driver