How To Compile PHP7 From Source on Raspbian Jessie
This tutorial explains how to compile PHP7 from source and installing it to Raspbian Jessie OS. I’m using the low end Pi 1 Model B. The processing power of Pi 1 is inadequate when it comes to compiling software from source. It took about 5 hours to finish compiling PHP7. So I suggest using Pi 2 or Pi3 if you have one. Pi 2 has dual-core CPU and Pi 3 has quad-core CPU.
Compile PHP7 From Source
First SSH into your Raspberry Pi if you have a headless setup. Then download the PHP7 source code from official website.
wget http://php.net/distributions/php-7.0.4.tar.gz
Extract the tarball.
tar xvf php-7.0.4.tar.gz
cd to the newly-created directory.
cd php-7.0.4/
Install dependencies.
sudo apt-get install libkrb5-devlibc-client2007e libc-client2007e-dev libcurl4-openssl-dev libbz2-dev libjpeg-dev libmcrypt-dev libxslt1-dev libxslt1.1 libpq-dev build-essential git make
As you may already know, we must run a configure script to configure the compiling environment. But before that, I’d like to point out two errors I encountered during the configure phase and how to solve them.
Error #1
configure: error: Cannot find imap library (libc-client.a). Please check your c-client installation.
Solution: create a symbolic link and re-run the configure script.
sudo mkdir /usr/c-client/ sudo ln -s /usr/lib/libc-client.a /usr/c-client/
Error #2
error: Kerberos libraries not found. Check the path given to --with-kerberos (if no path is given, searches in /usr/kerberos, /usr/local and /usr )
Solution: create a symbolic link and re-run the configure script.
sudo mkdir /usr/kerberos sudo ln -s /usr/lib/arm-linux-gnueabihf/mit-krb5/* /usr/kerberos
If you want to see the error for yourself, you can now run the below configure script. If you don’t like seeing errors, then create the above symbolic links and run the below configure script.
Here’s the common arguments for the configure script. Copy the following code and paste it in the Raspbian terminal, then hit Enter to start the configuration.
./configure \ --prefix=/opt/php-7.0.4 \ --with-config-file-path=/opt/php-7.0.4/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=/usr/lib/arm-linux-gnueabihf \ --enable-ftp \ --with-imap \ --with-imap-ssl \ --with-kerberos \ --with-gettext \ --with-xmlrpc \ --with-xsl \ --enable-opcache \ --enable-fpm
When it’s done, let’s compile PHP7 with make command. use -j4
option if your Raspberry Pi has a quadcore processor.
make or make -j4
On my low end single-core Raspberry Pi, this compilation process took about 5 hours to finish. A Pi 3 with quad-core processor takes much less time.
Once the compilation is completed, install PHP7.0.4 with the following command:
sudo make install
Upon finishing, it tells you where things are installed. This is very useful information.
As you can see, the PHP-FPM binary is located at /opt/php-7.0.4/sbin/
and my PHP-FPM config dirctory is /opt/php-7.0.4/etc/
. So if I start php-fpm with this command:
/opt/php-7.0.4/sbin/php-fpm
An error message spits out telling me php-fpm.conf is not found.
We need to rename two conf files.
sudo mv /opt/php-7.0.4/etc/php-fpm.conf.default /opt/php-7.0.4/etc/php-fpm.conf sudo mv /opt/php-7.0.4/etc/php-fpm.d/www.conf.default /opt/php-7.0.4/etc/php-fpm.d/www.conf
And copy the php.ini-production file to the php-fpm config folder.
sudo cp /home/pi/php-7.0.4/php.ini-production /opt/php-7.0.4/etc/
Now start php-fpm again.
sudo /opt/php-7.0.4/sbin/php-fpm
Integrate with Nginx
To see how to compile Nginx from source on Raspbian Jessie, check out the following post. Of course, you can also install nginx on Raspbian Jessie from software repository with sudo apt-get install nginx
.
How To Compile Nginx From Source and Install on Raspbian Jessie
In your Nginx server block, add these lines.
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
The document root directory on my Raspbian Jessie is /usr/share/nginx/html
. You may need to change the document root directory. Save and close the file. Reload Nginx.
sudo sysemctl reload nginx
Then create a test.php file in your document root directory.
sudo nano /usr/share/nginx/html/test.php
Paste the following PHP code into the file.
<?php phpinfo(); ?>
Save and close the file. Now in your browser address bar, type
ip-of-raspberrypi/test.php
Replace ip-of-raspberrypi with the actual IP. You should see all your PHP installation information. If you can see this page, that means php-fpm is running happily with Nginx.
Fine Tuning
Enable Error Log
Edit php-fpm.conf
file
sudo nano /opt/php-7.0.4/etc/php-fpm.conf
Find the following line in this file.
;error_log = log/php-fpm.log
and change it to this:
error_log = /var/log/php-fpm.log
So now we can check php-fpm log in /var/log/php-fpm.log
file.
Integrate with Systemd
Create a systemd service file for php-fpm.
sudo nano /etc/systemd/system/php-fpm.service
Copy and past the following text into the file.
[Unit] Description=The PHP FastCGI Process Manager After=syslog.target network.target [Service] Type=simple PIDFile=/var/run/php-fpm.pid ExecStart=/opt/php-7.0.4/sbin/php-fpm --nodaemonize --fpm-config /opt/php-7.0.4/etc/php-fpm.conf ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target
Save and close the file. Now kill the current running php-fpm process and start it again with systemctl.
sudo pkill php-fpm sudo systemctl start php-fpm
Check status
sudo systemctl status php-fpm
Enable php-fpm automatically start when Raspberry Pi is powered on.
sudo systemctl enable php-fpm
Edit www.conf File
sudo nano /opt/php-7.0.4/etc/php-fpm.d/www.conf
Find the following two lines
;listen.owner = www-data ;listen.group = www-data ;listen.mode = 0660
Uncomment them. Then find this line.
listen = 127.0.0.1:9000
Change it to
listen = /var/run/php7-fpm.sock
Save the file. Now edit your Nginx configuration file and change the value of fastcgi_pass
to /var/run/php7-fpm.sock
location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass unix:/var/run/php7-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
Save and close the file. Then restart Nginx and php-fpm.
sudo systemctl restart nginx sudo systemclt restart php-fpm
Congrats! You have successfully compiled PHP7 and installed it on Raspbian Jessie. Also we created a php-fpm.service file and can easily manage php-fpm with Systemd.