Setup PHP 7 and Apache on a Raspberry Pi

05/03/2021 - 10:26
Average: 4.4 (5 votes)
PHP 7.4 phpinfo example output

Pretty straight forward operation ? Sure, it is just an apt-get away, but once again we have the issue of old version in Buster  (and Stretch if you are still on that). 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 7.4. Debian Buster comes with version 7.2.9 that is already EOL:ed upstream and 7.3 is on security fixes only.

Note: The packages are not compatible with the ARMv6 based Raspberry Pis, you need to be using a ARMv7 based Raspberry Pi 2, 3 or 4.

This tutorial will show installation instructions for PHP 7.3 but in case you need to use something else like 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:

  1. Add apt source repository for uptodate PHP packages
  2. Install Apache with required modules
  3. Install PHP 7 with optional extras. In this guide we will start with installation of version 7.3
  4. Optiona: Parallel install other 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

 echo "deb https://packages.sury.org/php/ buster main" | sudo tee /etc/apt/sources.list.d/php7.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 7 and common modules

Next we will install PHP 7.3 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 in case your code requires 7.2 or 7.4, 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 php7.3-cli php7.3-fpm \
 php7.3-opcache php7.3-curl php7.3-mbstring \
 php7.3-pgsql php7.3-zip php7.3-xml php7.3-gd

Enable fpm proxy moudle and php-fpm configuration

 sudo a2enmod proxy_fcgi
 sudo a2enconf php7.3-fpm

And reload Apache

 sudo systemctl reload apache2

Test the installation

Test cli PHP

php7.3 -v

It should response with something close to

PHP 7.3.27-9+0~20210227.82+debian10~1.gbpa4a3d6 (cli) (built: Feb 27 2021 15:50:50) ( NTS )

Copyright (c) 1997-2018 The PHP Group

Zend Engine v3.3.27, Copyright (c) 1998-2018 Zend Technologies

    with Zend OPcache v7.3.27-9+0~20210227.82+debian10~1.gbpa4a3d6, Copyright (c) 1999-2018, 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:

/etc/php/7.3/fpm/php-fpm.conf

And for PHP specific configuration see:

/etc/php/7.3/fpm/php.ini

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 7.2 and 7.4, install both packages with:

sudo apt-get install php7.2-fpm php7.4-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.2-fpm.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>

<VirtualHost *:8081>
    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php7.4-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.

Enjoy PHP 7 on your Pi!

Keywords: 
PHP, Apache, PHP7, Raspberry Pi