The PHP version that comes with Raspbian is the 5.4.45 at the time I’m writing this. Recently I’ve installed OwnCloud 8.2.2, which recommends PHP 5.6 or newer, so, I decided to compile a newer version by myself. Indeed I tried with a couple of newer versions: 5.6.18 and 7.0.3, both with php-fpm support.
After compiling, configuring and running both versions as separated php-fpm sockets, I runned this benchmark script to compare them, and these were the results:
PHP-5-6-18:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
-------------------------------------- | PHP BENCHMARK SCRIPT | -------------------------------------- Start : 2016-02-14 23:25:27 Server : raspberrypi@192.168.1.77 PHP version : 5.6.18 Platform : Linux -------------------------------------- test_math : 14.507 sec. test_stringmanipulation : 15.244 sec. test_loops : 8.554 sec. test_ifelse : 5.554 sec. -------------------------------------- Total time: : 43.859 sec. |
PHP-7.0.3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
-------------------------------------- | PHP BENCHMARK SCRIPT | -------------------------------------- Start : 2016-02-14 23:24:53 Server : raspberrypi@192.168.1.77 PHP version : 7.0.3 Platform : Linux -------------------------------------- test_math : 3.947 sec. test_stringmanipulation : 5.452 sec. test_loops : 4.150 sec. test_ifelse : 2.725 sec. -------------------------------------- Total time: : 16.274 sec. |
It’s awesome, isn’t it? 🙂
So, I’m going to explain how to download, compile and install php-7.0.3 with the memcached extension for the Raspberry Pi. The instructions for php-5.6.18 are analogous, except for the version of memcached extension that you have to compile (below in the memcached section, simply use this source code instead of the php7 branch: https://github.com/php-memcached-dev/php-memcached/releases/tag/2.2.0).
First, download the versions you want from here (in my case were 7.0.3 and 5.6.18): http://php.net/downloads.php
Now, the quick recipe:
Installing dependencies
These were the dependences needed in my case. You may need to install additional packages. Take care of possible errors at compilation time, they use to be quite descriptive about what libraries are needed. If so, “apt-cache search ” is your friend 🙂
1 |
apt-get install libcurl4-openssl-dev libbz2-dev libjpeg-dev libkrb5-dev libmcrypt-dev libxslt1-dev libxslt1.1 libpq-dev git make build-essential |
A workaround to fix a compilation error with the imap support:
1 2 3 |
apt-get install libc-client2007e libc-client2007e-dev mkdir /usr/c-client ln -s /usr/lib/libc-client.a /usr/c-client/ |
If you are in Debian Jessie (Raspbian…) you’ll probably see the “gcc is unable to create an executable file” error when you try to compile anything. Well, that’s because Jessie comes with gcc-4.9, which seems to be broken (at least in my case), son I had to install gcc-4.8:
1 2 |
$ sudo apt-get install gcc-4.8 g++-4.8 $ rm /usr/bin/gcc ; ln -s /usr/bin/gcc-4.8 /usr/bin/gcc |
Compile and install php
Notice the –prefix and –with-config-file-path options below, and just set the directory name with your selected version:
1 |
./configure --prefix=/opt/php-7.0.3 --with-config-file-path=/opt/php-7.0.3/etc --with-zlib-dir --with-freetype-dir --enable-mbstring --with-libxml-dir=/usr --enable-soap --enable-calendar --with-curl --with-mcrypt --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 --enable-gd-native-ttf --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 |
If all was correct, then run the make command (if you have a Raspberry Pi 2, you should add the -j4 param to use all cores for the compilation):
1 |
make -j4 |
It will take several minutes. After the process, run:
1 |
sudo make install |
It will install php-7 under the /opt/php-7.0.3 path, so there is no conflicts with other possible php versions you may have installed.
Configuration
From the directory you’ve just compiled, install the php.ini-production and sapi/fpm/init.d.php-fpm files as following, and move the *.conf.default to their .conf versions at the installation path:
1 2 3 4 5 6 7 8 |
cp php.ini-production /opt/php-7.0.3/etc/php.ini cp sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm update-rc.d php7-fpm defaults cd /opt/php-7.0.3/etc/ mv php-fpm.conf.default php-fpm.conf mv php-fpm.d/www.conf.default php-fpm.d/www.conf |
Then edit the /opt/php-7.0.3/etc/php-fpm.d/www.conf to search and uncomment/set the following options (the last one will be very necessary to configure nginx later on):
1 2 3 4 5 6 7 |
user = www-data group = www-data listen.owner = www-data listen.group = www-data listen = /var/run/php7-fpm.soc |
And that’s it. You can test it by executing “/opt/php-7.0.3/bin/php –version”, and run the php-fpm by executing:
1 |
/etc/init.d/php7-fpm start |
Now to configure nginx to use php7, you should set the fastcgi_pass param to point to the new sock file. For example:
1 2 3 4 5 6 7 8 9 10 11 |
server { ... location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php7-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ... } |
Then, restart nginx and voilà.
Memcached
To download, compile and install the memcached extension, just execute the following:
1 2 3 4 5 6 7 |
git clone https://github.com/php-memcached-dev/php-memcached.git cd php-memcached git checkout php7 /opt/php-7.0.3/bin/phpize ./configure --disable-memcached-sasl make sudo make install |
Then edit the /opt/php-7.0.3/etc/php.ini file, look for the “Dynamic Extensions” section, and add this line to it:
1 |
extension=memcached.so |
For nginx, you’ll have to restart the php7-fpm and the nginx server. But before that you can test it by the command line with this:
1 |
echo "<?php phpinfo() ?>" | strace /opt/php-7.0.3/bin/php 2>&1 | grep memcached |
Among other things, there should be a line like “memcached support => enabled”.
Troubleshooting: If you still not having memcached enabled, go to the php7 source directory and execute again “sudo make install”.
If everything was fine, restart services and that’s all:
1 2 |
/etc/init.d/php7-fpm restart /etc/init.d/nginx restart |