Installing The Web Server (Apache) +PHP

To be able to control EduBoard from the web, you will need to install and configure the Apache web server & PHP on your Raspberry Pi.

On its own, Apache can serve HTML files over HTTP, and with additional modules can serve dynamic web pages using scripting languages such as PHP.

We will go through the process of installing both at the same time.

Before you begin to install any software it’s best to make sure the package files are up-to-date. Tutorial 3

Install Apache & PHP

Now install Apache and PHP using the following command:

The install might take some time. During installation you will be asked the following:

After this operation, xx.x MB of additional disk space will be used.
Do you want to continue [Y/n]?

Hit “Y” on your keyboard, followed by “Enter”
Now restart the service:

Or

Test the web server

Enter the I.P. address of your Raspberry Pi into your web browser. You should see something like this:

To find the IP address, type:

It Works
The default web page is just a HTML file, located at /var/www/index.html. Navigate to this directory in the terminal and have a look at what’s inside:

Output:
index.html

In order to edit the file, you must gain root permissions via edit the script with sudo, e.g.:sudo nano index.html

Open index.html with editor:

You can change the text in the html code, to display soemething else. Save the file, and refresh the webpage in your browser. If you already closed that page just simply enter the IP address of the Pi to the browser.

You can use the following code example to display the current date in the browser (HTML & JavaScript Code):

Save the file, and refresh the webpage in your browser. If you already closed that page just simply enter the IP address of the Pi to the browser.

 

Test PHP

Navigate to the WWW folder, if you not already there:

The prompt should look like this:
pi@raspberrypi /var/www $

Now move the index.html file to index.php:

Edit the file:

Put some PHP content in it:

Now save and refresh your browser. You should see “hello world”. This is not dynamic but still served by PHP. Try something dynamic:

or show your PHP info:

Thats it. Now you have Apache and PHP installed you can progress to setting up EduBoard control web page.

EduBoard Web Control

Use the following code example to execute Python scripts from PHP.

Replace your existing index.php file with the one above.

Save the file, and refresh the webpage in your browser. If you already closed that page just simply enter the IP address of the Pi to the browser.

It should look like this:

Web_Led

You can click the buttons, but nothing will happen just yet.

Now we will need to create the python scripts, what the PHP will call.

The python scripts called by PHP.

Create the following two python scripts.  red_on.py,  and red_off.py . To do this, refer to Tutorial 4

The python scripts should be within the same folder as the index.php file.

If you list the content of that directory (www), it should appear like this:

pi@raspberrypi /var/www $ ls
index.php red_off.py red_on.py
pi@raspberrypi /var/www $

There is only one more thing to do.

Please follow carefully the following few steps, if you do it wrong you might need to reinstall the Sd-card of your Pi.

You can only perfom the following, as being superuser. You need to use the following command to be “superuser”. Read more about sudo Tutorial 3

Your prompt should look like this:

pi@raspberrypi /var/www $ sudo -i
root@raspberrypi:~#

The command sudo python XYZ will be execute as the apache user. You’ll need to add this user to your sudoers file.

Replace “apache” with your systems apache username (may be “httpd” or “www” on some systems.)

To find out Apache user name, use the following command:

The output should look like this:

apache2 2112 root 3u IPv4 2786 0t0 TCP *:http (LISTEN)
apache2 2142 www-data 3u IPv4 2786 0t0 TCP *:http (LISTEN)
apache2 2147 www-data 3u IPv4 2786 0t0 TCP *:http (LISTEN)
apache2 2148 www-data 3u IPv4 2786 0t0 TCP *:http (LISTEN)
apache2 2149 www-data 3u IPv4 2786 0t0 TCP *:http (LISTEN)
apache2 2150 www-data 3u IPv4 2786 0t0 TCP *:http (LISTEN)
apache2 2366 www-data 3u IPv4 2786 0t0 TCP *:http (LISTEN)
apache2 2367 www-data 3u IPv4 2786 0t0 TCP *:http (LISTEN)

The username was www-data in my case, therfore I used the following command:

 

Now your web controlled EduBoard should be ready for action..:-)

Click on the “Red on” button in your browser, and the LED should lit Red.

 

  Next Tutorial (Coming Soon)