Tutorial – Install Apache, PHP and MySQL on a Raspberry Pi 2

I’ve been running LAMP setups on Raspberry Pi’s for years, so am excited to use the little pocket-sized powerhouse that is the Raspberry Pi 2 as a web server. This is the first in a series that will see a quad core Raspberry Pi 2 run this WordPress site.

Before we start

There are some prerequisites; I assume you know your way around terminal, and know how to SSH onto your Raspberry Pi 2. These instructions will work directly on the Pi itself. I also assume you know the IP address of your Raspberry Pi.

1. Let’s start

First, connect to your Raspberry Pi 2. Skip this step if you’re working directly on your Raspi:

ssh pi@raspberrypi

Replace ‘raspberrypi’ with the IP or hostname of your raspberry Pi. You should now be logged into your Raspberry Pi 2, and will see something like this:

Raspberry Pi 2

There’s likely to be updates to install, so we’ll go ahead and do these now before we start installing anything:

sudo apt-get update && sudo apt-get upgrade

This used to take a little time on the old ones, but the new quad-core makes light work of the updates. You’ll be prompted to choose ‘Y’ or ‘n’, so just keep hitting ‘y’ until the updates are installed. Providing the updates went in OK, you’re ready to install Apache 2.4 and PHP 5.4

2. Install Apache

Next run the following command. This will install Apache 2.4, which is the web server that responds to http (and https if you like) requests:

sudo apt-get install apache2 apache2-utils

Technically, this is all we need to do to run a very basic web server from our Raspberry Pi. Give it a go, pop the IP address of your Raspberry Pi in your web browser. You should see something like this:

Apache2 running on a Raspberry Pi 2

This is all fine and dandy, but our aim is to install WordPress, so we’re going to need PHP. Follow the next steps to do this.

3. Install PHP

This command will install the PHP 5 and the PHP libraries you’ll need for WordPress:

sudo apt-get install libapache2-mod-php5 php5 php-pear php5-xcache php5-mysql php5-curl php5-gd

That’s it, PHP 5 is now installed. We can test it to make sure it’s working by creating a index.php file and calling PHP Info. Change the directory to the default document root:

cd /var/www/

The permissions aren’t set correctly just yet, so for the time being, we’ll create a index file as sudo:

sudo echo "<?php phpinfo(); ?>" | sudo tee index.php

Now visit index.php in your web browser, you will see PHP Info. You should see the following:

PHP and Apache on a Raspberry Pi 2

4. Install MySQL

MySQL is the database server which will hold our data for our WordPress installation. Installing MySQL is very straight forward. Run the following command:

sudo apt-get install mysql-server

Once the installation begins, you will be asked to provide a master password for your MySQL installation. Ensure you choose a good secure password, and it’s a good idea to give MySQL a different password to the one you use to access your Raspberry Pi.

Install MySQL Server

Finish off by installing MySQL client, which will allow us to create a user for our WordPress installation in the next tutorial:

sudo apt-get install mysql-client

Conclusion

So, we’ve installed Apache and PHP, along with other packages we need to run WordPress. We’ve also installed our database server MySQL, so next we’ll configure the permissions and install WordPress. Come back soon for the next tutorial.

 

 

Ste

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

You may also like...

28 Responses

  1. Aaron says:

    Have you seen a vast improvement with the Pi 2 vs the Model B’s? You site seems to load really quickly now. (I don’t know if you are clustering though.) I originally saw your site when it was first posted on a single Pi and it was a little slow. I am actually looking for the “stress” test info you ran on that. I haven’t been able to find it yet on your site. Are you going to do a similar test with the Pi 2?

    Thanks, looking forward to more tutorials!

  2. kumokumori says:

    I like your site and there are helpful parts of your tutorials, but the thing that I find confusing is that you go into how to configure wordpress for nginx in earlier tutorials and now you talk of using apache2 and leave out migration details or even initial setup information. Which of the two engines do you prefer and why?

  3. alkahn says:

    Why Apache over nginx? Just curious after following your instructions to set up my Raspberry Pi 2 WordPress Server.

  4. Norway says:

    This website is awesome!!’

  5. GS says:

    Okay, its been a few months, when are you coming back with the config and word press install

  6. Laios C. says:

    Thx for this clear tuto…
    The default document root on the new apache2 package has been changed to /var/www/html/

  7. orsenblog says:

    I was unable to install the mysql server, it worked the first time, but then the gateway closed.

  8. Virginie V says:

    Hi, thanks for this awsome Tutorial. I managed to make it a webserver and to have an internet ip address. Could you please tell us now how to point this ip to a name ? it would be much appreciated.
    Thanks,
    Virginie

    ps : for those like me who tried to install the webserver thru SSH it worked well until phpmyadmin. I had a mirror server error or some kind. Anyway, you should run it from the raspberry and not SSH and it should work just fine. Took me a while to figure that out…

  9. Virginie V says:

    Could you please remove my last name in my last message ? It’s a mistake… I don’t want to be tracked. cheers,
    V

  10. Ran into an issue when checking if PHP was correctly installed.
    Seems index.php should now be placed at /var/www/html rather than just /var/www.

  11. disolved says:

    Has anybody managed to get server side includes working on Apache running under Raspian. I have rsearched this extensively but cannot see why it does not work. Are SSI disabled for some reason?

  12. Ben says:

    I had a problem with a mirrors address not found when I initially tried: sudo apt-get install apache2 -y . I tried to find a mirror that worked and add it to my sources.list. Now when I type: sudo apt-get update && sudo apt-get upgrade, I get this message:

    sudo apt-get update && sudo apt-get upgrade
    E: Type ‘[http://raspbian.mirrors.wvstateu.edu/raspbian]’ is not known on line 4 in source list /etc/apt/sources.list
    E: The list of sources could not be read.

    How do I fix this?

  13. toohoo says:

    Hello everybody,
    I read “this is for WordPress – so it is with PHP”. That’s why it is only LAMP and not LAMPP. If I would rather only use Perl, this seems to be preinstalled on the raspi already. Is there anything else to do? Is this method of some steps better in your eyes than – let’s say – a complete “package” lampp and installing it via:
    apt-get install lampp
    ?
    Thanks in advance

  14. Excellent tutorial.
    Sadly I still have the original Raspberry Pi and I installed LAMP on it but boy was it slow !
    Realised after I bought the wrong Pi.

    Definitely wouldn’t run anything intensive like a webserver on that although the new Pi’s seem a lot better & certainly faster.

    Had such high hopes for the Pi when I bought it ( had wanted one for ages ) as was going to be building a data logger & digital dashboard for a car, although with it struggling with a webserver which doesnt require constant communication I put that project on hold

  15. mbs says:

    Regarding step 3 – for me it was necessary to move the index.php file into the /var/www/html folder to make it work…

  1. 14th August 2015

    […] Web applications developer Ste W from Wales is currently blogging about setting up a LAMP stack on your Raspberry Pi in order to host a WordPress blog. You can read the first part of his tutorial, which covers installing Apache and PHP here. […]

  2. 15th September 2015
  3. 30th October 2015

    […] install PHP and Apache on my Raspberry Pi, referring to the tutorial Install Aphache, PHP on a Raspberry Pi by Ste […]

  4. 30th October 2015

    […] installed PHP and Apache on my Raspberry Pi, referring to the tutorial Install Aphache, PHP on a Raspberry Pi by Ste […]

  5. 3rd January 2016

    […] won’t go over all the steps needed to get everything up and running but I’ll leave some write ups that I found helpful. I setup a separate 2.4 GHz network for the nodes and made sure the db was […]

  6. 22nd February 2016

    […] Installing APACHE 2, PHP and MYSQL for Raspberry Pi 2 […]

  7. 20th April 2016

    […] Ich habe diese Anleitung verwendet, um einen Apache Webserver und PHP zu installieren. Ich beschreibe die Schritte hier noch kurz auf Deutsch: […]

Leave a Reply