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

In this tutorial you will learn how to install Apache PHP and MySQL on a Raspberry Pi 2 (LAMP stack)

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](Install MySQL Serverme/content/images/2015/08/Screen-Shot-2015-09-09-at-21.30.05.png?ssl=1)

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


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.