How to install and configure PHP 8.1 or later/earlier with Apache on Raspberry Pi running Raspberry Pi OS (Buster or Bullseye).
Raspberry Pi OS release track stable Debian release, the current being Bullseye. PHP is included in Bullseye, but unfortunately it is from the PHP 7 series that is no longer supported upstream and therefore not recommended to be used anymore. Upstream PHP support is time limited, PHP 7 is no longer supported and you should use 8.0, 8.1 or 8.2. As there can be major or minor incompatibilities you might need to run a specific version depending on the PHP based application that you are using. For example, if you are running Drupal 9 or 10 site the supported version is 8.0 and recommended version is 8.1
|Branch||Initial Release||Active Support Until||Security Support Until|
|28 Nov 2019||28 Nov 2021||28 Nov 2022|
|8.0||26 Nov 2020||26 Nov 2022||26 Nov 2023|
|8.1||25 Nov 2021||25 Nov 2023||25 Nov 2024|
|8.2||8 Dec 2022||8 Dec 2024||8 Dec 2025|
- Debian Buster comes with version 7.2.9 that is already EOL:ed upstream and 7.3 is now unsupported too.
- Debian Bullseye comes with version 7.4.30 that is EOL
- Debian Bullseye does NOT come with PHP 8
Note: The packages from the repository we will use are unfortunately not compatible with the ARMv6 based Raspberry Pis, you need to be using a ARMv7 or ARMv8 based Raspberry Pi 2, 3 or 4.
This tutorial will show installation instructions for PHP 8.1 but in case you need to use something else like 8.2 or still 7.4, just adjust the versions in the packages and commands. And there is also the issues of the recommended way to run PHP these days. It is not as a Apache module, libapache2-mod-php, anymore!
What we need to do is:
- Add apt source repository for up to date PHP packages
- Install Apache with required modules
- Install PHP 8.1 with optional extras. In this guide we will start with installation of version 8.1
- Optional: Parallel install other PHP versions
Add the PHP repository
So how to get a fresh version of PHP then ? Luckily the Debian PHP maintainer hosts a repository with fresh version of PHP and it includes armhf (Note: armv7 or above only!) versions too. Lets start by adding the repository PGP key:
wget -q https://packages.sury.org/php/apt.gpg -O- | sudo tee /etc/apt/trusted.gpg.d/php.gpg
And add the PHP repository to your apt sources (adjust for the Raspberry OS version you are running, stretch/buster or bullseye)
echo "deb https://packages.sury.org/php/ bullseye main" | sudo tee /etc/apt/sources.list.d/php.list
Update repository pacakge index
sudo apt update
In case apt complains about "The method driver /usr/lib/apt/methods/https could not be found", install the following optional packages:
sudo apt install ca-certificates apt-transport-https
Install Apache and FastCGI module
Next we will install Apache and the required extra module for FastCGI support:
sudo apt install apache2 libapache2-mod-fcgid
Verify that apache work by going to http://YOUR-RPI-IP with your browser. If all is well and you get a response, continue with installation of PHP.
Install PHP 8.1 and common modules
Next we will install PHP 8.1 with some common modules, but not the Apache module version! Instead we will use php-fpm, the FastCGI version.
If you need some other version, just adjust the version in the package names and commands. Later we will show how you can have multiple PHP version running in parallel.
sudo apt install php8.1-cli php8.1-fpm \ php8.1-opcache php8.1-curl php8.1-mbstring \ php8.1-pgsql php8.1-zip php8.1-xml php8.1-gd
The above list is some of the most common PHP modules that you might need, for example for running Drupal. If you need something else, just use: apt search php8 to find them.
Enable apache fpm proxy module and enable php-fpm configuration with (make sure you installed both apache2 and libapache2-mod-fcgid first!)
sudo a2enmod proxy_fcgi sudo a2enconf php8.1-fpm
And reload Apache
sudo systemctl reload apache2
Test the installation
Test PHP cli
It should response with something close to:
PHP 8.1.12 (cli) (built: Oct 28 2022 18:35:51) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.12, Copyright (c) Zend Technologies with Zend OPcache v8.1.12, Copyright (c), by Zend Technologies
Test serving a PHP page
Create a "test.php" file in /var/www/html with your favorite editor, use the contents:
<?php phpinfo(); ?>
And the point your browser to http://YOUR-RPI-IP/test.php
Your browser should now show the PHP information page with all kinds of details.
Configure PHP & PHP-FPM
The default FPM configuration are quite suitable for a small to medium site and the power of a Pi. But in case you need to adjust them see the php-fpm configuration file:
And for PHP specific configuration see:
Parallel install other PHP versions
With php-fpm it is possible to have multiple version of PHP installed and served by Apache. You have one version configured as the default and then Virtual hosts can run some other version.
This might be needed in case your PHP based software required some specific version or is not yet compatible with the latest version. The following example will show how to run multiple versions at the same time.
Install other PHP versions
Installing other versions of php-fpm is straightforward, just install the phpVersion-fpm packages you need. For example, to run both 8.1 and 7.4, install both fpm packages with:
sudo apt-get install php7.4-fpm php8.1-fpm
Example parallel configuration
We will create two virtual hosts, running on custom ports. Edit Apache configuration file /etc/apache2/ports.conf and add the following lines after "Listen 80":
Listen 8080 Listen 8081
Then edit the default VirtualHost configuration in /etc/apache2/sites-available/000-default.conf and add two more VirtualHost settings:
<VirtualHost *:8080> <FilesMatch ".+\.ph(ar|p|tml)$"> SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost" </FilesMatch> </VirtualHost> <VirtualHost *:8081> <FilesMatch ".+\.ph(ar|p|tml)$"> SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost" </FilesMatch> </VirtualHost>
Then restart apache. Now when visiting the test page with http://YOUR-RPI-IP:8080/test.php or http://YOUR-RPI-IP:8081/test.php will give you different PHP versions. You can of course run them on different IP addresses or paths instead of ports.
Enjoy PHP 8 on your Pi!