Friday 23 August 2013

Running a web server on a Raspberry Pi

I've been quite frustrated in my attempts to get anything connected to my Raspberry Pi, mainly because it usually involves using a programming language that I only know the basics of, or some obscure application library that isn't very well documented.
As always, back to basics! I've decided to install a web server with PHP and MYSQL, then take it from there - these are tools that I am very proficient in and it will removes the unknowns from the equation. As I expected, this is very easy but I will take it slowly and assume that you are inexperienced. Please feel free to ask questions, point out errors or just generally comment.
Apache is a very popular web server that will let your Raspberry Pi run as a web server which means you will be able to access web pages that have been written and are stored on the Pi.
The web pages are usually written in HTML, but you can add a lot of functionality to the web server by adding another popular programming language called PHP.
Its also good to be able to use a database to store information about any systems that you create on the server.

Step 1 Install Raspbian on your Raspberry Pi.

I arent going to document this as there are hundreds of guides and I will only echo what has already been said. For the record, I used NOOBS (New out of the box software) to install it, then allowed SSH access and used MobaXTerm to access the graphical interface. (This isnt important, you can just as easily do this straight from a normal monitor view)

Step 2 Install Apache, PHP and MYSQL

On the desktop of Raspbian you will see an icon saying "LX Terminal". Click on that and then type this:

sudo apt-get update

The command "sudo" means "run this instruction as an administrator," which in linux is a user called "root".
The apt-get update part tells Raspbian to make sure that all its records of available software are up to date. You will see it trundle off and check lots of things, just leave it alone until you see a coloured command prompt starting "Pi@..."
The next command to type is:

sudo apt-get install apache2 php5 mysql-server

This instructs Raspbian to install the web server (Apache2), PHP version 5 and mysql-server. This will take five minutes or more, its fascinating to read the things that flash by on your screen, all very clever but once you have seen it a couple of dozen times, the delay is just an inconvenience!
You will be asked to set a password for mysql. Make this a secure, complex password and also make sure that you remember it!

Step 3 - Install phpmyadmin

You will now have a web server running (we will check it later) and we could start to add web pages. I've always used a great utility called phpmyadmin which lets you look after any databases running on Apache very easily.
Type this command:

sudo apt-get install phpmyadmin

Again, this takes a while. You will be asked for the MySQL password (the one you specified in step 2) and also for another password which you will use to log on to phpmyadmin once it is installed.

I usually install a GUI interface that is great for controlling a web server, its called webmin. This guide will tell you how to do it, but it isn't essential by any means!

http://www.raspberrypi.org/phpBB3/viewtopic.php?f=63&t=6096

Step 4 - check it all works.

Type this command:

ifconfig

Into the terminal window, and Raspbian will tell you lots of information about how it connects to the network.
Look for  a line that starts "inet addr", followed by an IP address such as 192.168.1.210
This is the IP address of your Raspberry Pi. On any other computer on your network, open a browser and type this IP address into the main URL box. You should see a page that says "It works" - well done!
You can also use "http://192.168.1.200/phpmyadmin" to open the phpmyadmin window. It will ask you for the password that you entered in Step3 (nb, not step 2!)

Any web pages for your web server are stored on your raspberry Pi at /var/www
You should see a file there already called "index,html"

If you have come this far, well done! You will now need to work out how you can edit pages on your server and make it do something useful - watch our for future updates when I will be detailing various methods.

No comments:

Post a Comment