Hour 22. Web Programming
What You’ll Learn in This Hour:
Installing a web server on your Raspberry Pi
Using CGI to run your Python programs from the Web
How to generate dynamic webpages using Python
How to retrieve form data in your Python web programs
With the popularity of the World Wide Web, these days it’s often a requirement to write applications
that are Web aware. While Python wasn’t intended to be a Web-based programming language, over
the years it has evolved to provide many Web features. In this hour, you’ll learn how to move your
Python programs into the Web world, using the Apache web server and some Python modules that are
part of the standard Python library on your Raspberry Pi.
Running a Web Server on the Pi
Before you can move your Python applications into the Web world, you need to have a web server to
host them. While the Raspberry Pi isn’t intended to be a production web server that supports
thousands of customers, it works just fine as a host for small intranet applications on your local
network.
As with just about everything else in the Linux world, there are a few different web servers that you
can choose to install on your Raspberry Pi. Here’s a list of the most popular ones:
Apache—A full-blown production web server environment that runs on many platforms.
Nginx—A lightweight web and email server package.
Monkey HTTP—A development web server built for the Linux environment.
lighttp—A small web server that focuses on performance.
The Apache web server is by far the most popular web server used on the Linux platform. It runs just
fine on the Raspberry Pi, as long as you don’t try hosting thousands of concurrent users. You’ll use the
Apache web server in the examples in this hour. The following sections walk you through setting up
the popular Apache web server in your Raspberry Pi environment.
Installing the Apache Web Server
Installing the Apache web server on the Raspberry Pi is a simple process, thanks to the Raspbian
software repository. The complete Apache web server package is contained in a single software
package, apache2. You simply use the apt-get utility to install it:
Click here to view code image
pi@raspberypi ~ $ sudo apt-get install apache2
The apache2 package installs the web server and all the supporting files required to run the server.
Table 22.1 shows some of the most important files and folders that you need to become familiar with
as you use the Apache web server.