- Raspberry Pi Home Server
- Raspberry Pi Web Server Apache Download
- Raspberry Pi Apache2
- Raspberry Pi Web Server Apache Download
In this tutorial we install Apache web server in Raspberry Pi to control the LED from a webpage that can be accessed from anywhere over the internet. This is a basic tutorial with minimum features and it can be further modified to use this method in IoT based home automation, remote control automation, robotics etc.
To install a full web server on Raspberry Pi (LAMP), three packages are required: Apache, PHP and MySQL. Apache answer HTTP requests, PHP generate dynamic content and MySQL store and read information in a database. Restart Apache sudo /etc/init.d/apache2 restart If you type your Raspberry Pi IP address into the web browser on a machine connected to the network you should see the apache default web page appear. Adding Your HTML, PHP etc Files.
Here we control an LED, connected to Raspberry Pi by using Apache web server. For this we create an html/php web page which has two buttons - one for turning on the LED and second for turning off the LED.
Components Required
- Raspberry pi board (With Raspbian operating system)
- LED
- 250 ohm resistor
- Jumper Wires
A SSH client (Putty) is used to connect the Raspberry pi using Laptop or computer. For this the raspberry pi needs be connected to network via LAN or Wi-Fi. If you have a separate monitor for your raspberry pi, then it's better to connect raspberry pi with monitor and you don’t have to use any SSH client.
Controlling LED using Raspberry Pi Webserver
Step 1 Connections
The connections in this project are quite simple - positive pin of LED is connected to GPIO 27 pin and the negative to a 270 ohm resistor, the other side of which is connected to GND pin.
Step 2: Installing WiringPi Library
WiringPi is a PIN based GPIO access library written in C for the BCM2835, BCM2836 and BCM2837 SoC devices used in all Raspberry Pi versions. It’s released under the GNU LGPLv3 license and is usable from C, C++ and RTB (BASIC) as well as many other languages with suitable wrappers.
1. First we will update our Pi with latest versions of Raspbian using the command:
2. Now we will install git by using this command:
3. Now obtain WiringPi using git by this command:
4. Then install WiringPi library using:
Step 3: Installing a Web Server
Apache is a very popular webserver, designed to create web servers that have the ability to host one or more HTTP-based websites. Apache Web Server can be enhanced by manipulating the code base or adding multiple extensions/add-ons. In our project we are using an HTTP server and it's PHP extension.
To Install Apache web server we will use following commands:
First, update the available packages :
Mac os x 10.5 download for windows 7. Now, install the apache2 package by using this command command in terminal:
To test the web server whether it is working or not, go to your browser and type the Pi’s IP address in the tab. To find the Pi's IP address, type ifconfig at the command line.
By default, Apache puts a test HTML file in the web folder. This default web page is served when you browse to http://192.168.1.31 (whatever the Pi's IP address is) from another computer on the network.
Browse to the default web page either on the Pi or from another computer on the network and you will see the following:
This means Apache web server is working.
Now we will see how to Change the default web page with your own HTML page
This default web page is just an HTML file on the filesystem. It is located at var/www/html/index.html.
Navigate to this directory in a terminal window and have a look at what's inside:
This shows that by default there is one file in /var/www/html/ called index.html and it is owned by the root user . To edit the file, you need to change its ownership to your own username. Change the owner of the file using:
You can now try editing this file and then refresh the browser to see the web page change.
PHP Installation in Raspberry pi
Now if we want to use PHP code along with HTML then we have to further install the PHP extension in Raspberry pi. Using PHP code we can create shell commands to control the LED from PHP script.
To allow Apache server to edit PHP files, we will install the latest version of PHP and the PHP module for Apache. Use the following command in terminal to install these:
Now remove the default index.html file:
And create the your own index.php file:
Now enter the below code in index.php to test the PHP installation.
Raspberry Pi Home Server
Save it by pressing CTRL + X and the ‘y’ and enter. Now refresh the webpage in your browser you will see a long page with lots of information about PHP. This shows that the PHP extension is installed properly. If you have any problem to the pages or if the pages do not appear try reinstalling apache server and its PHP extension.
Step 5: Start Coding for controlling GPIO pin using this Raspberry Pi Webserver
Now delete the previous code in index.php (<?php phpinfo(); ?>) file and insert below PHP code to control GPIO pins inside body of HTML code.
Below is the complete code for creating two buttons to turn on and off the LED connected to Raspberry Pi.
In above code there is a PHP script which checks which button is pressed by using below code and then turns on and off the LED accordingly.
Here we have used shell_exec() command in php code, this command is used to run the shell command from the PHP script. Learn more about shell_exec here. If you run the command inside shell_exec directly form the terminal of Raspberry pi, you can directly make GPIO pin 27 low or high. Below are two commands to test the LED directly from terminal.
After completing this, run the code in your browser by typing the IP address of raspberry pi in the browser. You will see 2 buttons - ON, OFF to control your LED by clicking these buttons.
This is a continuation of my pervious article on gathering System information of a Raspberry Pi with Python and Flask. In this article we can see how to run that application via Apache Web Server.
In my previous article , I have deployed the Python Flask application into a folder named “/home/pi/www/sysinfo” in which “pi” is the username which may change in your case if you have configured the Raspbian OS with different user name. To configure routing via Apache, I have added a folder named “logs” to the existing project folders and have added a new file named sysinfo.wsgi . This is an Apache wsgi configuration file and this requires the “wsgi” modeule for Apache to be installed in Raspberry Pi. If you have not installed this module before, you can install it by executing the below command in the terminal
sudo apt-get install libapache2-mod-wsgi
Add the below script to the newly created file “sysinfo.wsgi” ( change the folder path in the below script according to your file location)
Note: You can create this file with leafpad editor or inbuilt terminal editor nano. If you are using nano , execute the command “nano /home/pi/www/sysinfo/sysinfo.wsgi” in terminal and copy paste the above script
The next step is to create a configuration file for Apache Web Server. This requires the default ‘pi” user to be added to the “www-data” user group of your Linux ( Raspbian) system. To check the user’s groups, execute the command “id pi” in terminal. This displays the list of user groups in which the user ‘pi’ is a member of. If the “pi” user is not a member of “www-data” group add the user to this group by executing the below command
Raspberry Pi Web Server Apache Download
sudo usermod –a -G “www-data” pi Editplus for mac free download.
The above command adds the existing user “pi” to the user group “www-data”
To create the config file execute the below command in terminal
sudo nano/etc/apache2/sites-available/sysinfo.conf
Copy paste the below scripts to the file and save it.
Raspberry Pi Apache2
Note: Change the folder location based on the location of your site
The final step is to tie the knot between your python website and Apache web server. This is done by executing the below command in terminal. This adds a virtual path to Apache and the Python application would be served as a WSGI application by Apache web server.
sudo /usr/sbin/a2ensite sysinfo.conf
The above command expects the Apache configuration to be re-loaded. This can be done by executing the below command in terminal.
sudo service apache2 reload
Now we would be able to access the python flask web application by accessing http://[defaultapachewebsiteurl]/sysinfo . In my case it’s http://192.168.0.106/siteinfo. You can port forward this site as a regular Apache website and access it from outside your environment.
The below is output of the application accessed on LAN
Raspberry Pi Web Server Apache Download
Currently I am behind a double NATed network and my service provider does not support port forwarding. But I have a work around with different set of tools to access my Raspberry Pi outside my home network which I will explain in detail in subsequent articles.
