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

Overview

  1. Requirements

  2. Install PHP

  3. Integration with Apache2

  4. Install modules

  5. Wrapping up

1. Requirements

2. Install PHP

ℹ️ The current version of PHP when writing this article was 8.2.10

2.1. Preparing to get the repositories

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.

  1. Update the package list and upgrade the existing packages, as usual.

    sudo apt update
    sudo apt upgrade
  2. 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.

2.2. Set up the repositories in the APT package manager

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.

  1. Download the GPG key

    sudo wget -qO /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
  2. Add the PHP repository

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

    sudo apt update

2.3. Install PHP

I'll install the PHP 8.2 with the command line interface (CLI).

sudo apt install -y php8.2-common php8.2-cli

2.4. Test the installation

Let's quickly create a PHP script

  1. Create and edit a PHP file in our ~/tmp directory (because you already have a tmp directory in your home, right?)

    nano ~/tmp/test.php
  2. Add some random PHP content

    <?php echo "Hello World\n" ?>
  3. Save (ctrl + o) and exit (ctrl + x)

  4. Parse the script

    php ~/tmp/test.php

    It should output Hello World!

3. Integration with Apache2

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

  1. Install the Apache2 module

    sudo apt install -y libapache2-mod-php8.2
  2. Restart Apache2

    sudo service apache2 restart

4. Install modules

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
  • json - PHP JSON Library implements the JavaScript Object Notation data-interchange format
  • 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]

... where [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

But some of them don't work like this

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:

  • curl 
  • dom - is installed with xml
  • gd 
  • mbstring 
  • xml 
  • zip
sudo apt install php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip

Then,

  • 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 php8.2-cli.

4.2. Which modules / extensions do I have installed?

We can use the following terminal command to see which modules or extensions do I have already installed:

php -m

It should output something like the following:

[PHP Modules]
calendar
Core
ctype
curl
date
dom
exif
FFI
fileinfo
filter
ftp
gd
gettext
hash
iconv
json
libxml
mbstring
openssl
pcntl
pcre
PDO
Phar
posix
random
readline
Reflection
session
shmop
SimpleXML
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

5. Wrapping up

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.

Previous Post Next Post