This article is to provide step by step instructions for making Raspberry Pi as a web server where we can publish our web pages over a local network using python scripts. To set up and use a simple web server using python a framework called Flask is required. With all these packages and with a basic idea about these packages, you can able to create web based applications easily.
In this project the Raspberrypi board is booted with the Ubuntu OS and is connected to the Ethernet port of a Windows7 PC. The board is connected to the internet connection as explained in the project Connecting the Raspberrypi to the internet. The IP address of the Raspberrypi board has been obtained to remote login in the TUI using the PUTTY and is remotely accessed using VNC.
Make sure you’re up-to-date:
To check for update and to install them enter the following command:
sudo apt-get update
In this tutorial we use a Python web framework called Flask to create a dynamic web server in Raspberry Pi. While there’s a lot you can do with Flask “out of the box,” it also supports many different extensions for doing different operations such as user authentication, generating forms, and using databases. Flask also enable you to have access to the wide variety of standard Python libraries that are available online. In order to install Flask, you’ll need to have pip installed. It can be installed by entering the following command in the terminal.
sudo apt-get install python-pip
Fig. 1: Install Pip And Flask To Create A Python-Powered Web Server
After pip is installed, you can use it to install Flask and its dependencies:
sudo pip install Flask
Note: commands are case-sensitive.
Making Your Python Web Page:
Fig. 2: Creating Basic Web Page With Flask And Run On Raspberry Pi
To test the installation, create a new file called hello_python_web.py with the code from below by the following command.
sudo nano hello_python_web.py
from flask import Flask
app = Flask(__name__)
return “Hello World!”
if __name__ == “__main__”:
app.run(host=’0.0.0.0′, port=80, debug=True)
Note: “0.0.0.0” in above program should be changed to your IP address. You can use the ifconfig command to find the IP address of the Raspberry Pi.
Now the server is ready to start, we can use the following command to run the webserver:
sudo python hello_python_web.py
Fig. 3: Running Raspberry Pi Web Server