Apache Webserver

Why not to install your own Home Webserver on your Raspberry Pi ?

Refer also to Ubuntu nginx Webserver on a Android Tablet

- Understand the concept of a web server for the Raspberry Pi APRS igate:
It is possible to remotely access the Raspberry Pi which can be done via SSH, FTP, and also you can run a web server on it.
For eg. If you write a PHP code, then you just need to save it with actual library files in a location which is accessible to your web server (normally under /var/www/ ) and then use your remote web browser to access this page and run the code remotely on the Pi and get the result on your local machine.
In case you want to remotely monitor or do telemetry, a web server is usually needed.

I am trying to make a remote DC current measurement with the Raspberry Pi connected to an USB Amp-meter.
This remote Amp-meter will be used to measure the charging current of a battery set powered by a remote solar panel.
I will use the Apache web server running in the Pi, to run the PHP code and serve the http requests for the DC current measurement values.
Basically I will be able to see the DC charging current values of the remote solar system from 1000`s of kms away..
Such remote Amp-meter device would costs hundreds of EURs, but can be build this ways for a portion of that cost.
- Accessing and running commands on the Pi over SSH
- Editing a file with nano
- Publishing a webpage

Static IP address, SSH Access (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.

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.

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

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; andlibapache2-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. nano runs inside the Terminal window and looks like this:


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:

<?php phpinfo(); ?>

and then exit your file, confirming that you want to save the changes.
The ‘<?php ?>’ 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.