PHP 5.6 and PHP 7.3 on Ubuntu 16.04

I have a situation with the PHP websites on my Ububtu server. I would like to upgrade the server to PHP 7.3 but some of them may fail to run under that. Therefore, I am going to install PHP 7.3 whilst keeping PHP 5.6 as my default in Apache until I am satisfied I can safely switch and remove PHP 5.6.

I actually upgraded the server from 14.04 to 16.04 earlier this year but kept PHP 5.6 and have not installed any 7.x versions.

Getting Prepared for PHP 7.3

To start with I can confirm that I am running PHP 5.6 by default from the command line. Then add the “ondrej” repository and install any upgrades. The following commands are run in order.

php -v
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update
sudo apt upgrade -y

apache-php-switch01
apache-php-switch02
apache-php-switch03
apache-php-switch04

Installing PHP 7.3

Install PHP 7.3 and PHP7.3-MySQL

sudo apt install php7.3
sudo apt install php7.3-mysql

apache-php-switch05
apache-php-switch06

Switching between PHP versions in Apache

Check which version of PHP Apache is using by default in a browser. Disable PHP 5.6 in Apache, enable PHP 7.3 and restart Apache. Check the version again in a browser. Once satisfied PHP 7.3 is running, switch back to PHP 5.6 as the default.

First disable 5.6 then enable 7.3

sudo a2dismod php5.6
sudo a2enmod php7.3
sudo systemctl restart apache2

apache-php-switch07

Create a couple of directories “new” and “old” and then add files that return phpinfo().

sudo mkdir /var/www/html/new
sudo mkdir /var/www/html/new
sudo nano

apache-php-switch08
apache-php-switch09
apache-php-switch10

Check the version in a browser.

apache-php-switch11

Once satisfied PHP 7.3 is running, switch back to PHP 5.6 as the default then check again in the browser

sudo a2dismod php7.3
sudo a2enmod php5.6
sudo systemctl restart apache2

apache-php-switch12
apache-php-switch13

Switching PHP versions on a site or in a directory

Install FPM versions of PHP, enable proxy_fcgi, check what the default PHP version is for the site. Then modify the site config file or add a htaccess file to the root of the directory that you want to switch PHP versions away from the default.

First, install and enable the PHP sockets for each. Then check in the “new” directory in the browser that PHP 5.6 is still running by default.

sudo apt install php7.3-fpm php5.6-fpm
sudo a2enmod actions alias proxy_fcgi
sudo systemctl restart apac

apache-php-switch14
apache-php-switch15
apache-php-switch16

Create a “.htaccess” file in the new directory with the following content. Alternatively, you can modify the system configuration if you want a whole site to run a particular PHP version. Then test the “new” directory in the browser to make sure it now returns PHP 7.3.

Check the version again in a browser. Once satisfied PHP 7.3 is running, switch back to PHP 5.6 as the default.

<FilesMatch \.php$>
SetHandler “proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost”
<\FilesMatch>

apache-php-switch17
apache-php-switch18
apache-php-switch19

Switching PHP versions outside of Apache

We have Apache squared away but you might use PHP on the server outside of Apache. You can switch that easily too.

Check the current PHP version (which will be whatever was installed last regardless of the fact that we are using PHP 5.6 in Apache). Set some alternatives to the automatic default version (7.3 and then 5.6). PHP will now be set manually to the version set last so check that. Switch it back to Auto or use a manual mode via the alternative config.

php -v
sudo update-alternatives ‐‐set php /usr/bin/php7.3
sudo update-alternatives ‐‐set php /usr/bin/php5.6
php -v

apache-php-switch20
apache-php-switch21

As PHP 5.6 was set here last, that will be the default version. It will have no affect on the version enabled in Apache.

To switch outside of Apache, use the config. Here I’ll switch from 5.6 to 7.3 and then back again.

sudo update-alternatives ‐‐config php
php -v

apache-php-switch22
apache-php-switch23

Finally

You should clean up your test files. I’m going to leave the “/var/www/html/new/.htaccess” file in place so I can copy it to later.

sudo rm /var/www/html/new/index.php
sudo rm -r /var/www/html/old

apache-php-switch25

I should have mentioned that I had PHP-GD locked on one server but not on the other so if you don’t want the PHP update to overwrite a previously applied GD fix then you should make sure you apply a hold beforehand. if you do get stuck with PHP-GD then check out my article https://pathowe.co.uk/upgrade-to-php-5-6-on-ubuntu-with-gd-library-fix/.

sudo apt-mark hold php-gd

apache-php-switch24

It is also worth mentioning that PHP 5.6 is no longer being updated and you should move to the latest version of PHP before any security incident occurs.

Running multiple versions of PHP on Ubuntu

Leave a Reply