- Save the file and then exit the editor.
- Copy the script2201.html file to the /var/www folder, like this:
Click here to view code image
pi@raspberrypi ~ $ sudo cp script2201.html /var/www
pi@raspberrypi ~ $ - Change the mode of the new file so that it will be readable by everyone:
Click here to view code image
pi@raspberrypi ~ $ sudo chmod +r /var/www/script2201.html
pi@raspberrypi ~ $ - Open a browser and navigate to the new webpage by using the URL
http://localhost/script2201.html.
Congratulations! You just published a webpage on your Apache web server! The next step is to
publish your Python programs.
Programming with the Common Gateway Interface
There are a few different ways to publish Python programs on the Apache web server. We’ll take a
look at the oldest and easiest way of do so: using the Common Gateway Interface (CGI).
The following sections describe the CGI and how to use it with your Python programs to allow
remote network clients to run and interact with your programs.
What Is CGI?
The CGI is a feature built into the Apache web server that allows remote clients to run shell scripts
on the host server. Allowing unknown visitors to your website to run scripts can be dangerous.
However, with the proper security controls, running scripts provides a new dimension to web
applications.
By default, the CGI in the Apache web server restricts shell scripts to a specific folder on the server:
/usr/lib/cgi-bin. This is where you must place your Python scripts so that remote clients can
run them.
Running Python Programs
To get a Python program to run as a CGI script, you have to modify it a bit. First, you have to tell the
shell that it’s a Python program. You do this by using the #! command, pointing to the standard path
of the python3 application. For the Raspberry Pi environment, you just add this line to the top of
your Python programs:
#!/usr/bin/python3
When the Linux system runs the shell script, it knows to process the program through the python3
interpreter.
Another issue with running your Python program via the Apache web server is that you won’t have
access to the command prompt to view the output from your program. Instead, the Apache CGI
redirects any output from your Python program directly to the web client. That setup is problematic