Why do we need to set-up a web server?
For this project we will be using our Raspberry Pi 3 web server to serve the interface for monitoring and controlling our Aquaponics set-up.
Below is a screenshot from the old interface I was using when I was running this on a Raspberry Pi 1!
All sensor readings can be displayed via LCD screens or alike but we want to be able to access all the information from anywhere, No? Without a web server we cannot have this degree of control and visual feedback with anywhere near as much ease.
(There are other ways to get this information such as an App on an Android device and we will touch upon that in the future)
This is why we need to set-up a web server!
If we take a look at the old interface you can see it shows us a whole bunch of information and has some buttons to control different parts of the set-up.
Old Interface screenshot breakdown
At the top was information showing how long the Raspberry Pi has been up and running.
All of these sensor readings below the logo were shown in semi-real time, they were all read at the point that the interface was loaded in a browser. Refreshing the page would retake the readings.
We can see there was the ability to turn the grow lights on and off remotely and also links to both a camera pointing at the fish tank and a separate one pointing at the grow bed (the Fish tank camera was not running off a Raspberry Pi)
Finally at the bottom of the interface, Simple links to relevant social media sites and a button for a quick refresh.
What is not shown here is the improvements that were made to include graphing and logging. Each sensor reading had a small link next to it that would bring up a graph showing the details taken from the sensor in visual form and inform the max and min of the readings, see screenshot below. The sensor readings where taken every 15 minutes for the graphing and logging.
So for us to create something even better than this, the first step on the journey after all that initial set-up, is hosting a web server on our Raspberry Pi 3!
Bear in mind none of this will not open your Raspberry Pi 3 up to the internet! We will cover that AFTER we cover some basic security in a future post - but if you want to jump ahead its a case of configuring your router to direct any outside traffic to the correct ports on your Raspberry Pi 3.
LAMP stack
We are going to be installing a LAMP stack for this project, what is a LAMP stack?
Well it stands for Linux, Apache, MySQL and PHP. They are a bunch or stack of programs we are going to install to get our Raspberry Pi 3 running as a fully functional web server.
Linux being the operating system, Apache being the Web server, MySQL being the database program and PHP being the programming language used on the web server.
Do not worry if this all seems a bit daunting, it is not as bad as it sounds. Its just new stuff to learn about! So lets get to it!
If you haven't done a sudo apt-get upgrade recently or expanded your file system since initial installation by using raspi-config followed by choosing Expand file system followed by a reboot then do these now. You can just go ahead without it, but if they need doing then these bits will slow you down.
(I really should of covered those in the set-up section and will add them in the future)
Install time!
SSH in to your Raspberry Pi 3 and in the terminal type
sudo apt-get install apache2 php5 libapache2-mod-php5
You will be asked to confirm you want to use the required amount of disk space, press y and then enter to continue.
The programs will then install. Just wait for a short while here, while what needs to be done gets done.
Important note - If when you try to install, you are shown a message saying cannot authenticate some files you can remedy this by firstly typing sudo apt-get upgrade in the terminal before running the installation command above. I needed to do this, follow it with a reboot and start again from the beginning.
Test Apache
Once this has finished installing, open your browser on your windows machine or Android device connected to the same network as your Raspberry Pi 3 and in the address bar type the IP address of the Raspberry Pi 3. If all went well you will be presented with the webpage below.
This is great! it means Apache installed correctly and is running.
(Personal note: I do not use Edge browser usually and nor would I recommend it, I am writing this blog from within my main browser (Chrome) and I doubt my readers want to see all my open tabs.)
Right back on topic, few more things to install!
Install MySQL
In the terminal on your Raspberry Pi 3 type this
sudo apt-get install mysql-server mysql-client php5-mysql
Again you will be asked if you are happy to use the required disc space press y and enter to continue.
A whole bunch of downloading will happen.
And you will the be asked for your MySQL "root" user password, choose something you will not forget. You will be asked for confirmation or to repeat the password also.
A whole bunch of unpacking and installation will happen.
If you get errors saying you do not have enough space and installation fails then you may need to expand the file system. You need to run the command raspi-config from the command line and choose the option to expand the file system, followed by a reboot. After the reboot, run the installation terminal command again - IE start this section again, I needed to do this.
Once everything has installed reboot your Raspberry Pi 3.
Reboot and Test MySQL
Upon reboot type in to the terminal
mysql -u root -p
You will be asked for a password, enter the password you chose for the root user during installation.
You will now see something looking like the screenshot below, this means MySQL is successfully installed!
If you type showdatabases; followed by pressing enter you will be returned a list of the current databases in your MySQL installation. Press Ctrl+c to quit out of MySQL, we are finished with this part.
Test PHP
Right we are almost there, lets test the PHP installation was a success. Type in to your terminal
sudo nano /var/www/html/phpinfo.php
/var/www/html/ is the location of the website files for your web server on your Raspberry Pi 3.
This will open the text editor nano, paste the text below in to the file and then press Ctrl+x and confirm you want to save the file.
<?php phpinfo(); ?>
Now return to your browser on your other device and type into the address bar the Raspberry Pi 3`s IP address followed by /phpinfo.php
In my case this is 192.168.0.11/phpinfo.php
You will be shown (assuming PHP installed correctly) a lot of information about your Raspberry Pi 3.
That is all for part 1 of Setting up the Raspberry Pi 3 as a Web Server
In part two here we take a look at our web page on our system and change the current default page displayed by our new Raspberry Pi 3 web server to something of our choosing, enable some permissions and install a piece of software for easy transferral of our webpage files from a Windows machine to the Raspberry Pi 3 and more!
Thanks for reading, did you encounter any issues? Was something not clear enough? Let me know in the comments and I can update the post.
Show support for aquaponicpi and like the Facebook page!
Click on the icon below to be taken to aquaponicpi on Facebook!

















No comments:
Post a Comment