Turn your Raspberry Pi 3 into a PHP 7 powered web server

Important: This tutorial needs updating. As the comments show, lots of people having the same issue. I will update the tutorial this week.

In this tutorial I’m going to go through the steps to install Apache2 and PHP 7.0 on your Raspberry Pi 3, to create the ultimate low-powered, super quick web server.

Prerequisites

I’m going to assume that you have Raspbian installed on your Raspberry Pi 3. For the best results, I recommend running Raspbian from a USB Flash drive rather than a MicroSD card. See my tutorial on how to do this.

Update

These instructions should work fine on the Raspberry Pi 2 as well. Let me know if anyone tries this.

Step 1 – Add new repository sources

At the time of writing, PHP 7.0 hasn’t yet made the official repository sources, so we need to add one which provides us with the PHP 7.0 packages. Edit your sources file with this command:

sudo nano /etc/apt/sources.list

Below the existing entries in the file on a blank line, add the following two lines:

deb http://repozytorium.mati75.eu/raspbian jessie-backports main contrib non-free
#deb-src http://repozytorium.mati75.eu/raspbian jessie-backports main contrib non-free

Your file should look a little like this:

Install Apache 2 and PHP 7 on Raspberry Pi 3

Next we need to add a couple of certificates in order to allow us to use the sources with apt-get. Run the following two commands:

sudo gpg --keyserver pgpkeys.mit.edu --recv-key CCD91D6111A06851
sudo gpg --armor --export CCD91D6111A06851 | sudo apt-key add -

And finally we need to update the package list by running:

sudo apt-get update

Step 2 – Install the core Apache2 and PHP 7 packages

We’re ready to install Apache2 and PHP7, along with some common packages that we may need for most web applications such as Symfony2, WordPress, Drupal etc. Run the following command to install these packages:

apt-get install apache2 php7.0 php7.0-curl php7.0-gd php7.0-imap php7.0-json php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-xmlrpc libapache2-mod-php7.0

This may take a little time, but as soon as we’re done, we are essentially finished.

Step 3 – Test Apache2 and PHP 7

Assuming all went well, we’re ready to test to see if everything went OK with the installation. First we’ll test the version of PHP that CLI is using:

php -v

You should see something like this:

Install PHP 7 on Raspbian

Next we’ll test two things; Apache2 and PHP 7. In your web browser, visit the IP address of your Raspberry Pi. If you haven’t used ssh and have followed these steps on your Raspberry Pi 3 itself, just visit http://127.0.0.1, otherwise change 127.0.0.1 for the IP address of your Raspberry Pi.

You should see the default Debian holding page:

Turn Raspberry Pi into a web server

Next we’ll test PHP. Run the following command to create a test file:

echo "<?php phpinfo();" >> /var/www/html/phpinfo.php

Yet again, visit the IP address of your Raspberry Pi, this time adding /phpinfo.php to the URL. You should see the PHP Information as shown below:

Install PHP 7 on Raspberry Pi

Conclusion

The Raspberry Pi 3 is a very capable, low power consuming little piece of kit. Combine that with the awesome power and speed of PHP 7.0 and you have a low energy web server that’s lightning quick and ideal for blogging from. As mentioned in the prerequisites, I do recommend using a decent USB Flash drive instead of an MicroSD card to run the operating system.

My first experiments with WordPress on PHP 7 on a Raspberry Pi 3 were very promising. I’m probably going to switch back to hosting this site from one again, now that the equipment and software can keep up with the traffic to the site.

Any questions or feedback, feel free to add below. Let me know about your own projects too and I’ll share them.

Ste

Web Developer living in Manchester, working for Studio Skylab (http://www.studioskylab.com). Views and thoughts are my own.

You may also like...

61 Responses

  1. It would be nice to a see a fourth part to your three part tutorial refreshing things.

    Now that we have internal OPCaching and major Zend improvements in PHP 7 it would be nice to see this in conjunction with nginx.

    Thanks =)

    • Ste says:

      I totally agree. I’ll take a look into this and get something written up. I experimented with NGINX but had issues where traffic got to a level and had constant outage without any verbosity which was frustrating. I thing this was more down to my own inexperience with php-fpm with NGINX, but still was frustrating.

      Give me a couple of weeks to break out of my Apache comfort zone and I’ll get a new tutorial written up 🙂

    • Ste says:

      Hi Brian

      I’ll be writing this up over the course of the bank holiday weekend. Keep your eyes peeled 🙂

      Ste

  2. Harry says:

    It works fine on the pi 2 i tried it! Still using the pi 2 for my server since it does not overheat and it uses less power!!

    • Ste says:

      Glad to hear it worked for you. I’m going to write up a tutorial for using PHP7 with Nginx this weekend. Typically, Nginx uses less resources than Apache so should be even better on a Raspberry Pi.

  3. Mat says:

    Thanks for this – really useful!

    I ran into an issue when running the following command:

    “sudo gpg –armor –export CCD91D6111A06851 | apt-key add -”

    I was getting “ERROR: This command can only be used as root”. It took me a bit of trial and error, including trying and failing with “sudo -i”, but I found that each command either side of the pipe needed a separate sudo:

    “sudo gpg –armor –export CCD91D6111A06851 | sudo apt-key add -”

    Thanks again,
    Mat

  4. cbattarel says:

    hello, thanks for the tutorial; works great, except i dont think you can use wordpress without a database server 😉

    • Ste says:

      I’d recommend using a database server such as MySQL on a separate box, the many IO operations MySQL performs quickly causes MicroSD or USB Flash to fail. I use Amazon RDS as the database server, but if you really wanted to use MySQL locally, it’s installed with sudo apt-get install mysql-server.

      I could look to do a tutorial where we have database replication between two Raspberry Pi devices 🙂

      • cbattarel says:

        thanks a lot for the tutorial and this last advice; what do you think about storing the database on a usb external hard disk ?

        • Ste says:

          Yep, that would work fine. Off a decent USB flash drive would be ok, I used to have the db for this site on a Corsair Voyager which was lightning quick, lasted 18 months serving around 10k visitors a month. Another option would be to have a couple of these in a Raid config, not sure which off the top of my head, but would make a nice tutorial. I’m going to see if this can be done

  5. Berk CAN says:

    latest mysql installation please

  6. raffaell says:

    Nice tutorial, I have planned to buy pi3 as well as currently I’m using pi2. Do you have any measurements and optimization tips for high traffic ? i had 1 site that run very poor response time due to the daily traffic more than 2000 hits per day.

    • Ste says:

      Hi. Thanks for the feedback. For performance gains, caching all the way. Opcache works really nicely with PHP 7. I’ll write up a tutorial soon 🙂

  7. Carlo says:

    hi
    very nice guide
    i am writing because i am trying to add teleconference and other small features to a an ILIAS cms, so i end up needing a java and a Big Blue Button server as well,
    thus, since the only spare computer i have at the moment is an old Pentium 4, 2.53 Ghz, and i’ve never had a Raspberry, i’d like to know if and how Raspberry would perform as a not very attended
    java server
    lamp server
    BBB server
    owncloud
    host for a couple of websites

    hope i’ve been clear enough
    cheers

    • Ste says:

      It should work better than an old P4, but I strongly recommend using either a decent class 10 MicroSD card or a decent USB flash drive for the OS. I swear by the Corsair Voyagers.

      • Carlo says:

        thank you for the reply
        i have a class 10 micro sd from the old mobile but i have a brand new flash voyager go i’ve retrieved yesterday: which does sound better to you?

  8. Werner says:

    hi,
    thanks for the tutorial. I installed the package with jessie and everything went as expected. One problem is left and I am not sure if this is the right place to post.
    I try to read a xml-file with simplexml_load_file and this is not working. PHP hangs up – just a white screen. Is there any additional xml-package I have to install? File_exists and Is_readable are ok. The same code runs perfectly under Windows.
    Any sugestions?

  9. John Clayton says:

    My only problem with this was having php5 already enabled!

    sudo a2dismod php5
    sudo a2enmod php7.0
    sudo apache2ctl restart

    fixes that problem.

  10. PaulA says:

    php7.0-mcrypt simply will not install kicks back this error:
    The following packages have unmet dependencies:
    php7.0-mcrypt : Depends: libltdl7 (>= 2.4.6) but 2.4.2-1.11 is to be installed
    E: Unable to correct problems, you have held broken packages.

  11. Tony says:

    Hi, I am having a hard time getting word press running well. I am running word press on a PI 1 B with
    Jessie 4.4 apache2 Mysql-server (not sure which version but at-get update says it is the newest) & php 5. On a 8 gig class 4 SD card. The problem I am having is that on my local net work it is running so slow it is impossible to use. 3 or 4 min to load the home page and when I try to log in it times out on the connection. This is not a graphics problem as it is a brand new install and I have done nothing to it. Outside users tell me it loads ok. What I have tried is two complete re installs from ne sd cards. Two different computers, and chrome & Firefox. It seems to be ok from outside my network, but that is second hand info. The site is Tonyd.org I can tell you the problem starts when I change the word press address ulr & the site address ulr under general and settings. To http://tonyd.org I have been on this for three day now, and have looked at all the PI forms with no luck. I have the same problem when I try to load word press from the pi via the pi’s browser. I have also tried to this on a new Pi 3 Please note I have rolled the web page back to a static HTML page until I can work this out.
    Please?
    Thanks
    P.s.
    this is the doc i used to set up my system. thanks again.
    https://www.raspberrypi.org/learning/lamp-web-server-with-wordpress/

  12. yuki says:

    Simple, clear and well presented article. Perfect. =)

  13. macshack2 says:

    no import of GPG Key possible:

    ERROR:
    gpgkeys: key CCD91D6111A06851 can’t be retrieved

    How may i solve this problem?

  14. Leo Sammarco says:

    Awesome tutorial, was up and running within 5 minutes! The descriptions provided were clear and concise.

  15. Grunto says:

    Good work. Just got my pi and was trying to set up a lamp server. After much frustation, apt errors and PGP fail I came here and it all worked.

  16. Drizz says:

    Easy to follow and understand but like others php7 won’t install on a pi 2.

    The following packages have unmet dependencies:
    php7.0-gd : Depends: libgd3 (>= 2.1.1) but 2.1.0-5+deb8u6 is to be installed
    E: Unable to correct problems, you have held broken packages.

  17. Stiltskin says:

    Had the same problem as Drizz. Fixed by updatin libgd3:
    sudo apt-get -t jessie-backports install libgd3

  18. andig says:

    Same problem with the held broken packages here unfortunately.

  19. Ernst says:

    Try this:
    sudo apt-get -t jessie-backports install libgd3

  20. Try this first before installing PHP7:

    sudo apt-get -t jessie-backports install libgd3

  21. Eric says:

    For those of you with libgd3 dependency errors, this fixes it:

    sudo apt-get -t jessie-backports install libgd3

  22. Zero says:

    I am using Raspbian Jessie Lite on Pi 2 and it gave me:

    The following packages have unmet dependencies:
    php7.0-gd : Depends: libwebp5

    So I tried sudo apt-get install libwebp5
    It still gave me depends: libwebp5

    Then I tried:
    sudo apt-get -t jessie-backports install libwebp5

    That worked and now it shows default Debian page.

  23. Mat Griffin says:

    The following packages have unmet dependencies:
    php7.0-gd : Depends: libwebp5 (>= 0.4.3) but 0.4.1-1.2 is to be installed

  24. Mat Griffin says:

    This has also broke phpmyadmin, which now fails to load

  25. Jack says:

    Hi,

    i’m trying to install PHP on my rPi3 but I have always this problem:
    Unable to locate package!!!

    pi@raspberrypi:~$ sudo apt-get update
    Hit http://repo.mosquitto.org jessie InRelease
    Hit http://mirrordirector.raspbian.org jessie InRelease
    Hit http://archive.raspberrypi.org jessie InRelease
    Hit http://mirrordirector.raspbian.org jessie/main armhf Packages
    Hit http://repo.mosquitto.org jessie/main armhf Packages
    Hit http://archive.raspberrypi.org jessie/main armhf Packages
    Hit http://mirrordirector.raspbian.org jessie/contrib armhf Packages
    Hit https://deb.nodesource.com jessie InRelease
    Hit http://archive.raspberrypi.org jessie/ui armhf Packages
    Hit http://mirrordirector.raspbian.org jessie/non-free armhf Packages
    Hit http://mirrordirector.raspbian.org jessie/rpi armhf Packages
    Hit https://deb.nodesource.com jessie/main Sources
    Ign http://repo.mosquitto.org jessie/main Translation-en_GB
    Ign http://repo.mosquitto.org jessie/main Translation-en
    Hit https://deb.nodesource.com jessie/main armhf Packages
    Get:1 https://deb.nodesource.com jessie/main Translation-en_GB [162 B]
    Get:2 https://deb.nodesource.com jessie/main Translation-en [162 B]
    Get:3 https://deb.nodesource.com jessie/main Translation-en_GB [162 B]
    Get:4 https://deb.nodesource.com jessie/main Translation-en [162 B]
    Ign http://archive.raspberrypi.org jessie/main Translation-en_GB
    Ign http://archive.raspberrypi.org jessie/main Translation-en
    Ign http://archive.raspberrypi.org jessie/ui Translation-en_GB
    Ign http://archive.raspberrypi.org jessie/ui Translation-en
    Get:5 https://deb.nodesource.com jessie/main Translation-en_GB [162 B]
    Get:6 https://deb.nodesource.com jessie/main Translation-en [162 B]
    Get:7 https://deb.nodesource.com jessie/main Translation-en_GB [162 B]
    Get:8 https://deb.nodesource.com jessie/main Translation-en [162 B]
    Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en_GB
    Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en
    Get:9 https://deb.nodesource.com jessie/main Translation-en_GB [162 B]
    Ign https://deb.nodesource.com jessie/main Translation-en_GB
    Ign http://mirrordirector.raspbian.org jessie/main Translation-en_GB
    Get:10 https://deb.nodesource.com jessie/main Translation-en [162 B]
    Ign https://deb.nodesource.com jessie/main Translation-en
    Ign http://mirrordirector.raspbian.org jessie/main Translation-en
    Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en_GB
    Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en
    Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en_GB
    Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en
    Reading package lists… Done

    pi@raspberrypi:~$ sudo apt-get install php7.0 php7.0-opcache
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package php7.0
    E: Couldn’t find any package by regex ‘php7.0’
    E: Unable to locate package php7.0-opcache
    E: Couldn’t find any package by regex ‘php7.0-opcache’

    pi@raspberrypi:~$ apt-cache show
    E: No packages found

    Could you help me?

  26. Marco Zanirato says:

    Hello, I have problems with the key: http://pgpkeys.mit.edu/pks/lookup?search=11A06851&op=index

    Is there an alternative way?

    Thanks

  27. eric says:

    Hello,

    thanks for this procedure, that i followed step by step on my rasberry pi3. But when i come to my page nextcloud i have the following messages concerning missing modules : “php module zip is not installed, please ask …” for modules “dom”, “XMLWriter”,XMLReader”, “libxml”, “mb multybyte”, “simplexml”…
    An finally “php modules have been installed but they are still missing? please ask your server administrator to restart the web server”. i also restarted the pi and webserver. But nothing changed.
    Any Idea?
    have a good day,

    Eric

  28. Richard Burke says:

    The only issue I currently have is allowing people to view my site from outside. I can see WordPress site internally but cannot access it out of the network, and yes I have opened 80 and 443, what can I do!! anyone help I’m ripping my hair out 🙁

    • Ste says:

      Hi Richard, have you also forwarded port 80 and 443 traffic on your router to the Raspberry Pi’s IP address on your network?

      • Richard Burke says:

        Yes port forwarded on TCP for them both, could my ISP be blocking this? I was able at one stage to get the test php file visible from outside. Since I posted I tried at my girlfriends homr with a new load of WordPress and it works to a point. I cannot see images externally, internally is fine

  29. Tyler says:

    Other than switching from php55 to php70 in this tutorial, great help!

  30. Blake B says:

    Whenever I reach “Step 2 – Install the core Apache2 and PHP 7 packages” I receive the following errors. Any idea what the problem is?

    E: Could not open lock file /var/lib/dpkg/lock – open (13: Permission denied)
    E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

    Thanks!

    • Blake B says:

      I found my problem – wasn’t using the sudo command properly. I ran as a root shell and it worked fine, probably would have worked had I just used sudo correctly. Thanks!

  31. silvanet says:

    Hey, I am getting error running the php testing command:
    echo “> /var/www/html/phpinfo.php
    I get
    bash: /var/www/html/phpinfo.php: Permission denied
    I am in root doing this. Even if I prefix with sudo I get the error.
    Any help with that? I read the comments and did not see anyone having that problem.

    Every other step worked perfectly up until then.

  32. Rob Beguelin says:

    Silvanet…if you enter sudo chown -R pi/var/www and then echo “> /var/www/html/phpinfo.php It works!

  33. prospectre says:

    Hi Ste.

    I saw the red box over this tutorial about getting it updated.

    Did you look into it? Does it work on a RPi 3 now?

    Regards.

  34. N4RFC says:

    I had the Permission denied error also. Opening the permissions up on the /var/www/html directory fixed the problem:

    sudo chmod a+rwx /var/www/html

  1. 23rd April 2016

    […] this article following a number of requests, and a bit of a follow-up to a previous tutorial “Turn your Raspberry Pi 3 into a PHP 7 powered web server“. This tutorial will guide you through steps to install MySQL Server on your Raspberry […]

  2. 23rd June 2016
  3. 5th January 2017

Leave a Reply