How can I get Apache to use PHP 7.0 (not 7.1)?

When setting up a Ubuntu 16.04 vhost, Apache will use PHP 7.0.22-2+ubuntu16.04.1+deb.sury.org+4.

However, after trying to resolve some conflicts by doing apt-get dist-upgrade (and failing), I ended up with a badly broken system. I've since managed to roll back most package versions to those that are default, but Apache uses PHP 7.1.8-2+ubuntu16.04.1+deb.sury.org+4, which is incompatible with some PHP-libraries I need.

So far, I've tried:

sudo a2dismod php7.1
sudo a2enmod php7.0
sudo service apache2 restart

However, this breaks Apache and it refuses to restart:

Syntax error on line 140 of /etc/apache2/apache2.conf: Syntax error on line 3 of /etc/apache2/mods-enabled/php7.0.load: Cannot load /usr/lib/apache2/modules/libphp7.0.so

How can I get Apache to run the default PHP version (PHP 7.0) again?

Edit: A comment to my [now redacted] self-answer below by Dan made me realize that having added the following third party PPA ppa:ondrej/php may have something do do with the problem.

So:
I have added the PPA ppa:ondrej/php to my site.


Solution 1:

I'll split my answer into two parts. The first part describes how your problem occurred, the second part is the actual answer to your issue.

Description

Disclaimer: Most of my description is basically speculation, as I cannot really know what you did. But it's the most likely scenario, as I cannot think of another way that would end up giving you the issue you described.

From the problem you describe, it seems you have installed a third party PPA which installed PHP 7.1 on your system. The most likely PPA is Ondrej's PPA.

When you first installed PHP, you installed in the following method:

sudo apt install php

The php package is only a meta package and does not contain the binaries needed. It depends on the latest version of the available PHP package (by default 7.0). So when you install it, the php7.0 package is installed along with php7.0's dependencies (and libapache2-mod-php7.0 if you have apache2) and all those dependencies are marked in the package manager as "automatically installed".

$ apt show php
Package: php
Version: 1:7.0+35ubuntu6
Priority: optional
Section: php
Source: php-defaults (35ubuntu6)
Origin: Ubuntu
Maintainer: Ubuntu Developers 
Original-Maintainer: Debian PHP Maintainers 
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 11.3 kB
Depends: php7.0
Supported: 5y
Download-Size: 2,832 B
APT-Manual-Installed: yes
APT-Sources: http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages
Description: server-side, HTML-embedded scripting language (default)
 PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
 open source general-purpose scripting language that is especially suited
 for web development and can be embedded into HTML.
 .
 This package is a dependency package, which depends on Debian's default
 PHP version (currently 7.0).

I bolded the parts of the command's result, which explains what I just mentioned.

Ondrej's PHP PPA offers multiple PHP versions that can be installed alongside each other. So when you added it your system, apt found a newer version of PHP so it replaced php7.0* with php7.1 along with any related packages that were automatically installed.

Answer

There are two methods to fix your issues.

  1. Completely remove the 3rd party PPA, and revert back to the default PHP packages

    sudo apt install ppa-purge
    sudo ppa-purge ppa:ondrej/php
    
  2. Ondrej's PPA offers multiple versions of PHP, so you can install more than one version along side each other. But you would enable only the php7.0 Apache module.

    sudo apt install php7.0 libapache2-mod-php7.0
    sudo a2dismod php7.1
    sudo a2enmod php7.0
    sudo apache2ctl restart
    

    Installing them in the previous manner would set the php7.0 package as "manually installed" in apt, so they won't get removed automatically without you removing them yourself. Don't forget to make sure to install any PHP modules you need for php7.0 as well (Like sudo apt install php7.0-mysql)

* php7.0 may have been kept in your system installed if you had manually installed a module specifically for php7.0 (for example php7.0-mysql).

Solution 2:

By searching packages.ubuntu.com for the file libphp7.0, I found two packages containing that file. Is either or both installed? libapache2-mod-php7.0 and/or libphp7.0-embed?

Reinstalling libapache2-mod-php7.0 with

sudo apt install --reinstall libapache2-mod-php7.0

then see what there is to read in the package. List the contents with

dpkg -L libapache2-mod-php7.0

The search results:

 » Ubuntu » Packages » Package Contents Search Results

    Search for libphp7.0.so within filenames
    Search for paths ending with libphp7.0.so 

Search in other suite: [trusty] [xenial] [yakkety] [zesty] [artful]

Limit search to a specific architecture: [amd64] [arm64] [armhf] [i386] [powerpc] [ppc64el] [s390x]

You have searched for files named libphp7.0.so in suite yakkety, all sections, and all architectures. Found 2 results.
File    Packages
/usr/lib/apache2/modules/libphp7.0.so   libapache2-mod-php7.0
/usr/lib/libphp7.0.so   libphp7.0-embed