Edit March 2017: I now added the downgrading variant, i.e. Ubuntu 16.04 with PHP.71 and 5.6

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.

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

UP - Ubuntu 14.04: PHP 5.6 and PHP 7.1

  • 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

# stop apache service
sudo service apache2 stop

# 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
extension=mongodb.so
  1. Start Apache
# cross fingers
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
  1. Using the PHP cli independently for different versions.
# my current cli version
php -v

# run command under PHP 7.1
/usr/bin/php7.1 <command>

# run command under PHP 5
/usr/bin/php5 <command>

DOWN Ubuntu 16.04: PHP 7.1 and PHP 5.6

  • 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

Using the PHP cli independently for different versions.

# my current cli version
php -v

# run command under PHP 7.1
/usr/bin/php7.1 <command>

# run command under PHP 5.6
/usr/bin/php5.6 <command>

Using composer

composer <task> --ignore-platform-reqs

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