How to Downgrade PHP from 7.2 to 7.1 on Ubuntu 16.04 with Apache Without Breaking Your Server Configuration


1 views

Recently I found myself in a situation where a vendor misinformed me about PHP version compatibility. Their application actually required PHP 7.1, despite claiming 7.2 support. Here's how I safely downgraded without wrecking my Apache configuration.

Simply removing PHP 7.2 packages often creates dependency hell with Apache and MySQL. The key is maintaining web server functionality while switching PHP versions.

First, add the older PHP repository:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

Install PHP 7.1 packages alongside existing 7.2:

sudo apt-get install php7.1 php7.1-cli php7.1-common php7.1-mbstring php7.1-gd php7.1-intl php7.1-xml php7.1-mysql php7.1-zip

Disable PHP 7.2 module and enable 7.1:

sudo a2dismod php7.2
sudo a2enmod php7.1
sudo systemctl restart apache2

Create a test file:

echo "" > /var/www/html/info.php

Check the output confirms PHP 7.1 is active.

If you encounter missing extensions:

sudo apt-get install php7.1-[extension-name]
sudo systemctl restart apache2

For package conflicts, use:

sudo apt-get -f install

After confirming everything works, remove PHP 7.2 packages:

sudo apt-get purge php7.2*

Many developers face similar situations when vendor requirements force PHP version changes. The key challenge here is maintaining Apache and MySQL functionality while switching PHP versions. Here's a tested method I've used in production environments.

First, check your current PHP version and installed modules:

php -v
dpkg --list | grep php7.2

Create a backup of your current configuration:

sudo cp -r /etc/php /etc/php_backup
sudo cp /etc/apache2/mods-enabled/php*.conf /etc/apache2/

We'll use Ondřej Surý's PPA which maintains older PHP versions:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

First, remove PHP 7.2 packages while keeping config files:

sudo apt-get remove php7.2* --purge
sudo apt-get autoremove

Install PHP 7.1 and common extensions:

sudo apt-get install php7.1 php7.1-common php7.1-cli php7.1-fpm php7.1-json php7.1-mysql php7.1-zip php7.1-gd php7.1-mbstring php7.1-curl php7.1-xml php7.1-bcmath

For Apache integration:

sudo apt-get install libapache2-mod-php7.1
sudo a2dismod php7.2
sudo a2enmod php7.1
sudo systemctl restart apache2

Create a test file at /var/www/html/info.php:

<?php
phpinfo();
?>

Access it via browser and verify:

http://your_server_ip/info.php

If you encounter missing extensions:

sudo apt-get install php7.1-[extension-name]

For configuration conflicts:

sudo cp /etc/php/7.1/apache2/php.ini /etc/php/7.1/apache2/php.ini.backup
sudo nano /etc/php/7.1/apache2/php.ini

Remember to update your composer packages if you're using them:

composer update

This method preserves your existing Apache configuration while cleanly switching PHP versions. The key is systematically removing the old version before installing the new one, rather than trying to install both versions simultaneously.