Installing PHP on a Raspberry PI is actually pretty straightforward, PHP is available in the Raspberry’s package repository. But, at the time of this writing, Just PHP 7.4 is available. Still ok, not terribly outdated, but dropped out of active support on November 28th, 2021 and will be EoL with November 28th, 2022. See PHP Supported Versions Overview.

With this in mind, we’re going for the latest PHP version, which – at this time – is 8.1. To install this one, some minor steps are needed, nothing too big, just a couple of commands to copy/paste.

Note: This guide is not only for Raspberry Pi and may also be used for Debian and/or Ubuntu (untested though).

Install PHP 8.1

First off, the respective sources need to be added. For this, the GPG keys need to be downloaded to verify the packages.

sudo wget -qO /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

Now add the repository with:

echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

And update the package list with:

sudo apt update

Now, PHP 8.1 and all its extensions can be installed using apt.

Example with the most common extensions:

sudo apt install -y php8.1-common php8.1-cli php8.1-mysql php8.1-xml php8.1-curl php8.1-zip php8.1-gd php8.1-imap 

Check which PHP version is active with:

php --version

Installed PHP modules/extensions can be checked with:

php -m

Integrate With Your Apache2 Webserver

To integrate PHP 8.1 with your Apache2 webserver, simply run:

sudo apt install libapache2-mod-php8.1

In case there is already another version of PHP used by your Apache2, the installation will return something like this:

...
Creating config file /etc/php/8.1/apache2/php.ini with new versionlibapache2-mod-php8.1: php8.0 module already enabled, not enabling PHP 8.1

In this case, the old PHP version needs to be disabled and the new one enabled. (In this example PHP 8.0 is the old version)

sudo a2dismod php8.0
sudo a2enmod php8.1

Now the Apache2 webserver needs to be restarted, which you do with this command:

sudo systemctl restart apache2.service

Test PHP

In the public directory of your webserver (/var/www/html/ by default) place a file called phpinfo.php with the following content:

<?php
phpinfo();

Now when opening this file in your web browser the PHP info page should be returned detailing everything about the PHP installation and configuration.

Make sure this page is not publically available though, it might expose some information of your installation you want to keep for yourself.

Uninstall PHP

To completely uninstall PHP again, run the following command:

sudo apt purge --autoremove -y php-common mime-support

Remove GPG keys and the repository with:

sudo rm -rf /etc/apt/trusted.gpg.d/php.gpg
sudo rm -rf /etc/apt/sources.list.d/php.list

And last but not least, remove the systemd file with:

sudo rm -rf /var/lib/systemd/timers/stamp-phpsessionclean.timer

Leave a Reply

Your email address will not be published. Required fields are marked *