Upgrade PHP and PHP-FPM from the Command Line on a Debian LAMP Stack

In this guide, we discuss the steps to both install and upgrade PHP using the command line on a Debian system running Apache HTTP Server. If you followed my guide to Install a LAMP Stack Manually on a Debian OS Only Amazon Lightsail Instance, then one of the identified benefits for building and operating your own server is that you control the installation of software versions in that environment. Since you own the environment, you can install the latest and greatest version of PHP without waiting for a managed hosting provider to make it available in a shared environment.

Getting Started

If you don’t know what version of PHP is installed or even if PHP is installed at all, let’s start by collecting some basic information.

At the shell prompt, execute the following command:

php -v

Using the -v option displays the version number for the executed PHP binary as there may be multiple installed instances of PHP. The command displays a message similar to the following. In this instance, PHP 8.1.17 is installed.

PHP 8.1.17 (cli) (built: Mar 16 2023 14:37:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.17, Copyright (c) Zend Technologies
with Zend OPcache v8.1.17, Copyright (c), by Zend Technologies

If the command displays the following message, then PHP is likely not installed or the path to an installed PHP binary is not properly set up.

-bash: php: command not found

If PHP is already installed, then you can skip the following steps for a new PHP installation. If PHP is not installed, then proceed with the steps for a new PHP installation.

Adding a PHP Repository to APT for a New Installation

Before we proceed to the PHP installation/upgrade steps, let’s update the server’s package list from the Debian software repository using apt update and then download and install the current updates using the apt upgrade command. APT stands for Advanced Package Tool. It handles the installation, management, and removal of software packages on Debian systems. The sudo command enables users to execute programs with the security privileges of another user, e.g., the superuser for elevated privileges to install or remove software.

At the shell prompt, execute the following command:

sudo apt update && sudo apt upgrade -y

Since PHP is not typically included in the default repository list known by APT, we need to configure APT to make it aware of a repository with the required software packages for PHP.

First, we need to install several additional supporting packages allowing APT to access repositories via HTTPS. There is no harm executing this command if these packages are already installed.

At the shell prompt, execute the following command:

sudo apt install -y apt-transport-https ca-certificates gnupg2 software-properties-common

Next, we import the GPG signing keys for the package from the repository. I am using the Sury package repository in this example.

At the shell prompt, execute the following command:

sudo wget -qO /etc/apt/trusted.gpg.d/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg

Finally, we need to add this PHP repository to APT.

At the shell prompt, execute the following command:

echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

At this point, APT is now aware of a PHP package repository and we will be able to download and install the PHP package in subsequent steps.

Installing or Upgrading PHP Using APT

The steps to either install or upgrade PHP are basically the same. In either case, we are installing a new version of PHP. The only difference with an upgrade is that there are few additional (optional) steps to remove prior PHP versions.

Before we install the PHP package, execute an apt update and apt upgrade to make sure we are working with the latest packages. Perform this step even if it was executed in the prior section to add a new PHP repository.

At the shell prompt, execute the following command:

sudo apt update && sudo apt upgrade -y

We are installing PHP version 8.2 which is the current stable version at the time this guide was written. Always verify the current stable version on the PHP Downloads index and adjust the following commands as needed. Please note that specifying 8.2 will install the latest patch version, e.g., 8.2.4 in this instance. This command also installs some commonly used modules and extensions, e.g., fpm, mysql. Please add or remove modules as needed.

At the shell prompt, execute the following command:

sudo apt install -y php8.2 php8.2-{fpm,mysql,curl,gd,imagick,intl,mbstring,mysql,xml,mcrypt,zip,ldap} libapache2-mod-php8.2

PHP 8.2 is now installed.

Configuring Apache

If you are using PHP with Apache HTTP Server and PHP-FPM, then the following sequence of commands configures Apache to use the new version of PHP. Please adjust the version numbers as needed for your server. In this example, we are upgrading from PHP 8.1 to PHP 8.2.

At the shell prompt, execute the following commands:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.2-fpm
sudo a2disconf php8.1-fpm
sudo a2dismod php8.1
sudo a2dismod php8.2
sudo systemctl enable php8.2-fpm
sudo systemctl reload apache2
sudo service apache2 restart
sudo service php8.2-fpm restart

Verification

At the shell prompt, execute the following command:

php -v

The following message is displayed. As expected, the current version is now 8.2.4 which is the latest stable version at the time this article was written.

PHP 8.2.4 (cli) (built: Mar 16 2023 14:37:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies

To verify Apache HTTP Server is serving PHP requests using the expected version of PHP, a temporary PHP file is added to an available virtual host. Create a file with the following code. The output of the phpinfo() function contains information about PHP’s configuration. Use this information to verify that the expected version of PHP is working correctly with Apache HTTP Server and it is processing requests for .php files.

For this guide, a file named phpinfo_test.php is created with the following content and stored in a location on the server accessible by a browser.

<?php
phpinfo();
?>

Using a browser, request the file from the server. The response renders with detailed environment information similar to the following screens. Verify that the displayed PHP version is as expected.

phpinfo – PHP Version
phpinfo – PHP Version
phpinfo – Zend Version
phpinfo – Zend Version
phpinfo – PHP-FPM
phpinfo – PHP-FPM
phpinfo – Core Version
phpinfo – Core Version

After you have completed the verification that the upgrade was successful, it is important to remove this temporary file. This file displays a lot of information about the server and its operating environment which could be useful to a threat actor if the file was discovered.

At the shell prompt, execute the following command:

sudo rm phpinfo_test.php

Removing Older PHP Versions

Before proceeding, please verify in the previous step that any virtual hosts with PHP content are executing as expected and with the expected version of PHP. If there was an issue with the upgrade, any hosted sites that require PHP may be rendered unavailable. Please also consider that removing older installed versions of PHP is an optional step. In this instance, the older version is PHP 8.1 so please adjust the command as needed for your environment.

sudo apt purge php8.1* -y

Leave a Comment