[zilla_tabs] [zilla_tab title=”Objective(s)”] * Understand the concept of a web server
* Accessing and running commands on the Pi over SSH
* Editing a file with nano
* Publishing a webpage [/zilla_tab][zilla_tab title=”Pre-requisites”] * Static IP Address
* SSH Access (Pi)
* Terminal access (Mac) [/zilla_tab] [/zilla_tabs]

Serve Me My Webs Already

So what’s a web server? It’s a program dedicated to serving web pages on the internet. You’ll find that the term ‘server’ can apply both to computers and the programs that run on them. In some cases when people refer to their company’s web server, they might be referring to a dedicated computer running nothing but web server software.
In this case, we’re adding one more capability to our Raspberry Pi – the ability to host and serve webpages as a web server, and the software we’re going to install is called Apache. Our web server will sit on your local network and not (yet) be available to the rest of the world via the Internet, but this is a good way to get started.

Sudo What Now?

Over the course of this tutorial you’ll be seeing a lot of the command called ‘sudo’. This may not make a lot of sense to you the first time you see it, but here it is in a nutshell:

And if that still doesn’t make sense  (it may not right now), think about it this way:
Sudo” is like a Jedi Mind Trick for computers [Only computers that run on a Unix-based system. This applies to Linux, Macintosh OS X but not Windows.]. Some of the commands you will give the computer require administrator permissions to run them, otherwise you might potentially and unintentionally cause your computer to do things it shouldn’t. ‘Sudo’ is the equivalent of saying “I’m the superuser here, and I need you to just go ahead and do what I’m telling you.” You can see how it works quite easily by trying some of the following commands without typing the sudo command in front of them.


Apt-get is another command you’re going to be seeing a lot of. We use it to install applications on our Pi. You can think of it like an App Store, except you have to know the name of the app you want to download first, rather than browse through a list. Don’t worry though, in this tutorial you’ll be shown exactly what it is you need to download and why we’re downloading it.
h2″>Before We Get Started

This tutorial is going to take some time. Somewhere between 10-20 minutes depending on your network connection as you’ll be downloading software from the Internet straight on to your Pi. So be prepared to be patient.
Also, you should probably update the existing software on your Pi first before carrying out the rest of the installations. You can do this by typing the following:

sudo apt-get update

This is the equivalent of running Software Update on your Mac or Microsoft Update on Windows, and can also take some time. You can read this command as “Hi there, I’m the superuser here, please do go and get any updates for my pi. Thanks!”

Step 1: SSH

Here’s the command you’re going to run using Terminal on your Mac:

ssh pi@192.168.1.x

(again, where 192.168.1.x will be your own IP address). If you haven’t connected before, you’ll be asked if you want to add your Pi’s address to your list of ‘known hosts’. Go ahead and press Y to continue. You’ll be asked to put in your password – remember that by default, SSH will not show you your password as you type it in.

Step 2: Apache

sudo apt-get install apache2 php5 libapache2-mod-php5
We’re installing three things here, but doing them all at once: apache2, which is the webserver software; php5, which is a scripting language for developing web sites; and  libapache2-mod-php5, a module that allows the PHP language to talk with the Apache webserver. We could write out three commands like this:

sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5

..but putting it all into one command saves us a little bit of time.

Step 3: MySQL

sudo apt-get install mysql-server mysql-client php5-mysql

As with the previous command, we’re rolling three installations into one: mysql-server, which is free database server software that powers an enormous chunk of the web;  mysql-client, again part of MySQL – this allows you to interface with your MySQL database; and php5-mysql, which enables the PHP scripting language we installed in step 2 to talk to MySQL.

Step 4: Restart Apache

sudo apachectl restart

You might see this written differently across the web – ‘sudo service apache2 restart’ – for example, But in essence, both commands are the same. We’re telling the apache web server that it needs to restart once we’ve finished installing all this extra functionality over the top of it. Notice that we don’t need to actually restart the Pi – just the Apache service.

Step 5: Creating phpinfo.php

We could test the web server out right now – in fact, it’s pretty much ready to go if all you want to do is serve HTML files – but before that we’re just going to make sure that our installation of PHP went okay. We’re going to do that by creating a file called phpinfo.php and insert some code in there that should display information about our PHP installation when we connect to the web server.

sudo nano /var/www/phpinfo.php

sudo you know. nano is a text editor, similar to TextEdit on a Mac or NotePad in Windows, only when you run it, it won’t open up in a nice graphical window as nano runs inside the Terminal window.
Instead of a menu system along the top of the screen, you’ll notice that all the commands are at the bottom of the screen and can be used by pressing the Ctrl button (that’s what the ^ means) and the appropriate letter. So ^G brings up the Help menu for example. What you may not see is a ‘Save’ function. In nano you save by exiting first with ^X, then confirm that you want to save your changes by pressing Y. This only works on files that you have permission to edit, which is why we run sudo when we run this command.
The /var/www/ is the path to your web server on the Pi, and where all your files and webpages will live. phpinfo.php is the file that we’re editing. Once of the nice touches about nano is that it actually creates the file for us as we type it in. Prior to typing the above command in, the phpinfo.php file didn’t exist – and now it does!
Enter the following code into your phpinfo.php file:
and then exit your file, confirming that you want to save the changes.
The ” part of the code above is an opening and closing tag, meaning that whatever we put inbeween, we want the computer to do for us. the phpinfo(); is a function that outputs information about the PHP configuration on your web server.

Step 6: Testing the Web Server

Here’s the part where you can test out everything works by opening up a web browser and typing in the IP address of your Pi followed by a / and the phpinfo.php filename:


If everything’s gone correctly, you should see a page like this:

and if you do – congratulations! You’ve just installed and set up your very own web server on your Pi.

Step 7: Creating index.html

While PHP might be a little daunting to get started with, you could still easily have a go at creating standard HTML pages. The first file a web browser will look at when pointed at a web server is (usually) called index.html. You can try this by using Terminal and typing the following:

sudo nano /var/www/index.html

to create your very first file. Where to go from there? Well, that’s up to you. I would recommend having a look at the Webmonkey Tutorials for help on getting some HTML basics down.
Have fun!