Last active
December 23, 2022 22:23
-
-
Save nealey/8aa6627b354ec69f9cf7fef84d656cc8 to your computer and use it in GitHub Desktop.
Raspbian: set up Apache with CGI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# Let the user write to the HTML directory | |
sudo chown pi /var/www/html | |
# Set up Apache | |
sudo apt update | |
sudo apt install apache2 | |
sudo a2enmod cgi | |
sudo apache2ctl graceful | |
sudo tee /usr/lib/cgi-bin/handler.cgi <<'EOF' >/dev/null | |
#! /usr/bin/python | |
import cgitb; cgitb.enable() | |
import sys | |
sys.path.insert(0, "/var/www/html") | |
import handler | |
EOF | |
sudo chmod +x /usr/lib/cgi-bin/handler.cgi | |
tee /var/www/html/handler.py <<'EOF' >/dev/null | |
#! /usr/bin/python | |
import cgi | |
form = cgi.FieldStorage() | |
print("Content-Type: text/html") | |
print("") | |
print("<p>") | |
print("The value of fred is:") | |
print(form.getfirst("fred")) | |
print("</p>") | |
cgi.print_form(form) | |
EOF | |
chmod +x /var/www/html/handler.py | |
tee /var/www/html/sample.html <<'EOD' >/dev/null | |
<html> | |
<head><title>Sample Form</title></head> | |
<body> | |
<h1>Sample Form</h1> | |
<form method="post" action="/cgi-bin/handler.cgi"> | |
<input type="text" name="fred"> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> | |
EOD | |
echo | |
echo "All done!" | |
echo "A sample form can be loaded at http://localhost/sample.html" | |
echo "Your CGI handler is in /var/www/html/handler.py" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment