edan.uk
Posted on
Linux

Installing and Upgrading to PHP 8.4 on Debian and Ubuntu

Author
Installing and Upgrading to PHP 8.4 on Debian and Ubuntu

PHP 8.4 introduces a host of new features and enhancements that improve performance and security, alongside some deprecated functionalities. For users looking to either install PHP 8.4 fresh or upgrade from an existing version, there are efficient methods available for both Debian and Ubuntu systems. Utilizing a trusted APT repository streamlines the installation process while ensuring access to regular updates and security patches.

Assessing Compatibility with Existing Applications

Transitioning to PHP 8.4 may come with certain backward compatibility challenges, as some functions are deprecated and others have been removed entirely. Key changes include:

  • Implicitly Nullable Parameters: This feature is deprecated, and developers may need to revise their function signatures to explicitly declare nullable types.

  • Removal of E_STRICT: This constant's deprecation means applications reliant on it may face adjustments.

  • Extensions Transition: Notably, several extensions such as Pspell, IMAP, OCI8, and PDO_OCI have moved from core PHP to PECL, which may impact existing applications and necessitate additional management.

Backup is highly recommended before any changes are implemented, allowing for easy reversion if issues occur during the upgrade or installation process.

Listing Existing PHP Packages

For those upgrading an established PHP setup, it is crucial to catalogue currently installed PHP packages. The following command generates a list of all installed PHP packages and writes them to a file named packages.txt for later reference.

dpkg -l | grep php | tee packages.txt

Adding the Ondřej Surý PPA/DPA Repository

PHP 8.4 is not included in the default repositories for Debian or Ubuntu, making it necessary to add external repositories.

For Ubuntu Users:

sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php 
sudo apt update

For Debian Users:

sudo apt-get update
sudo apt-get -y install lsb-release ca-certificates curl apt-transport-https
sudo curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
sudo dpkg -i /tmp/debsuryorg-archive-keyring.deb
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt-get update

Installing PHP 8.4 Server API Packages

Once the repository is added, the installation of PHP 8.4 SAPI packages can commence. The packages cater to different needs based on how PHP will be utilized.

PHP CLI Only Installation

For users who only need the command-line interface, the following command achieves this:

sudo apt install php8.4-cli

PHP for Apache Installation

For those wishing to use both PHP CLI and Apache, install the module with:

sudo apt install php8.4-cli libapache2-mod-php8.4

Recommended PHP-FPM Installation

Using PHP-FPM is recommended for better integration with web servers. Install with:

sudo apt install php8.4-cli php8.4-fpm

Verification of PHP Installation

To confirm that PHP 8.4 has been installed correctly, execute:

php -v

This command will display the current PHP version and additional details, verifying a successful installation.

Moreover, check the status of PHP-FPM if it's installed:

sudo systemctl status php8.4-fpm

Installing PHP Extensions

Most PHP extensions can be installed using the php8.4-EXTNAME naming convention. Refer to your previously saved packages.txt file for existing extensions and install equivalent PHP 8.4 versions.

Example installation of the GD extension:

sudo apt install php8.4-gd

To install a commonly used set of PHP extensions:

sudo apt install php8.4-common php8.4-{bcmath,bz2,curl,gd,gmp,intl,mbstring,opcache,readline,xml,zip}

For further exploration, utilize:

apt search php8.4

Web Server Integration

Ensuring that the web server recognizes the PHP 8.4 installation is critical depending on the chosen SAPI.

PHP-FPM Configuration

For integration with PHP-FPM:

Apache Integration

Enable PHP 8.4 configurations:

sudo a2enconf php8.4-fpm

Nginx Integration

Update the configuration:

fastcgi_pass unix:/run/php/php8.4-fpm.sock; 

Caddy Server Integration

Modify the reverse proxy settings accordingly:

reverse_proxy @phpFiles unix//run/php/php8.4-fpm.sock

PHP as an Apache Module

If installed as an Apache module, disable the previous version and enable PHP 8.4:

sudo a2dismod php8.3 # Adjust this based on the existing version
sudo a2enmod php8.4

Migrating Configuration

PHP configuration files are located in /etc/php/8.4. Rather than copying existing configurations directly, it is advisable to review and adjust them as necessary. Use the diff command to compare configurations:

diff /etc/php/8.3/cli/php.ini /etc/php/8.4/cli/php.ini

Purging Old PHP Versions

Once the PHP 8.4 setup is confirmed to be functioning correctly, you may remove old, unnecessary PHP packages:

sudo apt purge '^php8.3.*'

Running Multiple PHP Versions

PHP 8.4 can coexist with other versions, allowing for easy switching between them. PHP CLI for version 8.4 will be located at /usr/bin/php8.4. To manage the default PHP version, the update-alternatives command can be utilized:

sudo update-alternatives --config php

To set the default PHP version without the interactive prompt:

update-alternatives --set php /usr/bin/php8.4

Conclusion

The installation and upgrade process to PHP 8.4 on Debian and Ubuntu systems is straightforward when following these steps. With careful consideration towards compatibility and proper configuration, users can take full advantage of PHP 8.4's new features and enhancements while maintaining the stability of their applications. Regular monitoring and updates will keep PHP installations secure and efficient.