How to Install PHP 8 on Debian 12

|
Last Updated:
|
|

In this tutorial, you will learn how to install PHP 8 on Debian 12. PHP 8.x is a major update of the PHP language. It contains many new features and optimizations including named arguments, union types, attributes, constructor property promotion, match expression, nullsafe operator, JIT, and improvements in the type system, error handling, and consistency.

Installing PHP 8 on Debian 12

Different applications requires different versions of PHP. In Linux, you can be able to install any version of PHP that your application may require.

Install PHP 8.2 on Debian 12

The default Debian 12 repositories provide PHP 8.2;

apt-cache policy php

php:
  Installed: (none)
  Candidate: 2:8.2+93
  Version table:
     2:8.2+93 500
        500 http://deb.debian.org/debian bookworm/main amd64 Packages

Thus, to installing PHP 8 on Debian 12, simply execute the commands below;

apt update
apt install php

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  apache2 apache2-data apache2-utils libapache2-mod-php8.2 php-common php8.2 php8.2-cli php8.2-common php8.2-opcache php8.2-readline
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom php-pear
The following NEW packages will be installed:
  apache2 apache2-data apache2-utils libapache2-mod-php8.2 php php-common php8.2 php8.2-cli php8.2-common php8.2-opcache php8.2-readline
0 upgraded, 11 newly installed, 0 to remove and 0 not upgraded.
Need to get 5,047 kB of archives.
After this operation, 23.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Type y and press enter to proceed with installation.

php -v

PHP 8.2.5 (cli) (built: Apr 27 2023 08:13:47) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.5, Copyright (c), by Zend Technologies

Install PHP 8.2 Modules

You can install any PHP 8.2 module by executing the command below, replacing the EXTENSION with specific PHP module.

apt install php-EXTENSION

For example, you can install PHP MySQLi extension using the command below;

apt install php-mysql

Install PHP 7.x/8.0/8.1 on Debian 12

Similarly, it is possible to literally install any PHP version as long as you have the correct package repositories in place.

Install SURY APT Repository

To install PHP 7.x/8.0/8.1 on Debian 12, you need to install SURY, third party repositories that provides PHP packages as follows;

apt update
apt -y install apt-transport-https lsb-release ca-certificates curl wget gnupg2
wget -qO- https://packages.sury.org/php/apt.gpg | \
gpg --dearmor > /etc/apt/trusted.gpg.d/sury-php-x.x.gpg
sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" \
> /etc/apt/sources.list.d/php.list'

Re-synchronize your system packages to the latest versions;

apt update

Install PHP 7.x on Debian 12

PHP 7.x is now available for installation;

apt-cache policy php7.4

php7.4:
  Installed: (none)
  Candidate: 1:7.4.33-6+0~20230609.83+debian12~1.gbp32abef
  Version table:
     1:7.4.33-6+0~20230609.83+debian12~1.gbp32abef 500
        500 https://packages.sury.org/php bookworm/main amd64 Packages

Thus, you can install PHP 7.x from SURY repos.

For example to install PHP 7.4;

apt install php7.4

For modules, do (replace EXTENSION with respective PHP module);

apt install php7.4-EXNTENSION

Install PHP 8.0 on Debian 12

To install PHP 8.0 on Debian 12, run the command below;

apt install php8.0

This command will install PHP 8.0 along with other required package dependencies.

Install PHP 8.1 on Debian 12

To install PHP 8.1 on Debian 12, run the command below;

apt install php8.1

You can confirm the installed version;

php -v

Disable SURY repos

Once you are done, you can disable SURY repos;

sed -i 's/^/#/' /etc/apt/sources.list.d/php.list

Replace the path to the source list accordingly.

Testing PHP Execution on Linux

To confirm the execution of PHP, you can create a simple PHP test file and place it in your web server root directory.

For example, in apache;

echo '
<?php
phpinfo();
?>
' > /var/www/html/test.php

Next, restart Apache;

systemctl restart apache2

If using Nginx, ensure PHP-FPM is installed and configure Nginx to process PHP by updating your site config;

For example, we are using the default site config;

vim /etc/nginx/sites-available/default

Next, configure Nginx to process PHP by updating the location ~ .php$ section accordingly.

In the most basic way, this is the config section to update;


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

Replace the following line with the specific address your Nginx is configured to accept FastCGI requests

unix:/run/php/phpX.X-fpm.sock;

You can get the specific address as follows;

grep -E '^listen' /etc/php/8.2/fpm/pool.d/www.conf

Replace 8.3 on that path with your current PHP version number.

Sample output;

listen = /run/php/php8.2-fpm.sock
listen.owner = www-data
listen.group = www-data

So we will update our Nginx config as follows;


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

Restart Nginx;

Then access PHP test page using the address http://server-IP/test.php;

Sample PHP page processed by Apache;

How to Install PHP 8 on Debian 12

How to Set Default PHP Version on Debian

You can set the default PHP version using the update-alternatives command.

If you have multiple versions of PHP installed, there could be some situations that might make you choose a default version to use.

You can list the paths of the installed PHP versions by running the command update-alternatives --list php.

update-alternatives --list php

Sample output;

/usr/bin/php7.4
/usr/bin/php8.1
/usr/bin/php8.2

Thus, to set the default PHP version, you can use the update-alternatives command with the --config option followed by the path of the desired PHP version. For example, to set PHP version 8.1 as the default PHP version, run the command;

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

You can also run the command interactively by omitting the path to PHP;

update-alternatives --config php

There are 3 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/php8.2   82        auto mode
  1            /usr/bin/php7.4   74        manual mode
* 2            /usr/bin/php8.1   81        manual mode
  3            /usr/bin/php8.2   82        manual mode

To set a specific version of PHP as the default version, simply choose a number and press ENTER.

For example, in the above prompt, you can enter 2 and press ENTER to set PHP 8.1 as the default version;


There are 3 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/php8.2   82        auto mode
  1            /usr/bin/php7.4   74        manual mode
* 2            /usr/bin/php8.1   81        manual mode
  3            /usr/bin/php8.2   82        manual mode

Press  to keep the current choice[*], or type selection number: 2

Confirm the version;

php -v

PHP 8.1.20 (cli) (built: Jun  9 2023 07:44:56) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.20, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.20, Copyright (c), by Zend Technologies

That is all on installing PHP 8 on Debian 12.

Other Tutorials

Install Debian 12 on VirtualBox

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
Kifarunix
Linux Certified Engineer, with a passion for open-source technology and a strong understanding of Linux systems. With experience in system administration, troubleshooting, and automation, I am skilled in maintaining and optimizing Linux infrastructure.

Leave a Comment