After installing an Apache in a #RaspberryPi, one of the common next steps is to add support for #PHP. Here I describe the steps to install and set up the PHP version 8.2 into our little host, including some of the most common modules / extensions and the integration with #Apache2
Integration with Apache2
A Raspberry Pi up and running, following my previous article Spawning a Raspberry Pi 4 with Raspberry Pi OS. It is also up to date by following the recent article Upgrade Raspberry Pi OS from 11 Bullseye to 12 Bookworm.
Not mandatory but as it is mentioned in a section below, an Apache2 installed as explained in my previous article Install Apache2 in a Raspberry Pi 4.
The host uses a local DNS server as explained in Quick DNS server on a Raspberry Pi and that's why you won't see IP addresses here.
The hostname is
alderaan and the linux user is
xavi. You may want to change them for your IP address and your own username.
What is the goal of your project? This will define which version of PHP and which modules should we install. Here we'll focus on the latest version of PHP at the moment of writing the article.
ℹ️ The current version of PHP when writing this article was 8.2.10
Before adding the repositories where I will get the packages from, I need to install some Debian packages so it can talk with the server properly.
This step may not be necesserary if we did it already with a previous installation, for example.
Update the package list and upgrade the existing packages, as usual.
sudo apt update
sudo apt upgrade
Install the debian packages that will allow us to interact with the external packages repositories
sudo apt install -y ca-certificates curl gnupg
Now the system is ready to add external repositories and interact with them as usual.
What I intent here is to prepare the system so that it can install the PHP as the rest of the packages of the system. This easies the life when it comes to updates, as we can rely on our package manager.
Download the GPG key
sudo wget -qO /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
Add the PHP repository
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
Update the package list
sudo apt update
I'll install the PHP 8.2 with the command line interface (CLI).
sudo apt install -y php8.2-common php8.2-cli
Let's quickly create a PHP script
Create and edit a PHP file in our
~/tmp directory (because you already have a
tmp directory in your
Add some random PHP content
<?php echo "Hello World\n" ?>
o) and exit (
Parse the script
It should output
I intend to use PHP as a module from Apache2, so that the webserver serve a PHP application. For this reason I also want to install the PHP module for Apache2
Install the Apache2 module
sudo apt install -y libapache2-mod-php8.2
sudo service apache2 restart
As I mentioned in the Overview, depending on the motivation to have PHP we may also want to have a bunch of PHP modules installed. The modules extend the functionality of PHP making it able to work with databases, images, etc.
In my case, I intend to install the following list of PHP modules:
curl - PHP Curl (Data Transfer Library) allows to use the URL syntax to receive and submit data
ctype - PHP Ctype provide a set of functions used to verify whether the characters in a string are of the correct type
dom - PHP DOM allows editing an HTML snippet
gd - PHP GD (Image Manipulation Library) an open source code library for the dynamic creation of images
mbstring - PHP Mbstring (Multibyte String Library) provides support for multibyte strings that are not covered by the standard PHP string functions
openssl - PHP OpenSSL (Secure Sockets Library) is meant to take care of communications that takes place over computer networks
xml - PHP XML Library provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators
zip - PHP Zip extension transparently read or write ZIP compressed archives and the files inside them
In general, to install a module we can use the following command:
sudo apt install php8.2-[module]
[module] is the module extension we want to install. For example, we can install the CURL module for PHP with the following command:
sudo apt install php8.2-curl
This is true. Some of them are distributed under another package, and some others are already installed and activated (which to be honest, I did not expect, for what I remembered in previous versions).
First, let's go for a one liner that installs the ones without issues. This will install:
dom - is installed with
sudo apt install php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip
ctype is included with the
php8.2-common, so it's already installed.
openssl comes already installed and activated.
json is a virtual package, and the functionality is already included when we installed
We can use the following terminal command to see which modules or extensions do I have already installed:
It should output something like the following:
PHP has been the scripting language by default in the linux side of the web servers world for ages. Even now it declines, it is still a very valid and powerful language to move sites and blogs. I use GravCMS, which is a PHP DB-less applicaltion and I am very satisfied with it.
As we saw, it is really not complicated to have PHP installed and, and ready to interact with Apache to serve PHP pages.