0

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!

7
  • 404: what URL are you accessing? And perhaps other parts of your config are incomplete? Assuming there's a default index.html , do you get to see that one at least when visiting 127.0.0.1 ? Commented Nov 27, 2012 at 12:48
  • i open /localhost/cgi-bin/script.py - the actual path where the script is and where apache has access. There is no index.html/index.php/index.else Commented Nov 27, 2012 at 13:02
  • Surely you mean http://localhost/cgi-bin/script.py? My apache configuration is a bit rusty, but possibly, you can leave off the .py extension there as well. But for your (and our) sanity, it'd be good to put an (empty) index.html file in /var/www/ and see if you can at least find that. Commented Nov 27, 2012 at 13:24
  • Yes! localhost/index.html and localhost/index.php can be found! Even <?php phpinfo() ?> testpage works fine. But it is about php, not python... Commented Nov 27, 2012 at 13:32
  • No, it's about an executable, whether that be shell, python, perl or php (the php you're seeing goes through modphp, not modcgi). You could try a perl script instead, but that will probably also fail. But that's beside the point. Commented Nov 27, 2012 at 13:50

2 Answers 2

2

At a guess, you have your script in /var/www/cgi-bin/cgi101.py.

But your Apache configuration has this:

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>

and add the AddHandler directive in there as well:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
        AddHandler cgi-script .cgi .py
    </Directory>

(and perhaps remove the section above this one, for the /var/www directory.)

I would then put your script at /usr/lib/cgi-bin/cgi101.py, and try again.

The Apache tutorial seems actually pretty clear on this.

Sign up to request clarification or add additional context in comments.

2 Comments

Just noticed the update, which tells exactly what I say above ;-).
Thank you !!! Works perfectly. Probably I have eyes in ... somewhere else than where they should be.
0

At first you must be sure that your script can be run by Python/OS. In your case you try to use Python3 and I see that you use old form of print. For Python3 you must use print() as function. Your script must run when you try to execute it from command line. Make sure it has execute attribute.

When you script will run from command line then you can try to run it via CGI. When in trouble check http server logs. They will probably be in /var/log/http and there should be error.log. Check it.

Also make sure that your file use unix type EndOfLine, i.e. LF only, not CRLF from DOS/Windows. If you use CRLF then first line (shebang) is corrupted.

Also you must separate HTTP headers from content using empty line. So instead of

print("Content-Type: text/html")

use:

print("Content-Type: text/html\n")

8 Comments

If the script won't run, the status returned to the browser will be 500, not 404.
You are right. Then maybe it is incorrect URL. But at first I would make it runnable from command line, then I would check http server logs.
I've done this before posting, the script runs successfully in shell (~$ python3 /var/www/cgi-bin/script.py). And the problem is really in apache configuration. i'm sure.
And sorry for misunderstanding, all above was correct, i actually use print() for py3.
Also try to execute your script from shell as: ~$ /var/www/cgi-bin/script.py i.e. not by python3, but via shebang (first line in your script).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.