Setup PHP 7 on a Raspberry Pi

25/02/2019 - 11:13
Average: 4.4 (5 votes)

Pretty straight forward operation ? Sure, it is just an apt-get away, but once again we have the issue of old versions in Stretch. And with PHP that is especially a big issues as upstream PHP support is time limited. Debian stretch comes with version 7.0.x that is already EOL:ed upstream and 7.1 is also already on security fixes only.

This tutorial will show installation instructions for PHP 7.2 but in case you need to use something else like 7.3, 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.2
  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 versions too (Note: ARMv6 support has not been tested, so assume that you need a ARMv7 based Raspberry Pi 2 or 3). Lets start by adding the repository PGP key:

 wget -q -O- | sudo apt-key add -

And add the PHP repository to your apt sources

 echo "deb stretch main" | sudo tee /etc/apt/sources.list.d/php7.list

Update repository pacakge index

 sudo apt-get 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-get 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-get 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.2 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.0 or 7.3, 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-get install php7.2-cli php7.2-fpm \
 php7.2-opcache php7.2-curl php7.2-mbstring \
 php7.2-pgsql php7.2-zip php7.2-xml php7.2-gd

Enable fpm proxy moudle and php-fpm configuration

 sudo a2enmod proxy_fcgi
 sudo a2enconf php7.2-fpm

And reload Apache

 sudo systemctl reload apache2

Test the installation

Test cli PHP

 php -v

It should response with something close to

 PHP 7.2.13-1+0~20181207100540.13+stretch~1.gbpf57305 (cli) (built: Dec  7 2018 10:05:40) ( NTS )
 Copyright (c) 1997-2018 The PHP Group
 Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
     with Zend OPcache v7.2.13-1+0~20181207100540.13+stretch~1.gbpf57305, 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:


And for PHP specific configuration see:


Parallel install other PHP versions

With the 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 software required some specific version or is not yet compatible with the latest version. The following example will show how to run 7.0, 7.2 (as default) and 7.3 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.0 and 7.3, install both packages with:

sudo apt-get install php7.0-fpm php7.3-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.3-fpm.sock|fcgi://localhost"

<VirtualHost *:8081>
    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost"

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!

14/01/2019 - 14:05
PHP, Apache, PHP7, Raspberry Pi