Installing PHP 7.2.6 on Raspian Stretch on Raspberry Pi 3 Model B+

Installing PHP 7.2.6 on Raspian Stretch on Raspberry Pi 3 Model B+

I recently wanted to install the PHP framework Laravel 5.6 and needed a more current version of PHP.  You see I discovered that Raspian Stretch’s debian packages of PHP7 only go upto PHP 7.0.27.  Laravel 5.6 requires at least PHP 7.1.3.  I couldn’t find any guides on the internet so I figured it out by myself.  These are the steps to achieve a PHP 7.2.6 installation:

  1. First I downloaded the latest version of PHP (which was 7.2.6 at time of writing) from http://php.net/downloads.php
  2. I then decompressed the tar.gz file.
  3. I then ran the following commands in the shell to setup some packages and items:
    sudo apt-get install libcurl4-openssl-dev libbz2-dev libjpeg-dev libkrb5-dev libmcrypt-dev libxslt1-dev libxslt1.1 libpq-dev git make build-essential 
    sudo apt-get install libc-client2007e libc-client2007e-dev
    sudo mkdir /usr/c-client
    sudo ln -s /usr/lib/libc-client.a /usr/c-client/
    sudo apt-get install pkg-config libssl-dev openssl
  4. It was then time to compile PHP and install it from the shell:
    ./configure --prefix=/opt/php-7.2.6 --with-config-file-path=/opt/php-7.2.6/etc  --with-zlib-dir --with-freetype-dir --enable-mbstring --with-libxml-dir=/usr --enable-soap --enable-calendar --with-curl --with-zlib --with-gd --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --enable-exif --enable-bcmath --with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli --with-mysql-sock=/var/run/mysqld/mysqld.sock --with-jpeg-dir=/usr --with-png-dir=/usr --with-openssl --with-fpm-user=www-data --with-fpm-group=www-data --with-libdir=/lib/arm-linux-gnueabihf --enable-ftp --with-imap --with-imap-ssl --with-kerberos --with-gettext --with-xmlrpc --with-xsl --enable-opcache --enable-fpm
    
    make -j4
    
    sudo make install
  5. Then alter some settings/files as follows:
    sudo cp php.ini-production /opt/php-7.2.6/etc/php.ini
    sudo cp sapi/fpm/php-fpm /usr/local/bin
    sudo update-rc.d php7-fpm defaults
    cd /opt/php-7.2.6/etc/
    sudo mv php-fpm.conf.default php-fpm.conf
    sudo mv php-fpm.d/www.conf.default php-fpm.d/www.conf

     

  6. Then edit the /opt/php-7.2.6/etc/php-fpm.d/www.conf . Search and uncomment or set the following options:
    user = www-data
    group = www-data
    
    listen.owner = www-data
    listen.group = www-data
    
    listen = /var/run/php7-fpm.soc
    user = www-data
    group = www-data
  7. Then some sorting to complete:
    sudo cp /user/opt/php-7.2.6/bin/php /user/local/bin/php
    sudo ln -s /usr/local/bin/php /usr/bin/php

     

  8. PHP can then be started on the server side as follows:
    sudo /usr/local/bin/php-fpm

NGINX

If you are using nginx then you will need to edit the conf files as follows:

location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php7-fpm.soc;
}

Composer

To install composer:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Then edit $HOME/.bashrc:

sudo nano $HOME/.bashrc

Then add the following line to the bottom of the file and save:

PATH=$PATH:$HOME/.config/composer/vendor/bin

You will then be able to execute ‘composer’ from the shell and thus install Laravel.

Node.js

You will need to install the latest version of Node.js in order to use the NPM package manager:

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt install -y nodejs

 

6 Comments

  1. Naveen Niraula , on Aug 22, 2018 at 04:05 Reply

    In step 5:
    after executing sudo update-rc.d php7-fpm defaults

    I get this error:
    update-rc.d: error: unable to read /etc/init.d/php7-fpm

    What might be the cause ?

  2. Shaun , on Sep 3, 2018 at 19:57 Reply

    Could be a permissions or ownership issue? Chmod 777 the file?

    • Naveen Niraula , on Sep 15, 2018 at 13:33 Reply

      Might be but I found an alternative for this method. Just add buster ppa in sources and update and install.

      Cheers.

  3. Sergio , on Sep 28, 2018 at 12:53 Reply

    and any tip on how to make apache use php 7.2 instead of php 7.0???

  4. Koapa , on Oct 14, 2018 at 12:56 Reply

    What do you mean with this comment?
    “Just add buster ppa in sources and update and install.”

    It doesn’t make sense. I was trying to install php7.2.11 according to your manual.
    Same error seams to be also by starting Apache2.

  5. Alexey Pavlov , on Oct 17, 2018 at 13:30 Reply

    sudo a2dismod php7.0
    sudo a2enmod php7.2

Leave a Comment