How to install and configure PHP 8.1 (and 7.4) with Apache on Raspberry Pi running Raspberry Pi OS (Buster or Bullseye).
Pretty straight forward operation ? Sure, it is just an apt-get away, but once again we have the issue of old version in Bullseye (and Buster, Stretch if you are still on any of those). And with PHP that is especially a big issues as upstream PHP support is time limited, the only properly supported branch from PHP 7 is version 7.4 and even that will soon be EOL (28 Nov 2021 with security updates until 28 Nov 2022).
|Branch||Initial Release||Active Support Until||Security Support Until|
|7.4||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|
- Debian Buster comes with version 7.2.9 that is already EOL:ed upstream and 7.3 is now unsupported.
- Debian Bullseye comes with version 7.4.30 that is EOL already with security updates stopping very soon, 28 Nov 2022
- 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.0 or 7.4, just adjust the versions in the package 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 todate 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 (armv7 only!) versions too. Lets start by adding the repository PGP key:
wget -q https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
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
Enable apache fpm proxy module and enable php-fpm configuration with
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!