I'm trying to run it on localhost. A short introduction. Some links I've read before post: How to run Python CGI script How can I run Python CGI scripts on my web server? most precise: Need Help to Configure apache Server to run CGI Script written in Python And also official http://httpd.apache.org/docs/1.3/misc/FAQ.html#CGIoutsideScriptAlias http://www.editrocket.com/articles/python_apache_windows.html and other...
All steps they suggest to configure Apache for .cgi and .py scripts: 1)
install libapache2-mod-wsgi
[done] 2) check the script is executable and available for apache ~$ ls -lah /var/www/cgi-bin/cgi101.py
-rwxrwxr-x 1 user user 318 2012-11-27 03:03 /var/www/cgi-bin/cgi101.py
3) edit /etc/apache2/sites-available/default[updated to actual]:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
# <Directory /var/www>
<Directory /var/www/cgi-bin>
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride None
Order allow,deny
allow from all
AddHandler cgi-script .cgi .py
# AddHandler wsgi-script .wsgi
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
or with
<Directory /var/www/cgi-bin>
then restart Apache
sudo /etc/init.d/apache2 restart
and finally, try the script. Something like[updated to actual]:
#!/usr/bin/python3
import cgi
form = cgi.FieldStorage()
# parse form data
print('Content-type: text/html\n')
# hdr plus blank line
print('<title>Reply Page</title>')
# html reply page
if not 'user' in form:
print('<h1>Who are you?</h1>')
else:
print('<h1>Hello <i>%s</i>!</h1>' % cgi.escape(form['user'].value))
On the output i get
404 Not found
error. WTH? I've tried both and /var/www. And my python path to python3:
~$ ls -lah /usr/bin/python*
...
lrwxrwxrwx 1 root root 9 2011-10-05 23:53 /usr/bin/python3 -> python3.2
lrwxrwxrwx 1 root root 11 2012-10-20 06:17 /usr/bin/python3.2 -> python3.2mu
-rwxr-xr-x 1 root root 2.8M 2012-10-20 06:17 /usr/bin/python3.2mu
...
that's why i use
#!/usr/bin/python3
Thanks in advance!
[updated] /var/log/apache2/error.log
[Tue Nov 27 13:47:56 2012] [error] [client 127.0.0.1] script not found or unable to stat: /usr/lib/cgi-bin/script.py, referer: http://localhost/cgi1$
Why it looks for script.py in /usr/lib/ ??
[updated] I closed my eyes while reading the lines
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
It must be path to executable .cgi or .py in /var/www/cgi-bin. Thanks Evert and everyone!