Keep PHP up to date on Debian 9 Stretch

This story might be outdated:
PHP 7.x is now end-of-life. You might wanna update to PHP 8.x (and also update to a newer Debian?)

When installing PHP on Debian 9, the default version is PHP 7.0, that have been declared End-of-life since January 10, 2019.

PHP 7.0 will not go further version 7.0.33 on Debian 9.

Adding a new repository for newer versions of PHP

First, this repository uses APT transport over HTTPS. This is basically getting packages over a TLS connection.
Let’s install the necessary packages, if you do not have them already:

sudo apt-get install ca-certificates apt-transport-https

Then, we add that will be our new source for installing and updating PHP:

wget -q -O- | sudo apt-key add -
echo "deb stretch main" | sudo tee /etc/apt/sources.list.d/php.list

Upgrading PHP 7.0 with patches

If you have already php7.0 packages installed on your system, you can get patches right away:

sudo apt-get update
sudo apt-get upgrade

Now the installed version of PHP is (at the time of writing) v7.0.33-5+0~20190309015553.9.
It contains some patches from higher version of PHP, added by the community.

This gives you extra time to update your code and support higher version of PHP before upgrading.

Upgrade PHP

You may want to update PHP to a later release, after you’ve made sure your code can support it!

You have 2 options: installing the latest release (7.3 when this was written), or installing a specific version (7.1 or 7.2). You can also perform both, if you need more than one version installed.

Those operations will not uninstall other version of PHP, and will not change Apache and/or Nginx configuration, so your websites will still use 7.0 by default.

a) Install PHP’s latest version

You can now install the current version of PHP (as of today, the 7.3) using:

sudo apt-get install php php-cli php-fpm php-mysql

If the package is already installed, you may need to run instead:

sudo apt-get dist-upgrade

b) Install PHP 7.x

You may want to install any version of PHP using any of the following commands:

# PHP 7.1
sudo apt-get install php7.1 php7.1-cli php7.1-fpm php7.1-mysql

# PHP 7.2
sudo apt-get install php7.2 php7.2-cli php7.2-fpm php7.2-mysql

You’re free to install any additional extensions that fit your needs.

Update your HTTP server

In order to your server to use the new version of PHP, you have to update its configuration.

a) Apache2

On Apache, you need to disable the old PHP handler before using the new.
More advanced configurations will allow you to use different versions of PHP on different websites if needed.

sudo a2dismod php7.0
sudo a2enmod php7.3 # Adapt the version of PHP with the version you want to use
sudo service apache2 restart

b) Nginx

With Nginx, you just need to edit your fastcgi configuration to make usage of the fpm version you need:

By default, those settings are in nginx.conf, but they may be inside a sites-available/ subfolder, or in a included custom configuration file.

sudo nano /etc/nginx/nginx.conf

Update the fastcgi_pass value according to the PHP version you’d like to use.

Then, simply restart nginx:

sudo service nginx restart

Optional: uninstall PHP 7.0

If you don’t need it anymore, it can be nice to perform some cleanup operations and remove PHP 7.0 from your server.

Be careful not to delete package that you still need by reviewing them before confirming!

sudo apt-get purge "php7.0*"

If the list seems okay, you may confirm the deletion, and now PHP 7.0 is completely gone!

Don’t miss a thing!

Don't miss any of my indie dev stories, app updates, or upcoming creations!
Stay in the loop and be the first to experience my apps, betas and stories of my indie journey.

Thank you for registering!
You’ll retrieve all of my latest news!

Anti-bot did not work correctly.
Can you try again?

Your email is sadly invalid.
Can you try again?

An error occurred while registering.
Please try again