Install PHP 8.3, 8.2, 8.1, 8.0, 7.4 on Debian 12, 11 or 10

Last Updated on Friday, October 20, 2023 by Joshua James

If you’re using Debian and looking to set up a dynamic website or web application, PHP is a go-to choice. This guide will help you “install PHP 8.3, 8.2, 8.1, 8.0, 7.4 on Debian 12 Bookworm, Debian 11 Bullseye or Debian 10 Buster.” PHP is a server-side programming language that’s widely used for web development. It’s versatile, easy to integrate with databases, and has a strong community backing.

Here’s why many web developers favor PHP:

  • Server-Side Execution: PHP runs on the server, delivering the final output as HTML to the user’s browser.
  • Works Everywhere: Whether on Windows, Linux, or macOS, PHP is there for you.
  • Database Friendly: Connect PHP with popular databases like MySQL, PostgreSQL, or Oracle to power your web applications.
  • Ready-to-Use Functions: PHP has built-in functions for various tasks, from handling strings to managing files.
  • Strong Community: A vast community of developers continuously contributes to PHP, ensuring it stays updated and efficient.
  • Open and Free: Being open-source, PHP is free to use, modify, and distribute.

To get started with PHP on Debian, this guide will introduce you to the Ondřej Surý Repository, ensuring you get the latest and most secure version of PHP for your needs.

Table of Contents

Step 1: Update Debian System Packages

Ensure all existing packages on your Debian operating system are up to date by updating them with the following command.

sudo apt update

If updates are available, use the command to initiate the upgrade process.

sudo apt upgrade

Step 2: Import Ondřej Surý PHP Repository

Install Required Dependencies

To follow this tutorial, specific packages need to be installed. Please run the following command to install them.

sudo apt-get install ca-certificates apt-transport-https software-properties-common curl lsb-release -y

The following step is to import and install the GPG key and repository using an automated script, which can be done by running the curl command. In the terminal, execute the following command.

curl -sSL https://packages.sury.org/php/README.txt | sudo bash -x

Refresh your APT repository list to reflect the changes made in the previous step.

sudo apt update

Specific packages may need updating after running the update command; be sure to update them before proceeding.

sudo apt upgrade

Step 2: Install PHP on Debian Linux

Option 1: Install PHP with Apache Option

You can run PHP as an Apache module or use PHP-FPM with the Apache HTTP server. To make it easier to manage and keep clean, it is best to install only the version of PHP that you require. You can execute the following command to install PHP as an Apache module.

To install the PHP 8.3 module, run the following command:

sudo apt install php8.3 libapache2-mod-php8.3

To install the PHP 8.2 module, run the following command:

sudo apt install php8.2 libapache2-mod-php8.2

To install the PHP 8.1 module, run the following command:

sudo apt install php8.1 libapache2-mod-php8.1

To install the PHP 8.0 module, run the following command:

sudo apt install php8.0 libapache2-mod-php8.0

To install the PHP 7.4 module, run the following command:

sudo apt install php7.4 libapache2-mod-php7.4

Once installation is complete, restart your Apache server for the new PHP module to be loaded.

sudo systemctl restart apache2

Option 2: Install Apache with PHP-FPM

PHP-FPM (FastCGI Process Manager) is a widely used alternative to the standard PHP (Hypertext Processor) FastCGI implementation.

Select the command to correspond to the version of PHP, PHP-FPM with Apache you want to install.

To install PHP and PHP-FPM 8.3 with Apache, use the following command.

sudo apt install php8.3-fpm libapache2-mod-fcgid

To install PHP and PHP-FPM 8.2 with Apache, use the following command.

sudo apt install php8.2-fpm libapache2-mod-fcgid

To install PHP and PHP-FPM 8.1 with Apache, use the following command.

sudo apt install php8.1-fpm libapache2-mod-fcgid

To install PHP and PHP-FPM 8.0 with Apache, use the following command.

sudo apt install php8.0-fpm libapache2-mod-fcgid

To install PHP and PHP-FPM 7.4 with Apache, use the following command.

sudo apt install php7.4-fpm libapache2-mod-fcgid

Please note that PHP-FPM is not enabled for Apache by default. You must enable it using the following command.

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php{version}-fpm

It’s important to note that you must replace the {version} section in the above command with your desired PHP version. For example, if you want to use PHP 8.2, the command should be as follows.

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.2-fpm

Finally, restart the Apache web server.

sudo systemctl restart apache2

Option 3: Install PHP support for Nginx on Debian Linux

Install PHP & PHP-FPM

Unlike other web servers like Apache, Nginx does not have built-in support for processing PHP files. You must install PHP-FPM (FastCGI Process Manager) to handle PHP files with Nginx. Use the following terminal command to install PHP and PHP-FPM for Nginx.

To install PHP 8.3 for Nginx support, use the following command.

sudo apt install php8.3 php8.3-fpm php8.3-cli -y

To install PHP 8.2 for Nginx support, use the following command.

sudo apt install php8.2 php8.2-fpm php8.2-cli -y

To install PHP 8.1 for Nginx support, use the following command.

sudo apt install php8.1 php8.1-fpm php8.1-cli -y

To install PHP 8.0 for Nginx support, use the following command.

sudo apt install php8.0 php8.0-fpm php8.0-cli -y

To install PHP 7.4 for Nginx support, use the following command.

sudo apt install php7.4 php7.4-fpm php7.4-cli -y

After installation, the PHP-FPM service will automatically start running. If this fails to occur, use the following command.

sudo systemctl start php{version}-fpm

Configure Nginx Server Block for PHP-FPM

For Nginx to process PHP files, you will need to edit your Nginx server block and add the following example. This should be added to all server blocks that handle PHP files, specifically the “location ~ .php$.”

location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php{version}-fpm.sock;
}

Replace {version} with your desired PHP version.

You can test it using the following command to ensure no errors with the adjustments made to your Nginx configuration.

sudo nginx -t

It will check the syntax of the configuration files and tell you if there are any issues.

Example output if successful:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To finalize the installation, it is necessary to restart the Nginx service.

sudo systemctl restart nginx

As a reminder, you can use the following command to check the PHP version currently installed on your system.

php --version

Example output with PHP 8.2 installed: