Installing LAMP On Your Pi

By | August 11, 2013

LAMP is an acronym for Linux Apache MySQL PHP.  Some people may say the P stands for Perl or Python, but we’re going to stick with PHP for now as it is the most popular back-end scripting language.

Linux is the base operating system on the Raspberry Pi, and by now we should have that installation taken care of.  The operating system tells the Pi how to interact with it’s other hardware.  If you still haven’t installed your operating system on an SD card for the Pi, take a look here to learn how to Install Raspian on an SD Card.

Apache is a popular open source (meaning free) web server that can run on multiple operating systems like Linux, Windows, or Mac.  This is the software that fetches our documents to display to a client when requested from a web browser.  Apache is a very popular web server and runs well on the Pi under a minimal load.  I use it because I know it well, however some may argue to use NginxLighttpd, or some other server since the Pi isn’t built to be a heavy duty server.  For the demonstrations on this site, I find it to be adequate.

MySQL is the database server that we will install on our Pi.  There are many variations again for the database server we select, however the important thing to note is that we are using a SQL server.  Structured Query Language is a language we can use to interact with our database server to manipulate a database.  This would be used for things like a blog, a user base, a store’s inventory . . .  basically anything we can interact with, manipulate, add to, or delete.

PHP was one of the first back-end languages created for server side scripting. It’s job is to execute a script on the fly to grab data from the user or another web interface and create a dynamic web page. And as I mentioned before, we have other variants like Perl or Python that we could use instead.  For the sake of some earlier lessons on this site, I want to start with PHP.
Install The Apache Web Server

As a general rule, I always like to update my repositories in Linux before I download any software. In order to do this, let’s go into the Terminal in Raspbian.

pi@raspberrypi ~ $ sudo apt-get update

Now that we have the repositories updated, let’s download the latest version of the Apache web server.

>pi@raspberrypi ~ $ sudo apt-get install apache2

It Works!

This builds a list of all the required packages to be downloaded. Type “y” and hit enter when it prompts you, and just wait for the software to install. You can see in the terminal as all of the software is being downloaded, and once you are returned to your prompt we can check to make sure everything was handled correctly. There is no window or open application to see the server at, it’s a background process that is running. So to see it in action, let’s open the Midori web browser from the Raspberry Pi. In the URL bar, just type in “http://localhost”, and see what you find.

The localhost reference is an obvious nickname referencing our own Raspberry Pi.  It actually calls our apache server, and that server located a web document on our Pi and sends it to the browser. So somewhere stored on our SD Card is the HTML document we see in our browser.  We want to find where this document is, so we can change the web page. By default, the apache web server stores it’s web page in a location at “/var/www”.  We can find this in the File Manager, and open the source in Leafpad.edithtml

The only problem is, we cannot edit the file and save it.  This has to do with the Linux file system, and how it uses file/folder permissions.  We can see from looking at the ownership, the file and folder are index.html file is in belongs to user “root”. We are signed in as user “pi”, so we can temporarily act as “root” to give ownership of the folder to user “pi”. From the terminal, we can issue a command that will change the ownership of the www folder and it’s files.

pi@raspberrypi ~ $ sudo chown pi -R /var/www

No we have pi as owner of the www folder and files in it. This allows us to play around now with the HTML source, and edit the web pages. Inside Leafpad, I can now edit the index.html file and save it. Then when I reload the web page, my changes are noticed.

icannowedit

Install MySQL Server

Now that we can get our web server to load up a web page we can manipulate, let’s take a quick moment to set up our database server. I’m going to talk about MySQL in another tutorial, but we can just get it ready now. Back to the terminal, let’s get MySQL server up and running. I will point out that this thing is a resource hog, but it is a great educational tool.

pi@raspberrypi ~ $ sudo apt-get install mysql-server

MySQL
Hit “y” again at the prompt to have the installation continue. Like everything else with Linux, the databases have owners, and be default a root owner comes with the package. Select a password for this user when prompted to do so, and just remember it for later.

There are several ways to create, edit, and view databases.  One interesting way to do it is with our web browser.  We will look at that in it’s own tutorial. To make this possible though, we’re going to need a back-end web scripting language like PHP.

Install PHP

While still in the terminal, let’s get the PHP server up and running. We need to install two packages for this so we can use the web scripting language and have it interact with our database server.

pi@raspberrypi ~ $ sudo apt-get install php5 php5-mysql

Again hit ‘Y’ at the continue prompt, and let the packages install themselves. We can test out our PHP server now by going back in to Leafpad and editing the index.html file. Let’s make some changes to the code, so that our server can hand off some of the script for the PHP to be executed. We also have to remember to rename our file index.php. This is how Apache knows how to handle the new code between the <?php ?> tags.

<html>
<h1>It works! I CAN NOW WRITE PHP</h1>
<?php
phpinfo();
?>
</html>

PHP Works

Now that we have everything installed, we can now create web applications on the Raspberry Pi.  Fortunately, all of the programs we just installed will automatically start when the Pi boots up.

Leave a Reply