I would like to remind you that it is very good practice to learn how to use a Virtual Machine in order to learn in a sandbox environment. Many of the things that you will do when playing around with Apache will require elevated privileges, so be careful.
Students using Windows may download Apache from here. macOS ships with Apache so you probably don't need to do something for now.
We will edit some configuration files to have Apache serve content from the directory that you want.
First you need to locate httpd.conf
and open it with your favorite text editor. In order to edit
httpd.conf
you need elevated priviledges so the easiest way it would be to open it using
sudo
on Mac:
$ sudo vim httpd.conf
You can replace vim
with the command to open your text editor. You need to be in the proper
directory, probably that would be /etc/apache2
.
On Windows, you need to Run as Administrator
in order to be able to make changes to
sudo
. The file will be in the directory that you unpacked when you downloaded Apache.
In httpd.conf
you will find a lot of lines. Some that are of interest to us are
the following:
Find this line:
LoadModule cgi_module libexec/apache2/mod_cgi.so
and uncomment it.
Apache needs to know that a particular directory is set aside for CGI programs.
It will assume that every file in this directory
is a CGI program, and will attempt to execute it,
when that particular resource is requested by a client. This is done by
ScriptAlias
. For example by default in Mac there will be a line
ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
which means that there is a preset ScriptAlias for the /cgi-bin/
to
/Library/WebServer/CGI-Executables/
. You can change this to whatever folder you want
to use.
If in Windows you can't find this specific file, don't worry. Just set the ScriptAlias to your working directory and then just add the appropriate lines using the folder that you chose.
Next we want to find the following:
<Directory "/Library/WebServer/CGI-Executables">
AllowOverride None
Allow from all
Options ExecCGI
Require all granted
AddHandler cgi-script .py
</Directory>
This will not be exactly as you see it as I have added the lines that you need to type in.
Windows user could (for example) start with:
<Directory "/path/to/whatever/folder/you/are/using">
AllowOverride None
Allow from all
Options ExecCGI
Require all granted
AddHandler cgi-script .py
</Directory>
Now we will create a simple HTML file, named input_name.html
that will prompt the user to write
something in a box and will point to a
form.py
script that lives in the webserver and it will do something with the information it gets from
input_name.html
:
<!doctype html>
<html>
<head>
<title>Demo HTML file</title>
</head>
<body>
<form name="pyform" method="POST" action="http://localhost/cgi-bin/form.py">
<input type="text" name="fname" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Additionally, we'll create a form.py
script to print what the user wrote in the box
of input_name.html
:
#!/usr/local/bin/python3
print("Content-Type: text/html")
print()
import cgi,cgitb
cgitb.enable() #for debugging
form = cgi.FieldStorage()
name = form.getvalue('fname')
print("Name of the user is:",name)
Notice the #!/usr/local/bin/python3
in the beginning? This needs to be altered to an absolute path of
your python3 interpreter.
Remember to change your script's permissions:
$ sudo chmod 755 form.py
To understand file permissions better take a look here
Now, your HTML file can live anywhere in your computer. Just open it with a browser and (hopefully) you'll see
a nice little box with a button that reads "Submit" next it. Type something in the box and click on the button. Let's
say that you typed Wookie
. If everything worked properly, your python script that lives in
the designated /cgi-bin/
folder should be called and you should see:
Name of the user is: Wookie