This is a Python CGI Web Hello World for Windows and Linux.
- Install Apache
- Install Python
- Create 1.py as following.
- Put 1.py in htdocs directory
- In httpd.conf uncomment and add .py to AddHandler so you will have something like: AddHandler cgi-script .cgi .py
- In httpd.conf add ExecCGI to Options so you will have somthing like: Options Indexes FollowSymLinks ExecCGI
- Just watch out for multiple instances of Options and add to the correct one otherwise you will get different errors such as: Options ExecCGI is off in this directory.
- Restart Apache
- Run http://localhost/1.py
#!D:/dev/Python3.1.2/python.exe
print("Content-type: text/html")
print() # this blank line is DEFINITELY needed.
print("")
print("Hello.")
print("")
Under Linux with Python 2.x the script should have 755 permission and the following is expected in /etc/apache2/sites-available/default in Ubuntu 11.04:
ScriptAlias /cgi-bin/ /home/reza/www/cgi-bin/
<Directory "/home/reza/www/cgi-bin">
AllowOverride None
Options ExecCGI SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
The script:
#!/usr/bin/env python
print "Content-Type: text/html"
print """\
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""
is expected to be under ~/www/cgi-bin/ directory and will run by: http://localhost/cgi-bin/index.py